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>的头文件,所以编译会出错,这里添加了以后就能正常使用了。

  • 相关阅读:
    【笔记】DLHLP
    【笔记】DLHLP
    【笔记】DLHLP
    【笔记】DLHLP
    【笔记】PyTorch框架学习 -- 2. 计算图、autograd以及逻辑回归的实现
    【笔记】PyTorch框架学习 -- 1. 张量创建及操作,线性回归的实现
    【安装指南】pytorch相关软件安装
    【笔记】机器学习
    【笔记】机器学习
    【笔记】机器学习
  • 原文地址:https://www.cnblogs.com/tzk971118/p/7613071.html
Copyright © 2011-2022 走看看