2013年5月21日 星期二

[C]Midterm 2

答案供參考,完全憑印象中的題目去寫的@@

考試的時候我是一個程式一個cpp檔

不過剛剛重寫,索性就把他全部整合再一個程式了

我有標在各個function名稱旁寫註解,註明這個function的功用,哪一題不懂,可以直接去看那個function

main()裡面都只是引導語跟input/output而以

#include <stdio.h>
#include <stdlib.h>
//Phototype
void swap(int* a,int* b);
void triangle_a(int level,int symbol);
void triangle_b(int level,int symbol);
int seven(int* number);
int prime_test(int number);
void prime_number_print(void);

void main(void)
{
	int choose;
	for(;;)
	{
		char again=0;
		printf("請輸入題號\n");
		printf("\t1-兩數字比大小,第一個大於第二個則交換,且以0和1控制是否繼續進行\n");
		printf("\t2-A組數字三角形\n");
		printf("\t3-B組數字三角形\n");
		printf("\t4-檢驗5位數字有幾個7,超過三個就改成全部皆為7\n");
		printf("\t5-列出1~1000的質數\n");
		printf("要Demo的題號是? >");
		scanf_s("%d",&choose);
		//給第一題的變數
		int play,first_number,second_number;
		//給第二題和第三題的變數
		int level,symbol;
		//給第四題的變數
		int number;
		switch(choose)
		{
		case 1:
			printf("You choose the first question - 交換數字\n");
			do{
			printf("please enter first number >");
			scanf_s("%d",&first_number);
			printf("please enter second number >");
			scanf_s("%d",&second_number);
			printf("You inupt number is %d and %d\n",first_number,second_number);
			swap(&first_number,&second_number);
			printf("after fuction now they is %d and %d\n",first_number,second_number);
			printf("Do you want to once again ? input 1 = again , inupt 0 = exit\n");
			printf("Your choose is ? >");
			scanf_s("%d",&play);
			}while(play==1);
			break;
		case 2:
			printf("You choose the Second question - A組數字三角形\n");
			printf("please enter triangle level>");
			scanf_s("%d",&level);
			printf("please neter triangle symbol>");
			scanf_s("%d",&symbol);
			triangle_a(level,symbol);
			break;
		case 3:
			printf("You choose the Third question - B組數字三角形\n");
			printf("please enter triangle level>");
			scanf_s("%d",&level);
			printf("please neter triangle symbol>");
			scanf_s("%d",&symbol);
			triangle_b(level,symbol);
			break;
		case 4:
			printf("You choose the fourth question - 5-digit check\n");
			printf("please enter 5-digit number >");
			scanf_s("%d",&number);
			printf("Your input is %d",number);
			printf(" have %d seven(s)",seven(&number));
			printf("\nAnd now it is %d\n",number);
			break;
		case 5:
			printf("You choose the fourth question - prime number from 1 to 1000\n");
			prime_number_print();
			printf("\n");
			break;
		}
		printf("Do you want to Demo else question ? [Y/N]");
		while(again != 'n' && again != 'N' && again != 'y' && again != 'Y')
			again=getchar();
		if(again == 'n' || again == 'N')
			break;
	}
	printf("Bye! Bye!");
	system("pause");
}

//比大小和交換
void swap(int* a,int* b)
{
	int temp=*a;
	if(*a > *b)
	{
		*a=*b;
		*b=temp;
	}
}

//A組的三角形
void triangle_a(int level,int symbol)
{
	for(int i=level;i>0;i--)
	{
		for(int j=0;j<i;j++)
			printf("%d",symbol);
		printf("\n");
	}
	for(int i=1;i<=level;i++)
	{
		for(int j=0;j<i;j++)
			printf("%d",symbol);
		printf("\n");
	}
}

//B組的三角形
void triangle_b(int level,int symbol)
{
	for(int i=1;i<level;i++)
	{
		for(int j=0;j<i;j++)
			printf("%d",symbol);
		printf("\n");
	}
	for(int i=level;i>0;i--)
	{
		for(int j=0;j<i;j++)
			printf("%d",symbol);
		printf("\n");
	}
}

//檢驗7的個數
int seven(int* number)
{
	int temp=*number,result=0;
	while(temp > 0)
	{
		if(temp%10 == 7)
			result++;
		temp=temp/10;
	}
	if(result >= 3)
		*number = 77777;
	return result;
}

//質數檢查,列出質數使用
int prime_test(int number)
{
	if(number%2 == 0 && number == 2)
		return 1;
	int i=2;
	for(;;)
	{
		if(number%i == 0)
		{
			return 0;
			break;
		}
		if(i>=number/2)
			break;
		i++;
	}
	return 1;
}

//列出1~1000的質數
void prime_number_print(void)
{
	int row_con=1;
	for(int i=1;i<1000;i++)
	{
		if(prime_test(i))
		{
			printf("%d\t",i);
			row_con++;
		}
		if(row_con > 10)
		{
			printf("\n");
			row_con=1;
		}
	}
}

沒有留言:

張貼留言