条件判断————11.整数加减法练习

要求说明:练习者自己选择是进行加法还是减法运算,之后输入进行多少以内的加法或减法运算,具体数值会由计算机随机产生,输入答案,计算机会根据输入的数据判断是否正确。

//整数加减法练习
#include <stdio.h>


int main(int argc, char **argv)
{   
    int a,b,c,sign,max;
    char sign1;
    printf("please select sign(1 or other,1:-,other:+):\n");  //输入数据赋值给sign,
    scanf("%d",&sign);
    
    printf("please select the max number(<10000):\n");        //输入数据赋值给max    
    scanf("%d",&max);
    srand((unsigned long)time(0)); //系统时间设定种子
    a = rand()%max; /产生小于max的随机数赋值给a
    b = rand()%max; //产生小于max的随机数赋值给b


    while((a<b)&&(sign == 1)) //选择减法运算时如果a小于b,则重新产生随机数
    {
        a = rand()%max;
        b = rand()%max;
    }


    sign1 = (sign==1?'-':'+'); //将选择的运算符赋给sign1
    printf("\n%d%c%d=",a,sign1,b);
    scanf("%d",&c); //输入计算结果
    if((sign == 1)&& (a-b == c) || (sign != 1)&& (a+b ==c)) //判断计算结果是否等于正确答案
    {
        printf("OK!\n"); //正确输出ok
    }
    else 
    {
        printf("the result is wrong!\n"); //输出错误
    }
    return 0;

}

样例输入输出:

please select sign(1 or other,1:-,other:+):
1
please select the max number(<10000):
998


955-928=27
OK!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值