小学四则运算

#include
#include
#include
#include

int menu()
{
	int choice;
	system("cls");
	printf("1:加法\n");
	printf("2:减法\n");
	printf("3:乘法\n");
	printf("4:除法\n");
	printf("0:退出\n");
	printf("请选择运算符号:");
	scanf("%d",&choice);
	while(choice<0||choice>4)
	{
		printf("请重新选择:");
		scanf("%d",&choice);
	}
	return choice;
}

void Start_Game(int n)
{
	int sum=0;//总题数
	int correct=0;//正确数
	int a,b;//两个操作数
	int ans;//记录结果
	char temp;
	system("cls");
	while(1)
	{
		srand(time(NULL));
		a=rand()%50;
		b=rand()%50;
		while(n==4 && a%b!=0)
		{
			a=rand()%50;
			b=rand()%50;
		}
		if(n==1)
		{
			printf("%d + %d = ",a,b);
			scanf("%d",&ans);
			if(ans == a+b)
			{
				correct++;
			}
		}
		if(n==2)
		{
			printf("%d - %d = ",a,b);
			scanf("%d",&ans);
			if(ans == a-b)
			{
				correct++;
			}
		}
		if(n==3)
		{
			printf("%d * %d = ",a,b);
			scanf("%d",&ans);
			if(ans == a*b)
			{
				correct++;
			}
		}
		if(n==4)
		{
			printf("%d / %d = ",a,b);
			scanf("%d",&ans);
			if(ans == a/b)
			{
				correct++;
			}
		}
		sum++;
		printf("是否继续?(Y/N):");
		fflush(stdin);
		scanf("%c",&temp);
		if(temp=='n'||temp=='N')
		{
			break;
		}
	}
	printf("一共回答了%d题\n",sum);
	printf("回答对了%d题\n",correct);
	printf("正确率为%.2f%%\n",correct*100.0/sum);
	system("pause");
}	

int main()
{
	int choice=menu();
	Start_Game(choice);
	return 0;
}

你可能感兴趣的:(C/C++)