主程序:
#include "stdafx.h"
#include "Calculator.h"
int main()
{
new Calculator();
system("pause");
return 0;
}
转换与计算
#include "stdafx.h"
#include "Calculator.h"
Calculator::Calculator()
{
Run();
}
Calculator::~Calculator()
{
}
bool Calculator::GetInfixExp() {
char c;
int newOperand;
bool lastExpIsNum = false;
bool nextNumAddMinus = false;
char printExp[128];
ifs_line.getline(printExp, 128);
cout << printExp << " ";
while (ifs >> c, c != '=') {
switch (c) {
case '-':
if (lastExpIsNum)
{
InfixExp[il].Operator = c;
}
else
{
nextNumAddMinus = true;
il--;
}
lastExpIsNum = false;
break;
case '+':
case '*':
case '/':
case '(':
InfixExp[il].Operator = c;
lastExpIsNum = false;
break;
case ')':
InfixExp[il].Operator = c;
lastExpIsNum = true;
break;
default:
if (c >= '0' && c <= '9') {
ifs.putback(c);
ifs >> newOperand;
newOperand = nextNumAddMinus ? -newOperand : newOperand;
nextNumAddMinus = false;
InfixExp[il].Operand = Fraction(newOperand);
}
else {
return false;
}
lastExpIsNum = true;
break;
}
il++;
}
return true;
}
bool Calculator::Transform() {
bool flag; //标识
for (int i = 0; i < il; i++) { //扫描中缀表达式
if (InfixExp[i].Operator == NULL) { //如果是操作数
PostfixExp[pl].Operand = InfixExp[i].Operand;
pl++;
}
else if (InfixExp[i].Operator == '(') {
is.push('(');
}
else if (InfixExp[i].Operator == ')') {
if (is.empty()) {
return false;
}
else {
flag = false; //flag标识是否找到'('
while (!is.empty()) {
if (is.top() != '(') {
PostfixExp[pl].Operator = is.top();
is.pop();
pl++;
}
else {
flag = true;
is.pop();
break;
}
}
if (is.empty() && !flag) {
return false;
}
}
}
else {
while (
!is.empty() &&
is.top() != '(' &&
!((is.top() == '+' || is.top() == '-') && (InfixExp[i].Operator == '*' || InfixExp[i].Operator == '/'))
) {
PostfixExp[pl].Operator = is.top();
is.pop();
pl++;
}
is.push(InfixExp[i].Operator);
}
}
while (!is.empty()) {
if (is.top() == '(') {
return false;
}
else {
PostfixExp[pl].Operator = is.top();
is.pop();
pl++;
}
}
return true;
}
bool Calculator::GetTwoOperands(Fraction& opd1, Fraction& opd2) {
if (ps.empty())
return false;
opd1 = ps.top();
ps.pop();
if (ps.empty())
return false;
opd2 = ps.top();
ps.pop();
return true;
}
bool Calculator::Compute(char op) {
bool result;
Fraction operand1, operand2;
result = GetTwoOperands(operand1, operand2);
if (result) {
switch (op)
{
case '+':
ps.push(operand2 + operand1);
break;
case '-':
ps.push(operand2 - operand1);
break;
case '*':
ps.push(operand2 * operand1);
break;
case '/':
if (operand1.numerator == 0) {
cout << "除数存在0,错误!" << endl;
return false;
}
else {
ps.push(operand2 / operand1);
}
break;
}
}
return true;
}
void Calculator::Run() {
ifs = ifstream("Expressions.txt"); //读入字符串
ifs_line = ifstream("Expressions.txt"); //读入完整式子
Fraction correct_answer, user_answer; // 正确答案和用户答案
int correct_num = 0, wrong_num = 0;
// 输入中缀表达式
cout << "请计算下列算式:" << endl;
while (!ifs_line.eof())
{
il = 0;
pl = 0;
while (!is.empty())
is.pop();
while (!ps.empty())
ps.pop();
for (int i = 0; i < 100; i++)
{
InfixExp[i] = PostfixExp[i] = Expression();
}
if (GetInfixExp() && Transform()) {
for (int i = 0; i < pl; i++) {
if (PostfixExp[i].Operator == NULL)
ps.push(PostfixExp[i].Operand);
else {
if (!Compute(PostfixExp[i].Operator))
return;
}
}
correct_answer = ps.top();
}
else
{
cout << "算式格式错误" << endl;
return;
}
cin >> user_answer;
user_answer.reduction(), correct_answer.reduction();
if (user_answer == correct_answer)
{
correct_num++;
cout << "计算正确" << endl;
//cout << ",共正确" << correct_num << "道,错误" << wrong_num << "道" << endl;
}
else
{
wrong_num++;
cout << "计算错误,正确答案是" << correct_answer << endl;
//cout << ",共正确" << correct_num << "道,错误" << wrong_num << "道" << endl;
}
}
cout << "所有算式计算完毕" << endl;
cout << "共" << correct_num + wrong_num << "题,";
cout << "正确" << correct_num << "道,错误" << wrong_num << "道" << endl;
}
#include "stdafx.h"
#include "Fraction.h"
Fraction::Fraction()
{
}
Fraction::Fraction(int n) :numerator(n), denominator(1)
{
}
Fraction::Fraction(int n, int d) : numerator(n), denominator(d)
{
}
Fraction::~Fraction()
{
}
Fraction Fraction::operator + (Fraction f) {
return Fraction(numerator * f.denominator + denominator * f.numerator, denominator * f.denominator);
}
Fraction Fraction::operator - (Fraction f) {
return Fraction(numerator * f.denominator - denominator * f.numerator, denominator * f.denominator);
}
Fraction Fraction::operator * (Fraction f) {
return Fraction(numerator * f.numerator, denominator * f.denominator);
}
Fraction Fraction::operator / (Fraction f) {
return Fraction(numerator * f.denominator, denominator * f.numerator);
}
bool Fraction::operator == (Fraction f) {
return numerator == f.numerator && denominator == f.denominator;
}
istream& operator >> (istream &in, Fraction &f) {
in >> f.numerator;
f.denominator = 1;
char c;
in.get(c);
if (c != '\n')
{
in >> f.denominator;
}
return in;
}
ostream& operator << (ostream &out, Fraction &f) {
f.reduction();
if (f.denominator == 1)
out << f.numerator;
else
out << f.numerator << "/" << f.denominator;
return out; //分母为1输出正数,否则输出除法形式
}
void Fraction::reduction() {
int gcd = get_gcd(numerator, denominator);
numerator = numerator / gcd;
denominator = denominator / gcd;
if (denominator < 0)
{
numerator = -numerator;
denominator = -denominator;
} //分母为负数,上下取反 约分
}
int Fraction::get_gcd(int a, int b)//求最大公约数,a为分子,b为分母
{
if (b == 0)
return a;
return get_gcd(b, a%b);
}