个人项目3

一、要求

完成一个能够随机生成任意道题目,并能统计成功率的四则运算程序。

二、源代码

// 312.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
 return 0;
}
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
void erzhuzi()
{
 srand((unsigned int)time(NULL));
 int z;
 int first_num, second_num;
 int n, i;
 float k;
 float d;
 int daan[100];
 int T = 0;
 int F = 0;
 cout << "请输入想要作答的题数:" << endl;
 cin >> n;
 first_num = rand() % 10;
 second_num = rand() % 10;
 for (int i = 0; i<n; i++)
 {                                        
  z = rand() % 4;
  first_num = rand() % 10;
  second_num = rand() % 10;
  z = rand() % 4;
  switch (z)
  {
  case 0:
   cout << i + 1 << ":" << first_num << "+" << second_num << "=  " << " ";
   daan[i + 1] = first_num + second_num;
   break;
  case 1:
   cout << i + 1 << ":" << first_num << "-" << second_num << "=  " << " ";
   daan[i + 1] = first_num - second_num;
   break;
  case 2:
   cout << i + 1 << ":" << first_num << "*" << second_num << "=  " << " ";
   daan[i + 1] = first_num*second_num;
   break;
  case 3:
   if (first_num != 0)
   {
    cout << i + 1 << ":" << first_num << "/" << second_num << "=  " << " ";
   }
   else
   {
    i = i - 1;
   }
   daan[i + 1] = first_num / second_num;
   break;
  }
 }
 for (i = 1; i <= n; i++)
 {
  cout << "你的答案:" << endl;
  cout << i << "、";
  cin >> d;
  if (d == daan[i])
  {
   cout << "正确!" << endl;
   cout << endl;
   T = T + 1;
  }
  else
  {
   cout << "答错了!" << endl;
   cout << endl;
   F = F + 1;
  }
 }
 k = (float)T / n;
 cout << "答对的数量:" << " " << T << "个" << endl;
 cout << "答错的数量:" << " " << F << "个" << endl;
 cout << "正确答案为:" << endl;
 for (i = 1; i <= n; i++)
 {
  cout << i << "、" << daan[i] << endl;
 }
 cout << "正确率为:" << " " << k << endl;
}
int main()
{
 erzhuzi();
 return 0;
}
三、运行结果

 

posted @ 2018-11-01 20:28  热心市民隔壁老王  阅读(99)  评论(0编辑  收藏  举报