C语言 让计算机在100以内数字随机产生两个进行随机四则运算

如题

    产生100为模的两个数字,记录下来;

    生成一个以4为模的数字,1对应+,2对应-。。。。

    计算。


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

    void main()
    {
    int i, j, calc, r;
    srand(time(NULL));
    i = rand()%100;
    j = rand()%100;

    calc = rand()%4;
    printf("i:%d,j:%d,c:%d\n", i,j,calc);
    }

    后面使用switch-case计算就行了
追问

求完整程序……

追答#include<stdio.h>
#include <time.h>

void main()
{
int i, j, calc;
double r;
srand(time(NULL));
i = rand()%100;
j = rand()%100;

calc = rand()%4;
printf("i:%d,j:%d,c:%d\n", i,j,calc);
switch (calc)
{
case 0:
r = i+j;
break;
case 1:
r = i-j;
break;
case 2:
r = i*j;
break;
case 3:
if (j==0)
{
printf("j=0,不能执行除法\n");
return ;
}
r = j/j;
}
printf("r = %.2f\n", r);
}

追问

I:\CCC\cc.cpp(8) : error C2065: 'srand' : undeclared identifier
I:\CCC\cc.cpp(9) : error C2065: 'rand' : undeclared identifier

追答

我使用的.c文件,你试试呢?

#include <stdio.h>
#include <time.h>
 
void main()
{
    int i, j, calc;
    double r;
    srand(time(NULL));
    i = rand()%100;
    j = rand()%100;
 
    calc = rand()%4;
    printf("i:%d,j:%d,c:%d\n", i,j,calc);
    switch (calc)
    {
    case 0:
        r = i+j;
        break;
    case 1:
        r = i-j;
        break;
    case 2:
        r = i*j;
        break;
    case 3:
        if (j==0)
        {
            printf("j=0,不能执行除法\n");
            return ;
        }
        r = i/j;
    }
    printf("r = %.2f\n", r);
}

追问

如果加上让人输入结果,让计算机判断正误的环节,又该怎么做呢?
计算机输出 两个数字的运算公式,让人输入结果。计算机再进行判断 正误

追答

switch之后的代码改一下:
在main函数开头定义一个double的rin

switch (calc)
{
case 0:
r = i+j;
printf("%d+%d=?\n", i, j);

break;
case 1:
r = i-j;
printf("%d-%d=?\n", i, j);
break;
case 2:
r = i*j;
printf("%d*%d=?\n", i, j);
break;
case 3:
if (j==0)
{
printf("j=0,不能执行除法\n");
return ;
}
r = i/j;
printf("%d/%d=?\n", i, j);
}
scanf("%f", &rin);
if(r == rin)

printf(right\n");
else
printf("wrong\n");

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-14
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>

int main(){
int a,b,c,d;
srand(time(0));
a=rand()%100;
b=rand()%100;
c=rand()%4;
switch(c){
case 0:printf("%d+%d=",a,b);break;
case 1:printf("%d-%d=",a,b);break;
case 2:printf("%d*%d=",a,b);break;
case 3:if(b==0){

while(b==0){
a=rand()%100;
b=rand()%100;
printf("%d/%d=",a,b);
}
}
else
printf("%d/%d=",a,b);
break;
}
scanf("%d",&d);
switch(c){
case 0:if(d==a+b)
printf("You are right");
else
printf("You are wrong");
break;
case 1:if(d==a-b)
printf("You are right");
else
printf("You are wrong");
break;
case 2:if(d==a*b)
printf("You are right");
else
printf("You are wrong");
break;
case 3:if(d==a/b)
printf("You are right");
else
printf("You are wrong");
break;

}
return 0;

}