2013年6月4日 星期二

[C]Chapter 7 homework


n  (20%)輸入一個字元,輸出其ascii code number,並判斷此字元
u 若為數字,則輸出”number”
u 若為字母,則輸出”alphabet”
u 其他則輸出”others”
u 可重複輸入,直到輸入’!’離開
n  (20%)讀一個字串 scanf(“%s”, str); 計算字串長度並輸出
u 必須用function, array,傳陣列給function
u strlen() 去做驗證是否一樣
n  (20%) A 1-D array has these ten elements:
 4.4 3.3 2.2 5.5 1.1 6.6 7.7 10.0 9.9 8.8n  Write a program to sort the array in descending order.
n  Find the max number of  the 10 elements, swap this value with the first element in the array
    Then, find the max number of  the 9 elements (except first element), swap this value with the second element in the array    …


編輯紀錄


6/7
1.補上第一題可輸入無限次,直到輸入”!”表停止。
2.第三題改為float,以支援非整數


#include <stdio.h>	//For I/O
#include <stdlib.h>	//For Universal Tool
#include <string.h>	//For strlen()
#include <ctype.h>	//For isdigit()

#define SIZE_OF_STRING 300	//For Second question
#define SIZE 10	//For Third question

void first_question(void);
void second_question(void);
int second_question_count_string_length(char test[]);
void third_question(void);
void third_question_sort_descending_order(float data[],int size);

void main (void)
{
	for(;;)
	{
		int question;
		char again=0;
		printf("Please enter number to choose question to Demo>");
		scanf("%d",&question);
		switch(question)
		{
		case 1:
			first_question();
			break;
		case 2:
			second_question();
			break;
		case 3:
			third_question();
			break;
		default:
			printf("The question is not exists\n");
		}
		printf("Demo other question ? [Y/N]");
		while(again != 'Y' && again != 'y' && again !='n' && again !='N')
		{
			fflush(stdin);
			again=getchar();
		}
		if(again == 'n' || again == 'N')
			break;
	}
	system("pause");
}

//第一題
void first_question(void)
{

	char test_character;
	while(true)
	{
	printf("please enter character > ");
	fflush(stdin);	//抓字之前先清掉input buffer避免出錯
	test_character=getchar();	//用getchar()抓字存在test_character
		/*
		ASCII CODE
		! = 33
		大寫字母=65~90
		小寫字母=97~122
		*/
		if(!(test_character ==33))
		{
		printf("The ASCII code of %c is %d\n",test_character,test_character);
		if(isdigit(test_character))	//利用isdigit檢查是不是數字
			printf("number\n");
		else if((test_character >= 65 && test_character <= 90) || (test_character >= 97 && test_character <= 122))
			printf("alphabet\n");
		else
			printf("other\n");
		}
		else
			break;
	}
}

//第二題
void second_question(void)
{
	char user_input_string[SIZE_OF_STRING];
	printf("please enter string to count length >");
	scanf("%s",user_input_string);
	//宣告兩個變數分別儲存自己函數與strlen()的計算結果
	int result_myself=second_question_count_string_length(user_input_string),result_strlen=strlen(user_input_string);
	printf("The counting result by function which is programmed by myself is %d\n",result_myself);
	printf("The counting result by library function,strlen() is %d\n",result_strlen);
	if(result_myself == result_strlen)	//如果兩個相等直接輸出code是對的
		printf("The code is corrent\n");
	else
		printf("The code is error\n");
}

int second_question_count_string_length(char test[])
{
	//利用字串節尾是\0的特性做計數
	int i=0;
	while(test[i]!='\0')
		i++;
	return i;
}

//第三題
void third_question(void)
{
	float data[SIZE];
	printf("please input the elements\n");
	//利用迴圈讓使用者輸入10筆資料
	for(int i=0;i<SIZE;i++)
	{
		printf("please input the %d-th element >",i+1);
		scanf("%f",&data[i]);
	}
	//傳入函數排序
	third_question_sort_descending_order(data,SIZE);
	//將排序後的資料重新列出
	printf("The sort descending order is :\n");
	for(int i=0;i<SIZE;i++)
		printf("%.1f\t",data[i]);
	printf("\n");
}
	
void third_question_sort_descending_order(float data[],int size)
{
	//氣泡排列法
	//效率不佳,不使用
	/*
	for(int i=0;i<size;i++)
	{
		for(int j=0;j<size;j++)
		{
			if(data[i]>data[j])
			{
				int temp=data[j];
				data[j]=data[i];
				data[i]=temp;
			}
		}
	}
	*/
	
	//插入排序法
	for(int i=1;i<size;i++)
	{
		float temp=data[i];
		int j=i-1;
		while(j>=0 && data[j] < temp)	//index不能<0 所以加上j>=0的條件
0		{
			data[j+1]=data[j];
			j--;
		}
		data[j+1]=temp;
	}
}

沒有留言:

張貼留言