zoukankan      html  css  js  c++  java
  • 四则运算三(接受用户输入答案,并判断对错。)

    一、要求:

        1.让程序能接受用户输入答案,并判定对错。最后给出总共对/错 的数量。

    二、思路: 

       定义变量right和wrong并初始化为0,每出一道题就答一道题,如果正确right++,如果错误wrong++,最后输出right和wrong的值。

    三、代码:

      1 #include<iostream>
      2 #include<stdlib.h>
      3 #include<time.h>
      4 using namespace std;
      5  
      6 
      7 char get_oper(int a)
      8 {
      9       switch(a)
     10       {
     11       case 0:return '+';
     12       case 1:return '-';
     13       case 2:return '*';
     14       case 3:return '/';
     15       }
     16 }
     17 int main()
     18 
     19 {
     20       srand(time(NULL));
     21       int num1=0;
     22       int num2=0;
     23       int operators=0;
     24       int cpl;          //定义每行输出题目个数
     25       int range;           //定义数值范围
     26       int Tanswer;        //定义计算结果
     27       int num;        //定义题目个数
     28       int JG;                //定义每行间隔
     29       char m;              //是否有乘除法
     30       char n;               //加减有无负数
     31       int answer;
     32       int wrang=0;
     33       int right=0;
     34       cout<<"请输入输出题目个数:";
     35       cin>>num;
     36       cout<<"请输入数值范围:";
     37       cin>>range;
     38       cout<<"请输入每行题目个数:";
     39       cin>>cpl;
     40       cout<<"请输入每行间隔:";
     41       cin>>JG;
     42       cout<<"是否有乘除法(Y/N)?";
     43       cin>>m;
     44       cout<<"是否有负数(Y/N)?";
     45       cin>>n;
     46       for(int i=1;i<=num;i++)
     47       {                                       //随机产生运算的数和运算符。
     48            num1=rand()%range;
     49            num2=rand()%range;
     50            if(m=='Y')
     51            {
     52                  operators=rand()%4;
     53                  switch(operators)
     54                  {
     55                       case 0:Tanswer=num1+num2;break;
     56                       case 1:Tanswer=num1-num2;break;
     57                       case 2:Tanswer=num1*num2;break;
     58                       case 3:Tanswer=num1/num2;
     59                  }
     60            }
     61            else if(m=='N')
     62            {
     63                  operators=rand()%2;
     64                  switch(operators)
     65                  {
     66                       case 0:Tanswer=num1+num2;break;
     67                       case 1:Tanswer=num1-num2;break;
     68                  }
     69            }
     70            if(answer<range)
     71            {         
     72                  if(n=='Y')
     73                  {
     74                       cout<<num1<<get_oper(operators)<<num2<<"=";
     75                      
     76                       cin>>answer;
     77                       if(answer==Tanswer)
     78                       {
     79                           cout<<"";
     80                           right++;
     81                       }
     82                       else
     83                       {
     84                           cout<<"";
     85                           wrang++;
     86                       }
     87                        for(int j=0;j<=JG;j++)
     88                       { 
     89                                cout<<" ";
     90                       }
     91                       cout<<"	";
     92                       if(i%cpl==0)
     93                       {
     94                       cout<<endl;
     95                       }
     96                  }
     97                  else if(n=='N')
     98                  {
     99                       i--;
    100                  }
    101            }
    102            else
    103            {
    104                  i--;
    105            }   
    106       }
    107       cout<<"作对数量:"<<right<<"错题数量:"<<wrang;
    108       return 0;
    109 }

    、运行截图:

    五、实验感想:

      通过对本次程序的编写,让我认识到在程序里边添加哪怕是再少的东西都会使程序无法达到预期的效果,猜想可能是跟软件维护会使软件的性能下降差不多一个道理,让我明白要想使程序在以后的修整过程中避免出现这种情况,要在最开始编写代码时尽最大努力使程序规范,修整时避免损坏程序结构。

  • 相关阅读:
    基于 BP 神经网络的识别手写体数字
    【science封面文章】Human-level concept learning through probabilistic program induction
    漫谈小样本的类人概念学习与大数据的深度强化学习
    Setting up a Deep Learning Machine from Scratch (Software)
    Building Apache Thrift on CentOS 6.5¶
    ---Ubuntu 14.04下配置caffe---
    markdown基本语法说明
    Andrew ng清华报告听后感
    Median of Two Sorted Arrays
    LeetCode Question Difficulty Distribution
  • 原文地址:https://www.cnblogs.com/cuipengbo/p/4369302.html
Copyright © 2011-2022 走看看