【课程设计|C++】C++程序设计基础:小学生数学练习系统
海轰Pro
共 42380字,需浏览 85分钟
· 2021-07-12
功能需求
程序需要实现的功能如下:
加法、减法、乘法、除法简易练习 加减混合练习 查看错题(txt文件存储) 自我检测 大整数加法 大整数减法
难度 🌟🌟
程序运行图
系统主界面
加法练习
用户输入需要练习题目的数量,程序随机生成算式
用户回答正确,得分+10
回答错误,该题存入错题本
减法练习
加减混合运算
乘法运算
除法运算
错题本查看
用户做错过的题均存在一个txt文件中
用户可以随时查看
并再做一遍
自测功能(难点)
用户使用该功能
可以自行输入一个多项式
如
1+2*3 2✖️3-3+5/4+2-3*3 1+2-3*4/5
程序会自动算出结果并与用户输入进行对比
(这个当时写的时候感觉还是有点难度)
大整数减法(难点)
实现两个任意整数的减法运算,不用担心int、double类型溢出
大整数加分(难点)
实现两个任意整数的加法运算,不用担心int、double类型溢出
源码
main.cpp
/*
writed by Haihong
time 2018
*/
#include "student.h"
#include "Stack.h"
#include <iostream>
#include <ctime>
#include <sstream>
#include <string>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
//判断 符号优先级
char Priority(char ch1, char ch2)
{
int a;
int b;
switch (ch1)
{
case '#':
a = 0;
break;
case '(':
a = 1;
break;
case '+':
a = 3;
break;
case '-':
a = 3;
break;
case '*':
a = 5;
break;
case '/':
a = 5;
break;
case '%':
a = 5;
break;
case '^':
a = 7;
break;
case ')':
a = 8;
break;
}
switch (ch2)
{
case '#':
b = 0;
break;
case '(':
b = 8;
break;
case '+':
b = 2;
break;
case '-':
b = 2;
break;
case '*':
b = 4;
break;
case '/':
b = 4;
break;
case '%':
b = 4;
break;
case '^':
b = 6;
break;
case ')':
b = 1;
break;
}
if (a < b)
return '<';
else if (a == b)
return '=';
else
return '>';
}
//计算
int Compute(int a, int b, char sign)
{
int result;
switch (sign)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
case '%':
result = a % b;
break;
case '^':
result = a ^ b;
break;
}
return result;
}
int main()
{
int key = 1;
int key1 = 1;
Student stu;
string str;
int choice;
int choice1;
int num;
while (key == 1)
{
cout << "**********************************************" << endl;
cout << "* 欢迎来到小学生加法系统 *" << endl;
cout << "* 1.加法 *" << endl;
cout << "* 2.减法 *" << endl;
cout << "* 3.加减混合 *" << endl;
cout << "* 4.乘法练习 *" << endl;
cout << "* 5.除法练习 *" << endl;
cout << "* 6.查看错题 *" << endl;
cout << "* 7.自我检测 *" << endl;
cout << "* 8.大整数减法 *" << endl;
cout << "* 9.大整数加法 *" << endl;
cout << "**********************************************" << endl;
cout << " 请输入你的选择: (1--9)";
cin >> choice;
while (cin.fail() || choice < 1 || choice > 10)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear(); //清除流标记
cin.sync(); //清空流
cin >> choice;
}
switch (choice)
{
case 1:
cout << " 输入练习题目的数量:";
cin >> num;
while (cin.fail() || num < 0)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> num;
}
stu.setnums(num);
stu.jia();
break;
case 2:
cout << " 输入练习题目的数量:";
cin >> num;
while (cin.fail() || num < 0)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> num;
}
stu.setnums(num);
stu.jian();
break;
case 3:
cout << " 输入练习题目的数量:";
cin >> num;
while (cin.fail() || num < 0)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> num;
}
stu.setnums(num);
stu.hun();
break;
case 4:
cout << " 输入练习题目的数量:";
cin >> num;
while (cin.fail() || num < 0)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> num;
}
stu.setnums(num);
stu.cheng();
break;
case 5:
cout << " 输入练习题目的数量:";
cin >> num;
while (cin.fail() || num < 0)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> num;
}
stu.setnums(num);
stu.chu();
break;
case 6:
stu.again();
break;
case 7:
int n;
cout << "请输入你需要进行自测题目的数量:";
cin >> n;
while (cin.fail() || n < 0)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> n;
}
int i;
for (i = 0; i < n; i++)
{
int ch1 = getchar();
Stack<int> number; //数字栈
Stack<char> character; //运算符栈
character.Push('=');
cout << "请输入你的算术式:(例:1+(2*3)=)(括号须为英文字符)()(以 = 结束)" << endl;
int ch = getchar();
char b;
b = static_cast<char>(ch);
while (b != '=' || character.getTop() != '=')
{
if (ch > 48 && ch <= 57)
{
number.Push(ch - 48);
ch = getchar();
b = static_cast<char>(ch);
}
else
{
switch (Priority(character.getTop(), b))
{
case '<': //栈顶元素优先级低
character.Push(b);
ch = getchar();
b = static_cast<char>(ch);
break;
case '=':
character.Pop();
ch = getchar();
b = static_cast<char>(ch);
break;
case '>': //出栈并且把运算结果入栈
number.Push(Compute(number.Pop(), number.Pop(), character.Pop()));
break;
}
}
}
int anwser;
cout << "请输入你的答案:";
cin >> anwser;
if (anwser == number.getTop())
cout << "正确!" << endl;
else
cout << "错误!" << endl;
}
break;
case 8:
//string numss1;
//string numss2;
// string aaaaaaaaaaaa;
{
string nums1;
string nums2;
// kkk;//答案
cout << " 输入练习题目的数量:";
cin >> num;
while (cin.fail() || num < 0)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> num;
}
stu.setnums(num);
cout << "请输入第一个数:";
cin >> nums1;
cout << "请输入第二个数:";
cin >> nums2;
//kkk=stu.sub(nums1,nums2);//>>nums2;
//cout<<"1请输入你的答案:";
//cin>>kkk;
cout << "答案是:" << endl;
cout << stu.sub(nums1, nums2) << endl;
// string a;
}
break;
case 9:
string nums1;
string nums2;
cout << " 输入练习题目的数量:";
cin >> num;
while (cin.fail() || num < 0)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> num;
}
stu.setnums(num);
cout << "请输入第一个数:";
cin >> nums1;
cout << "请输入第二个数:";
cin >> nums2; //>>nums2;
cout << "答案是:" << endl;
cout << stu.plus(nums1, nums2) << endl;
break;
}
cout << "是否继续?1:继续 2:退出" << endl;
cin >> choice1;
while (cin.fail() || choice1 < 1 || choice1 > 2)
{
cout << "输入错误!请重新输入:" << endl;
cin.clear();
cin.sync();
cin >> choice1;
}
if (choice1 == 1)
key = 1;
else
key = 0;
system("cls");
system("pause");
}
return 0;
}
Stack.h
//...................................................................................节点类
#ifndef STACK_H
#define STACK_H
template <typename T>
class Node
{
public:
T element;
Node *next;
Node(T element)
{
this->element = element;
next = NULL;
}
};
template <typename T>
class Stack
{
private:
Node<T> *top;
int size;
public:
Stack()
{
Node<T> *newNode = new Node<T>(0);
top = newNode;
size = 0;
}
int stackLength()
{
return size;
}
//判栈为空
bool stackEmpty()
{
if (0 == size)
return true;
else
return false;
}
//入栈
void Push(T e)
{
Node<T> *newNode = new Node<T>(e);
newNode->next = top->next;
top->next = newNode;
size++;
}
//出栈
T Pop()
{
Node<T> *current = top->next;
top->next = current->next;
T f = current->element;
delete current;
size--;
return f;
}
//返回栈顶元素
T getTop()
{
return top->next->element;
}
};
#endif
student.h
#ifndef _student_h_
#define _student_h_
#include <iostream>
#include <ctime>
#include <sstream>
#include <string>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
class Student
{
public:
Student() {}
void jia();
void jian();
void setnums(int a)
{
nums = a;
}
void hun();
void again();
void cheng();
void chu();
string plus(string num1, string num2);
string sub(string num1, string num2);
private:
int nums;
};
static int num = 0, a, b, d, c, sum, result, aa, cc;
void Student::hun()
{
cout << endl;
cout << endl;
cout << " 混合运算开始!" << endl;
for (int i = 0; i < nums; i++)
{
char str1;
char str2;
cout << "第" << i + 1 << "题\n";
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
aa = rand() % 100 + 1;
d = (a % 2 == 1 ? a + b : a - b);
d = (b % 2 == 1 ? d + aa : d - aa);
str1 = (a % 2 == 1 ? '+' : '-');
str2 = (b % 2 == 1 ? '+' : '-');
cout << a << str1 << b << str2 << aa << " = ";
cin >> result;
if (result != d)
{
cout << "error!\n";
ofstream outfile("错题.txt", ios::app);
if (!outfile)
{
cerr << "open error!" << endl;
exit(1);
}
outfile << a << str1 << b << str2 << aa << "=" << endl;
outfile.close();
}
else
{
cout << "right!\n";
num++;
}
}
cout << "\t\t\t你一共答对 " << num << " 道题\n";
cout << "\t\t\t你的分数是:" << num * 10 << endl;
}
void Student::jia()
{
cout << endl;
cout << endl;
cout << " 加法练习开始!" << endl;
for (int i = 0; i < nums; i++)
{
cout << "第" << i + 1 << "题\n";
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
d = a + b;
cout << a << " + " << b << " = ";
cin >> result;
if (result != d)
{
cout << "错误!\n";
cout << "正确答案是:" << d << endl;
ofstream outfile("错题.txt", ios::app);
if (!outfile)
{
cerr << "open error!" << endl;
exit(1);
}
outfile << a << "+" << b << "=" << endl;
outfile.close();
}
else
{
cout << "正确!\n";
num++;
}
}
cout << "\t\t\t共答对 " << num << " 道题\n";
cout << "\t\t\t你的分数是:" << num * 10 << endl;
}
void Student::jian()
{
cout << endl;
cout << endl;
cout << " 减法练习开始!" << endl;
for (int i = 0; i < nums; i++)
{
cout << "第" << i + 1 << "题\n";
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
d = a - b;
cout << a << " - " << b << " = ";
cin >> result;
if (result != d)
{
cout << "错误!\n";
cout << "正确答案是:" << d << endl;
ofstream outfile("错题.txt", ios::app);
if (!outfile)
{
cerr << "open error!" << endl;
exit(1);
}
outfile << a << "-" << b << "=" << endl;
outfile.close();
}
else
{
cout << "正确!\n";
num++;
}
}
cout << "\t\t\t共答对 " << num << " 道题\n";
cout << "\t\t\t你的分数是:" << num * 10 << endl;
}
void Student::again()
{
string str;
cout << endl;
cout << endl;
cout << " 你的错题是:(展示的是正确答案)" << endl;
ifstream infile("错题.txt", ios::in);
if (infile)
{
stringstream buf;
buf << infile.rdbuf();
str = buf.str();
cout << str;
// cout<<a<<endl;
}
else
{
cout << "错误 不能打开文件!" << endl;
}
infile.close();
}
void Student::cheng()
{
cout << endl;
cout << endl;
cout << " 乘法练习开始!" << endl;
for (int i = 0; i < nums; i++)
{
cout << "第" << i + 1 << "题\n";
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
d = a * b;
cout << a << " *" << b << " = ";
cin >> result;
if (result != d)
{
cout << "错误!\n";
cout << "正确答案是:" << d << endl;
ofstream outfile("错题.txt", ios::app);
if (!outfile)
{
cerr << "open error!" << endl;
exit(1);
}
outfile << a << "*" << b << "=" << endl;
outfile.close();
}
else
{
cout << "正确!\n";
num++;
}
}
cout << "\t\t\t共答对 " << num << " 道题\n";
cout << "\t\t\t你的分数是:" << num * 10 << endl;
}
void Student::chu()
{
cout << endl;
cout << endl;
cout << " 除法练习开始!" << endl;
for (int i = 0; i < nums; i++)
{
cout << "第" << i + 1 << "题\n";
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
d = a / b;
cout << a << " / " << b << " = ";
cin >> result;
if (result != d)
{
cout << "错误!\n";
cout << "正确答案是:" << d << endl;
ofstream outfile("错题.txt", ios::app);
if (!outfile)
{
cerr << "open error!" << endl;
exit(1);
}
outfile << a << "/" << b << "=" << d << endl;
outfile.close();
}
else
{
cout << "正确!\n";
num++;
}
}
cout << "\t\t\t共答对 " << num << " 道题\n";
cout << "\t\t\t你的分数是:" << num * 10 << endl;
}
string Student::plus(string num1, string num2)
{
if (num1.size() < num2.size())
{
string temp = num1;
num1 = num2;
num2 = temp;
}
int length1 = num1.size(), length2 = num2.size(), flag = 0, a, b, sum; //flag是进位标记
while (length1 > 0)
{
a = num1[length1 - 1] - '0';
if (length2 > 0)
b = num2[length2 - 1] - '0';
else
b = 0;
sum = a + b + flag;
if (sum >= 10)
{
num1[length1 - 1] = '0' + sum % 10;
flag = 1;
}
else
{
num1[length1 - 1] = '0' + sum;
flag = 0;
}
length1--; //向高位移动1位
length2--; //向高位移动1位
}
if (1 == flag)
num1 = "1" + num1;
return num1;
}
string Student::sub(string num1, string num2)
{
string num11;
int k = 0;
if (num1.size() < num2.size() || num1 < num2)
{
string temp = num1;
num1 = num2;
num2 = temp;
k = 1;
}
int length1 = num1.size(), length2 = num2.size(), flag = 0, a, b, s;
while (length1 > 0)
{
a = num1[length1 - 1] - '0';
if (length2 > 0)
b = num2[length2 - 1] - '0';
else
b = 0;
s = a - b - flag;
if (s < 0)
{
num1[length1 - 1] = 10 + s + '0';
flag = 1;
}
else
{
num1[length1 - 1] = '0' + s;
flag = 0;
}
length1--; //向高位移动1位
length2--; //向高位移动1位
}
//return k=0? num1:'-'+num1;
if (k == 0)
return num1;
else
return '-' + num1;
}
#endif
结语
代码均为原创
版权归作者所有!
仅供小伙伴们参考学习使用!
请勿用于其他用途!
希望对您有所帮助
如有错误欢迎小伙伴指正~
我是 海轰ଘ(੭ˊᵕˋ)੭
创作不易
点个赞吧
谢谢支持❤️
评论
谷歌员工爆料 Python 基础团队原地解散
转自 | 机器之心编辑 | 蛋酱什么?谷歌解雇了整个 Python 基础团队?「当与你直接共事的每个人,包括你的主管,都被裁员 —— 哦,是职位被削减,而你被要求安排他们的替代者入职,这些人被告知在不同的国家担任同样的职位,但他们并不为此感到高兴,这是很艰难的一天。」发布这一动态的 Tho
机器学习算法与Python实战
0
测试新人,如何快速上手一个陌生的系统!
大家好,我是狂师!作为刚入行不久的测试新人,面对一个陌生的系统时,可能会感到有些手足无措。面对一个全新的系统系统,如何快速上手并展开有效的测试工作是一个重要的挑战。本文将探讨测试新人如何通过一系列步骤和策略,快速熟悉并掌握新系统的测试要点,从而提高测试效率和质量。本文旨在为测试新手提供一份指导,帮助
测试开发技术
0
谷歌员工爆料Python基础团队原地解散
机器之心报道编辑:蛋酱什么?谷歌解雇了整个 Python 基础团队?「当与你直接共事的每个人,包括你的主管,都被裁员 —— 哦,是职位被削减,而你被要求安排他们的替代者入职,这些人被告知在不同的国家担任同样的职位,但他们并不为此感到高兴,这是很艰难的一天。」发布这一动态的 Thomas Wouter
机器学习初学者
0
教你基于 Jenkins 搭建一套 CI/CD 系统
原文链接:https://mp.weixin.qq.com/s/KVc4IFky1z7kZn-7bz79DQ一、CI/CD环境介绍本次要实现如下效果,开发人员完成功能开发并提交代码到gitlab仓库,jenkins自动完成拉取代码、编译构建、代码扫描(sonarqube)、打
码农编程进阶笔记
0
Linux系统性能优化:七个实战经验
来源公众号:twt社区IT社区原文链接:https://mp.weixin.qq.com/s/Rey0gSnnj-zoAEwE6J-GjwLinux系统的性能是指操作系统完成任务的有效性、稳定性和响应速度。Linux系统管理员可能经常会遇到系统不稳定、响应速度慢等问题,例如在Linux上搭建了一个w
开源Linux
1
Java项目实战——打造一款股票区间交易盯盘系统
点击上方“Java进阶学习交流”,进行关注后台回复“Java”即可获赠Java学习资料今日鸡汤身无彩凤双飞翼,心有灵犀一点通。一、简介大家好,我是Snowball。今天给大家分享的内容是基于Java编程,实现股票交易相关功能开发,如果读者对股票或金融衍生物交易不太了解,又比较感兴趣的话可自行查询相关
Java进阶学习交流
0
AI智能视觉检测技术在工业级测量领域的创新应用--AMB Tube-Q导管数字化测量系统
技术背景在深度学习算法出来之前,对于视觉算法来说,大致可以分为以下5个步骤:特征感知,图像预处理,特征提取,特征筛选,推理预测与识别。早期的机器学习中,占优势的统计机器学习群体中,对特征是不大关心的。深度学习是机器学习技术的一个方面,由人工神经网络提供支持。深度学习技术的工作原理是教机器通过实例学习
机器视觉
0
神作《凤凰架构:构建可靠的大型分布式系统》PDF来了
今天给大家带来了一本大作:周志明老师的 《凤凰架构:构建可靠的大型分布式系统》PDF版来了,文末直接获取周志明是谁?这可是真大佬,或许有些朋友没有听过,但是你们一定听过:《深入理解 Java 虚拟机》这本神书,没错就是这本书的作者,出过多本书,豆瓣评分如下,全是高分,尤其是《深入理解Java虚拟机》
路人甲Java
0