zoukankan      html  css  js  c++  java
  • 四则运算升级版

      关于四则运算的问题,我的第一条博客已经有所解决;而现在,我要解决在我这个界面内,可以做到即时答题,并且可以即时得出对错。

    并将错误的题目记录下来,我通过写文件的方法保存了错题。

      在线答题的解决方法:当随机产生了一个题目之后,便设置输入变量值,从而达到在线答题的效果,与此同时,若答错了,则将此题目写入文件中。

    具体代码如下:

     1 #include<iostream>
     2 #include<fstream>
     3 #include<time.h>
     4 #include<stdlib.h> //调用随机函数;
     5 using namespace std;
     6 int main()
     7 {
     8     srand((unsigned)time(NULL));//使随机产生的题目不重复;
     9     int x,y,n;//x,y为随机数,n为题目数量;
    10     int ch;
    11     cout<<"输入要做的题目数量:";
    12     cin>>n;
    13     float a,b;
    14     ofstream ofile;               //定义输出文件。
    15     ofile.open("E:\信息作业\软件工程概论\错题本.txt");     //作为输出文件打开。
    16     ofile<<"错题:"<<endl;   //标题写入文件。
    17     for(int i=1;i<=n;i++)
    18     {
    19         x=rand()%100;y=rand()%100;
    20         ch=rand()%4;
    21         switch(ch)
    22         {
    23         case 0:cout<<x<<"+"<<y<<"="<<" ";b=x+y;cin>>a;
    24             if(a==b)
    25                 cout<<""<<endl;
    26             else
    27             {
    28                 cout<<"×"<<endl;
    29              ofile<<x<<"+"<<y<<"="<<" "<<a<<endl;
    30             }
    31             break;
    32         case 1:cout<<x<<"-"<<y<<"="<<" ";b=x-y;cin>>a;
    33             if(a==b)
    34                 cout<<""<<endl;
    35             else
    36             {
    37                 cout<<"×"<<endl;
    38                 ofile<<x<<"-"<<y<<"="<<" "<<a<<endl;
    39             }
    40             break;
    41         case 2:cout<<x<<"*"<<y<<"="<<" ";b=x*y;cin>>a;
    42             if(a==b)
    43                 cout<<""<<endl;
    44             else
    45             {
    46                 cout<<"×"<<endl;
    47                 ofile<<x<<"*"<<y<<"="<<" "<<a<<endl;
    48             }
    49             break;
    50         case 3:
    51             while(y==0)
    52             {
    53                 y=rand()%100;
    54             }
    55             cout<<x<<"/"<<y<<"="<<" ";b=(float)x/y;cin>>a;
    56             if(a==b)
    57             cout<<""<<endl;
    58         else
    59         {
    60             cout<<"×"<<endl;
    61             ofile<<x<<"/"<<y<<"="<<" "<<a<<endl;
    62         }
    63         }
    64         //if(i%10==0)
    65             //cout<<endl;
    66     }
    67     ofile.close();
    68     system("pause");
    69     return 0;
    70 }

    测试结果如图:

  • 相关阅读:
    结对编程之附加题:单元测试
    机器学习第二次作业
    第一次作业
    机器学习第二次作业
    机器学习第一次个人作业
    软工实践个人总结
    第08组 Beta版本演示
    第08组 Beta冲刺(5/5)
    第08组 Beta冲刺(4/5)
    第08组 Beta冲刺(3/5)
  • 原文地址:https://www.cnblogs.com/stdu-412/p/9910906.html
Copyright © 2011-2022 走看看