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 }

    测试结果如图:

  • 相关阅读:
    [LeetCode]2. Add Two Numbers链表相加
    Integration between Dynamics 365 and Dynamics 365 Finance and Operation
    向视图列添加自定义图标和提示信息 -- PowerApps / Dynamics365
    Update the Power Apps portals solution
    Migrate portal configuration
    Use variable to setup related components visible
    Loyalty management on Retail of Dynamic 365
    Modern Fluent UI controls in Power Apps
    Change screen size and orientation of a canvas app in Power App
    Communication Plan for Power Platform
  • 原文地址:https://www.cnblogs.com/stdu-412/p/9910906.html
Copyright © 2011-2022 走看看