【課程設計|C++】C++程序設計基礎:小學生數學練習系統(tǒng)
功能需求
程序需要實現(xiàn)的功能如下:
加法、減法、乘法、除法簡易練習 加減混合練習 查看錯題(txt文件存儲) 自我檢測 大整數加法 大整數減法
難度 ????
程序運行圖
系統(tǒng)主界面

加法練習
用戶輸入需要練習題目的數量,程序隨機生成算式
用戶回答正確,得分+10
回答錯誤,該題存入錯題本

減法練習

加減混合運算

乘法運算

除法運算

錯題本查看
用戶做錯過的題均存在一個txt文件中
用戶可以隨時查看
并再做一遍

自測功能(難點)
用戶使用該功能
可以自行輸入一個多項式
如
1+2*3 2??3-3+5/4+2-3*3 1+2-3*4/5
程序會自動算出結果并與用戶輸入進行對比
(這個當時寫的時候感覺還是有點難度)

大整數減法(難點)
實現(xiàn)兩個任意整數的減法運算,不用擔心int、double類型溢出

大整數加分(難點)
實現(xiàn)兩個任意整數的加法運算,不用擔心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;
//判斷 符號優(yōu)先級
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 << "* 歡迎來到小學生加法系統(tǒng) *" << 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 '<': //棧頂元素優(yōu)先級低
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 << "是否繼續(xù)?1:繼續(xù) 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
//...................................................................................節(jié)點類
#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
結語
代碼均為原創(chuàng)
版權歸作者所有!
僅供小伙伴們參考學習使用!
請勿用于其他用途!
希望對您有所幫助
如有錯誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
創(chuàng)作不易
點個贊吧
謝謝支持??

評論
圖片
表情
