小学四则运算

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值