四则运算

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int scan()
{
char s[100];
int i,t,z=0;
do
{
z=0;
gets(s);
for(i=0;s[i]!='\0';i++)
if(s[i]<'0' || s[i]>'9')
break;
if(i>=strlen(s))
for(t=0;s[t]!='\0';t++)
z=z*10+((int)s[t]-48);
else
printf("您的输入有误,请重新输入");
}
while(i<strlen(s));
return(z);
}
int x=1,y=1,choice=0,sum=0,right=0,z=0,answer;
float precent;
char fuhao;
void suiji()
{
srand(time(NULL));
x=rand()%100+1;
y=rand()%100+1;
}void choose()
{
int yunsuan(int,int,int);
if(sum==0)
precent=0.0;
else
precent=(float)right/sum;
printf("\n如需进行加减乘除运算测试,请分别输入1、2、3、4\n");
printf("如需退出请输入5\n"); choice=scan();
switch(choice)
{
case 1:printf("欢迎来到加法运算测试系统,如需退出请输入00\n");
fuhao='+';break;
case 2:printf("欢迎来到减法运算测试系统,如需退出请输入00\n");
fuhao='-';break;
case 3:printf("欢迎来到乘法运算测试系统,如需退出请输入00\n");
fuhao='*';break;
case 4:printf("欢迎来到除法运算测试系统,如需退出请输入00\n");
fuhao='/';break; case 5:printf("本次测试结束,欢迎下次再来测试\n");
printf("本次共做%d道题,其中%d道正确,正确率为%f\n",sum,right,precent); break;
default:printf("您的输入有误,请重新输入\n");choose();
}
}
int ans(int x,int y,int choice)
{
switch(choice)
{
case 1:z=x+y;break;
case 2:z=x-y;break;
case 3:z=x*y;break;
case 4:z=x/y;break;
} return(z);
}
void yunsuan()
{
suiji();
printf("%d%c%d=",x,fuhao,y);
z=ans(x,y,choice);
answer=scan();
if(z==answer)
{
printf("正确\n");
right++;
sum++;
yunsuan();
} else if(answer==00)
choose();
else
{
printf("不正确, 正确答案为%5d\n",z);
sum++;
yunsuan();
}
}
void main()
{
printf("欢迎来到四则运算测试系统\n");
choose();
while(choice!=5)
yunsuan();
}

posted @ 2015-03-26 11:33  44刘芮熔  阅读(122)  评论(1编辑  收藏  举报