zoukankan      html  css  js  c++  java
  • 作业1,四则运算器

    这个程序我在百度上面找了源代码,网址是https://zhidao.baidu.com/question/646917167186581165.html,源代码:

    #include<stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <conio.h>
    #include <time.h>
    int main()
    {
    int a = 0;
    int b = 0;
    int mode = 0;//0:加 1:减 2:乘 3:除
    int c = 0;
    int result = 0;
    int score = 0;
    int i = 0;
    srand((unsigned)time( NULL ) ); //初始化随机数发生器,使得每次运行生成的随机数不同
    for(i=0;i<10;i++) //做十题
    {
    a = rand() % 10; //生成一个0~9之间的随机数
    b = rand() % 10; //生成一个0~9之间的随机数
    mode = rand() % 4; //生成一个0~3之间的随机数,代表运算符
    printf("%d", a); //打印算式
    switch(mode) //确定运算符
    {
    case 0:
    printf("+ ");
    result= a + b; //选择了+运算的正确答案
    break;
    case 1:
    printf("- ");
    result= a - b; //选择了-运算的正确答案
    break;
    case 2:
    printf("* ");
    result= a * b; //选择了*运算的正确答案
    break;
    case 3:
    printf("/ ");
    result= a / b; //选择了/运算的正确答案
    break;
    default:
    printf("somethingis wrong! ");
    break;
    }
    printf("%d = ", b);
    scanf("%d", &c); //输入答案
    if(c == result) //与正确答案一致
    {
    score+= 10; //加分
    printf("Right ");
    }
    else
    {
    printf("Wrong "); //错开
    }
    }
    printf("Yourscore is: %d ", score);//显示十道题的得分
    return 0;
    }

    百度上的代码并没有加入<stdio.h>的头文件,所以编译会出错,这里添加了以后就能正常使用了。

  • 相关阅读:
    fastjson对String、JSONObject、JSONArray相互转换
    查看各进程分别占用多少服务器内存
    如何关闭或删除阿里云云盾安骑士
    docker 镜像操作
    docker 容器命令
    docker换源
    centos8 docker安装
    基本概念
    自动生成文件注释和函数注释
    Pycharm新建文件时自动添加基础信息
  • 原文地址:https://www.cnblogs.com/tzk971118/p/7613071.html
Copyright © 2011-2022 走看看