首页 > 技术文章 > 程序2

yikechaguazi 2018-10-07 20:41 原文

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int A();
int B();
int C();
int A()
{int m,n,r,num1,num2,num3;
srand((unsigned)time(NULL));
m=rand()%100+1;
n=rand()%100+1;
if (m<n)
{r=m;m=n;n=r;}
char op_lib[4]={'+','*','/','-'};
int rand_oper_idx=rand()%4;
char cur_oper=op_lib[rand_oper_idx];
if(cur_oper=='+')num1=m+n;
else if(cur_oper=='*')num1=m*n;
else if(cur_oper=='/')num1=m/n;
else (cur_oper=='-')num1=m-n;
printf ("%d,%c,%d=\n",m,cur_oper,n);
scanf(“%d”,&num2);
if (num2==num1)
{printf("答案正确”);num3=1;}
else {printf("答案错误");num3=0;}
}
int B()
{double m,n,r,num1,num2;int num3;
m=rand()/(double)(RAND_MAX);
n=rand()/(double)(RAND_MAX);
if (m<n)
{r=m;m=n;n=r;}
char op_lib[4]={'+','*','/','-'};
int rand_oper_idx=rand()%4;
char cur_oper=op_lib[rand_oper_idx];
if(cur_oper=='+')num1=m+n;
else if(cur_oper=='*')num1=m*n;
else if(cur_oper=='/')num1=m/n;
else (cur_oper=='-')num1=m-n;
printf ("%lf,%c,%lf=\n",m,cur_oper,n);
scanf(“%lf”,&num2);
if (num2==num1)
{printf("答案正确”);num3=1;}
else {printf("答案错误");num3=0;}
}
int C()
{double m,r,num1,num2,num3;int n;
srand((unsigned)time(NULL));
m=rand()/(double)(RAND_MAX);
n=rand()%100+1;
if (m<n)
{r=m;m=n;n=r;}
char op_lib[4]={'+','*','/','-'};
int rand_oper_idx=rand()%4;
char cur_oper=op_lib[rand_oper_idx];
if(cur_oper=='+')num1=m+n;
else if(cur_oper=='*')num1=m*n;
else if(cur_oper=='/')num1=m/n;
else (cur_oper=='-')num1=m-n;
printf ("%d,%c,%d=\n",m,cur_oper,n);
scanf(“%d”,&num2);
if (num2==num1)
{printf("答案正确”);num3=1;}
else {printf("答案错误");num3=0;}
}
int main()
{int num4,num5,num6,num7=0;
printf("请输入题目数量”);
scanf("%d",&num4);
while(num7<num4)
{srand((unsigned)time(NULL));
num5=rand()%100+1;
if (num5<34) num6=A();
else if(num5>=34&&num5<=67) num6=B();
else(num5>=68&&num5<=100) num6=C();
}
num7++;
return 0;
}

推荐阅读