C++代码记录

已实现功能:

1、程序每次运行时,会一次输出10道简单的加减法练习题。每输出1道题,等待用户输入其计算答案。题目难度限制在百以内的加减法,便于练习。
2、使用循环来实现。每轮循环输出随机生成的问题,如:“2 + 3 = "。接收用户的输入后,使用if语句检查其正确性。并将检查结果存入相关变量便于循环后统计输出。
3、使用头文件cstdlib中的rand函数实现随机生成数字,并将随机生成结果取模,以限制其范围。注意,需使用srand函数在程序开始前传入随机数种子,以确保每次运行均为不同的随机数。
4、在10道题目全部解答完毕后,程序会给出用户答对与答错的题目个数。
5、制定相应赋分规则,向用户输出答题的最终得分。

未实现:

1、在10道题目全部解答完毕后,给出答错的题目与其正确答案。
2、在答题程序开始前,可以通过简单的输入输出交互,让用户确定题目的数量、难度(运算的大小范围及是否设计乘除法)。
3、通过查阅C++相关功能,实现统计用户回答每一道题的用时,并向用户输出答题平均用时与最短用时。

#include<iostream>
#include <ctime>
#include <cstdlib>
#include <string>


using namespace std;
class Student
{
public:
	Student() {}
	void expression();//产生随机算术式
	
};
static int num, num1 = 0, a, b, d, c, sum, result;
void Student::expression()
{
	std::cout << "\n\n\n\t\t\t学生算数10道\n\n\n\n";
	for (int i = 0; i < 10; i++)
	{
		std::cout << "第" << i + 1 << "题\n";
		c = rand() % 2 + 1;
		switch (c)
		{
		case 1:
		{
			srand(time(NULL));
			a = rand() % 100 + 1;
			b = rand() % 100 + 1;
			d = a + b;
			std::cout << a << " + " << b << " = ";
		}
		break;
		case 2:
		{
			srand(time(NULL));
			a = rand() % 100 + 1;
			b = rand() % 100 + 1;
			if (a < b)
			{
				d = a;
				a = b;
				b = d;
			}
			d = a - b;
			std::cout << a << " - " << b << " = ";
		}
		break;
		}
		cin >> result;
		if (result != d)
		{
			cout << "error!\n";
			
		}
		else
		{
			cout << "right!\n";
			num++;
		}

}

	std::cout << "共答对 " << num << " 道题, 共答错, " << num1 << " 用户得分 " << num*10 ;
}


int main()
{
	Student stu;
	cout << "\t\t^^*^^*^^*^^*^^欢迎进入学生算数系统^^*^^*^^*^^*^^*^^\n\n\n\n";
	cout << "^^^^^^^^^^^^^^确定测试y/Y,我还没准备好n/N^^^^^^^^^^^^^^^^^\n";
	char ch;
	cin >> ch;
	if (ch == 'N' || ch == 'n')
		return 0;
	if (ch == 'Y' || ch == 'y')
	{
		stu.expression();
		
	}
	return 0;
}

在这里插入图片描述
改进:1、在10道题目全部解答完毕后,给出答错的题目与其正确答案。

目前情况:未改进成功,最后输出错误题目及其正确答案时,只会输出一个之前第一个计算错误的题目以及相应的答案。同时更BUG的是,代码重新运行时,我们再随机做10道题,最后打印出来的错题仍然时最开始运行代码后所保留的拿到错题,并不是当前训练时所做错的错题。

#include<iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include<fstream>
using namespace std;


class Student
{
public:
	Student() {}
	void expression();
	void again();
	
};
static int num, num1 = 0, a, b, d, c, sum, result;
void Student::expression()
{
	cout << "\n\n\n\t\t\t学生算数10道\n\n\n\n";
	for (int i = 0; i < 10; i++)
	{
		cout << "第" << i + 1 << "题\n";
		c = rand() % 2 + 1;
		switch (c)
		{
		case 1:
		{
			srand(time(NULL));
			a = rand() % 100 + 1;
			b = rand() % 100 + 1;
			d = a + b;
			cout << a << " + " << b << " = ";
		}
		break;
		case 2:
		{
			srand(time(NULL));
			a = rand() % 100 + 1;
			b = rand() % 100 + 1;
			if (a < b)
			{
				d = a;
				a = b;
				b = d;
			}
			d = a - b;
			cout << a << " - " << b << " = ";
		}
		break;
		
		}
		cin >> result;
		if (result != d)
		{
			cout << "error!\n";
			

			ofstream outfile("错题集.txt", ios::app);
			if (!outfile)
			{
				cerr << "open error!" << endl;
				exit(1);
			}
			outfile << a;
			switch (c)
			{
			case 1:outfile << "+"; break;
			case 2:outfile << "-"; break;
			}
			outfile << b;
			outfile << endl;
			outfile << d;
			outfile << endl;
			outfile.close();
		}
		else
		{
			cout << "right!\n";
			num++;
		}
	}
	cout << "\t\t\t共答对 " << num << " 道题\n";
}
void Student::again()
{
	char ch, c;
	cout << "\t\t\t是否查看错题(按y/Y继续,任意键结束)\n";
	cin >> ch;
	if (num != 10)
	{

		if (ch == 'Y' || ch == 'y')
		{
			ifstream infile("错题集.txt", ios::in);
			while (!(infile >> a >> c >> b >> d).eof())
			{
				cout << a << c << b << " = " << d;

				cout << "\t\t\t测试结束\n";
				infile.close();
			}
		}
		else
			cout << "\t\t\t测试完成\n";
	}
}

int main(){
	Student stu;
	cout << "\t\t^^*^^*^^*^^*^^欢迎进入学生算数系统^^*^^*^^*^^*^^*^^\n\n\n\n";
	cout << "^^^^^^^^^^^^^^首次测试选择y/Y,测试过选择n/N^^^^^^^^^^^^^^^^^\n";
	char ch;
	cin >> ch;
	if (ch == 'N' || ch == 'n')
		stu.again();
	if (ch == 'Y' || ch == 'y')
	{
		stu.expression();
		stu.again();
	}
	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cashapxxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值