C++課程設計之四則運算測驗系統設計

隨機選擇兩個整數進行加法、減法、乘法運算,給出算式要求學生作答。基本要求如下:
1、電腦隨機出5道題,每題20分,程序結束時顯示學生得分;
2、隨機給出的數在1-20之間;
3、每道題學生有三次機會輸入答案,當學生輸入錯誤答案時,提醒學生重新輸入,如果三次機會結束則輸出正確答案,該題不計入學生得分;
4、對於每道題目,學生第一次輸入正確答案得20分,第二次輸入正確答案得15分,第三次輸入正確答案得10分,否則不得分;
5、5道題全部答完以後,電腦給出學生總分,並將學生得分爲0的題目以及相應的正確答案再次輸出;

#include<iostream>
#include <string>
#include<stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#define MAXN 20  //自定義長度,方便全局使用
using namespace std;//標準命名空間
char input[81]; /*輸入的答案,先以字符串形式保存,可以接收不正當輸入,會去判斷 */
int error[5];//記錄錯題
int r = 0;//記錄錯題數量
class arithmetic {//定義一個四則運算類
private:
	int score; /*得分 */
public:
	static int right;//記錄正確題數量
	void generateQuestion(int a[], char op[], int b[], int c[]);//隨機出題函數
	int getAnswer(int answer, int tnum);//計算結果函數
	static int putr()//返回正確結果數
	{
		return right;
	}
};
void arithmetic::generateQuestion(int a[], char op[], int b[], int c[])
{
	int i, temp;
	srand((unsigned)time(NULL)); //隨機數初始化種子
	for (i = 0; i < 5; i++)//出5道題目
	{
		temp = rand() % 3;//3種操作運算符隨機出現
		switch (temp) //+ - *  分別代表加減乘
		{
		case 0:
			op[i] = '+';
			a[i] = rand() % MAXN + 1; //隨機數a
			b[i] = rand() % MAXN + 1; //隨機數b
			c[i] = b[i] + a[i];
			break;
		case 1:
			op[i] = '-';
			a[i] = rand() % MAXN + 1; //被減數最小是
			b[i] = rand() % (a[i] - 1) + 1; //保證 b 比 a小,從而 a、b、c都是正整數
			c[i] = a[i] - b[i];
			break;
		case 2:
			op[i] = '*';
			c[i] = rand() % MAXN + 1; //積最小是
			//保證 a 比 c小,而且 a、b、c 都是正整數
			do {
				a[i] = rand() % (c[i] - 1) + 1;
			} while (c[i] % a[i] != 0);
			b[i] = c[i] / a[i];
		default:
			break;
		}
	}
	return;
}
int arithmetic::getAnswer(int answer, int tnum)
{
	int flagresult = 0; /*回答是否成功的標記,不等於1爲不成功,1爲成功 */
	int len; /*字符串長度,和下面的p變量配合檢查輸入是否合法數字 */
	int i, j; //j爲答題次數

	j = 0;
	do {
		j++;
		cin >> input;
		len = strlen(input);//返回input的長度   功能:函數返回字符串str 的長度( 即空值結束符之前字符數目)。  
		for (i = 0; i < len - 1; i++)
			if (!isdigit(input[i])) //如果參數是0到9之間的數字字符,函數返回非零值,否則返回零值
			{
				cout << "輸入不合法!" << endl;
				if (j < 3) {
					cout << "再試下: " << endl;
				}
				break;
			}
		if (i == len - 1)
		{
			if (answer == atoi(input))//功能:將字符串str轉換成一個整數並返回結果。參數str 以數字開頭,當函數從str 中讀到非數字字符則結束轉換並將結果返回。
			{
				flagresult = 1;
				cout << "答對了!" << endl;
				right++;//回答正確,正確題目數量自增
				break;
			}
			else if (j < 3) {
				cout << "答案不對,再試下: " << endl;
			}

		}
	} while (j < 3);
	/*對於每道題目,學生第一次輸入正確答案得20分,第二次輸入正確答案得15分,第三次輸入正確答案得10分,否則不得分*/
	if (flagresult != 1)
	{
		error[r++] = tnum;
		cout << "很遺憾,正確答案是: " << answer << endl;
		return 0;
	}
	else if (j == 1) {
		return 20;
	}
	else if (j == 2) {
		return 15;
	}
	else {
		return 10;
	}
}
int arithmetic::right = 0;//初始化靜態成員數據
int main() {
	arithmetic arit;
	int a[5], b[5], c[5];/*五道題目的第一個a和第二個操作數b以及答案c */
	char op[5]; /*五道題目的操作類型,+ - * 分別代表加減乘*/
	int score = 0, k;
	arit.generateQuestion(a, op, b, c); /*隨機出題*/
	cout << "\n一共5道題,每道題有3次機會輸入答案,\n第一次輸入正確答案得20分,第二次輸入正確答案得15分,第三次輸入正確答案得10分,否則不得分\n" << endl;
	cout << "現在開始出題:" << endl;
	for (int i = 0; i < 5; i++)
	{

		cout << "第 " << i + 1 << " 題" << a[i] << op[i] << b[i] << "=";
		score += arit.getAnswer(c[i], i); /*獲取答案輸入並評分*/
	}
	cout << "\n您的總分: " << score << endl;
	if (arit.putr() != 5)
	{
		cout << "錯誤的題和答案:" << endl;
		for (int j = 0; j < r; j++)
		{
			k = error[j];
			cout << "第" << k + 1 << "題: ";
			cout << a[k] << op[k] << b[k] << "=" << c[k] << endl;

		}
	}
	cout << "等級:";
	if (score > 90) {
		cout << "優秀" << endl;
	}
	else if (score > 80) {
		cout << "良好" << endl;
	}
	else if (score > 70)
	{
		cout << "一般" << endl;

	}
	else if (score > 60) {
		cout << "及格" << endl;
	}
	else {
		cout << "不及格" << endl;
	}

	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章