代码

ex4_17.cpp

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include "ex4_17.h"

using namespace std;

int initRecord();
void updateRecord(int);
int menu();
int test(Expression*, int, int ,int);
void display(Expression*, int);

int main(){
	Expression *p;
	int i, n, num, right, score, record;
	int choice;
	record  = initRecord();
	srand((unsigned)time(NULL));
	while((choice=menu()) != 6){
		cout << "请选择难度: " << endl;
		cout << "1. 一级难度(10以内的运算)" << endl;
		cout << "2. 一级难度(100以内的运算)" << endl;
		cin >> i;
		if(i == 1) n = 9;
		else n = 99;
		cout << "请输入测试题数量: " << endl;
		cin >> num;
		p = new Expression[num];
		right = test(p, num, n, choice);
		score = (double)right/num*100;
		cout << endl << "测试结束,共" << num << "题, 你答对了" << right << "题 " << score << "分" << endl;
		cout << "最高记录是 " << record << " 分" << endl;
		if(score > record) record = score;
		cout << "你要看所有的试题吗?(y/n)" << endl;
		char yon;
		cin >> yon;
		if(yon == 'y' || yon == 'Y') display(p, num);
		delete[] p;
	}
	updateRecord(record);

	system("pause");
	return 0;
}

display.cpp

#include<iostream>
#include "ex4_17.h"
using namespace std;

void display(Expression *p, int num){
	for(int i = 0; i < num; i++){
		cout << i + 1 << "." << p[i].x << p[i].ex_op << p[i].y << "= " << p[i].result << "\t";
		cout << "你的回答是:" << p[i].t << "\t";
		if(p[i].result == p[i].t) cout << "正确" ;
		else cout << "错误" ;
		cout << endl;
	}
}

test.cpp

#include<iostream>
#include "ex4_17.h"

using namespace std;
int test(Expression *p, int amount, int n, int op){
	int opl = op;
	int right_num = 0;
	for(int k = 0; k < amount; k++){
		p[k].x = 1 + rand()%n;
		p[k].y = 1 + rand()%n;
		if(op == 5) opl = 1 + rand()%4;
		switch(opl){
			case 1:p[k].ex_op = '+'; p[k].result = p[k].x + p[k].y; break;
			case 2:p[k].ex_op = '-'; p[k].result = p[k].x - p[k].y; break;
			case 3:p[k].ex_op = '*'; p[k].result = p[k].x * p[k].y; break;
			case 4:p[k].ex_op = '/'; p[k].result = p[k].x / p[k].y; break;
		}
		cout << k + 1 << ". " << p[k].x << p[k].ex_op << p[k].y << "= ";
		cin >> p[k].t;
		if(p[k].result == p[k].t){
			cout << "恭喜你,答对了" << endl;
			right_num++;
		}else{
			cout << "傻逼,答错了,正确答案是" << p[k].result << "老弟你不行" << endl;
		}
	}
	return right_num;
}
#include<iostream>
#include<fstream>

using namespace std;

int menu(){
	int i;
	cout << endl << endl;
	cout << "**************************************************" << endl;
	cout << "************欢迎进入小学生四则运算测试系统********" << endl;
	cout << "**************************************************" << endl;
	cout << "1.加法测试\t 2.减法测试\t 3.乘法测试\t 4.除法测试\t 5.混合测试\t 6.退出" << endl;
	cout << "请选择(1~6) " << endl;
	cin >> i;
	while(i < 1 || i > 6){
		cout << "错误的选择,请选择(1~6)" << endl;
		cin >> i;
	}

	return i;
}

record.cpp

#include<iostream>
#include<fstream>

using namespace std;

int initRecord(){
	int record = 0;
	ifstream ifile("record.txt");
	ofstream ofile;
	if(!ofile){
		ifile.close();
		ofile.open("record.txt");
		ofile << record;
		ofile.close();
	}else{
		ifile >> record;
		ifile.close();
	}
	return record;
}

void updateRecord(int record){
	ofstream ofile;
	ofile.open("record.txt");
	ofile << record;
	ofile.close();
}

ex4_17.h

#ifndef CHOICE_H
#define CHOICE_H
struct Expression{
	int x;
	int y;
	char ex_op;
	int t;
	int result;
};
#endif