四则运算第一阶段
Posted dongdongdongdong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算第一阶段相关的知识,希望对你有一定的参考价值。
源代码:
#include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int main() { float x, y; int a, b, i; srand(time(NULL)); float k; float d; float answer[10]; int zq = 0; int cw = 0; for (i = 0; i<10; i++) { x = rand() % 100; y = rand() % 100; a = rand() % 4; switch (a) { case 0: cout << i + 1 << ". " << x << "+" << y << " "; answer[i + 1] = x + y; break; case 1: cout << i + 1 << ". " << x << "-" << y << " "; answer[i + 1] = x - y; break; case 2: cout << i + 1 << ". " << x << "*" << y << " "; answer[i + 1] = x*y; break; case 3: if (y != 0) { cout << i + 1 << ". " << x << "÷" << y << " "; answer[i + 1] = (float)x / y; } else { i = i - 1; } break; } if ((i + 1) % 3 == 0) { for (int c = 0; c <= 1; c++) { cout << endl; } } if ((i + 1) == 10) { cout << endl; } } for (i = 1; i <= 10; i++) { cout << "请答题:" << endl; cout << i << ". "; cin >> d; if (d == answer[i]) { cout << "回答正确!!" << endl; cout << endl; zq = zq + 1; }
else { cout << "回答错误,正确答案为: " << answer[i] << endl; cout << endl; cw = cw + 1; } } k = (float)zq / 10; cout << "答对数为: " << zq << "个" << endl; cout << "答错数为: " << cw << "个" << endl; cout << "正确率为: " << k << endl; return 0; }
结果
以上是关于四则运算第一阶段的主要内容,如果未能解决你的问题,请参考以下文章