C语言编程:随机出10道100以内的整数加减法算术题。

如题

这个其实很简单
给你个完整的,我很少写完整的代码
#include <stdio.h>
main()
{
int i;
int a[10],b[10],c[10],d[10],e[10];
for(i=0;i<10;i++) //生成题目
{
while(1)
{
a[i]=rand()%100+1; //产生ab随机数
b[i]=rand()%100+1;
c[i]=rand()%4+1; //产生运算+-*/随即数
switch(c[i])
{
case 1:d[i]=a[i]+b[i];break;
case 2:d[i]=a[i]-b[i];break;
case 3:d[i]=a[i]*b[i];break;
case 4:while(b[i]==0) //在除法中,要是b是0,重新生成,直到不是0为止
b[i]=rand()%100+1;
d[i]=a[i]%b[i];break; //这里看余数,一般来说,不能整除,不可以
}
if((d[i]<100||d[i]>0)II(c[i]==4&&d[i]!=0)) //判断d是否在100之内,还有就是除法不能有余数
break; //满足条件,生成下一个,不满足重新来过
}
if(c[i]==4)
d[i]=a[i]/b[i]; //这个算出除法的结果
}
for(i=0;i<10;i++) //出题
{
printf("%d\n%d",i+1,a[i]);
if(c[i]==1)
putchar("+");
if(c[i]==2)
putchar("-");
if(c[i]==3)
putchar("X");
if(c[i]==4)
putchar("/");
printf("%d=",b[i]);
scanf("%d",e[i]);
system("cls");
}
for(i=0;i<10;i++) //最后打印结果
{
printf("%d\n%d",i+1,a[i]);
if(c[i]==1)
putchar("+");
if(c[i]==2)
putchar("-");
if(c[i]==3)
putchar("X");
if(c[i]==4)
putchar("/");
printf("%d=%d",b[i],e[i]);
if(d[i]==e[i])
printf("yes!!\n");
else
printf("NO!! %d\n",d[i]);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-11-25
用rand()产生随机数,rand()会产生从0到一个很大的数,我记不清了,反正很大。如果想出现100以内的,就用rand()%100。你可以定义三个int型整数,两个表示加或者减的对象,另一个标示加或者减,因为只要出现两种情况之一,所以可以用rand()%2,这样只会有0,1两种情况来标示加或者减。下面的就很容易了
int a,b,i,c;
for(i = 0;i < 10;i++)
{
a = rand()%100;
b = rand()%100;
c = rand()%2;
if(c == 0)//标示加法
{
printf("%d + %d = %d\n",a,b,a+b);
}
else
{
printf("%d - %d = %d\n",a,b,a-b);
}
}
大致就这样,希望给你点帮助本回答被提问者采纳
第2个回答  2012-03-15
60- 2 = ?
89-32 = ?
19+56 = ?
63-11 = ?
42-13 = ?
6+87 = ?
13- 4 = ?
20+77 = ?
18-11 = ?
96-86 = ?
Press any key to continue

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int i,j,tmp,A1,B1;
srand((unsigned)time(NULL));
for (i=0;i<10;i++)
{
A1= rand()%99+1;
B1= rand()%99+1;
A1>B1?printf("%2d-%2d = ?\n",A1,B1):printf("%2d+%2d = ?\n",A1,B1);
}
}
相似回答