[求助]C++分数计算器运行计算结果出现Process exited after 15.56 seconds with return value 32212
//main#include"first.h"
#include"second.h"
#include<iostream>
using namespace std;
int main()
{
Integer dis;
Fraction a, b, result, real;
int m, z;
char c;
dis.display();
cout << "请输入数字:";
cin >> m;
while (m != 0)
{
if (m == 1)
{
cout << endl;
cout << "请输入一个分数:";
cin >> a ;
cout << "请输入运算符";
cin >> c ;
cout << "请输入一个分数:";
cin >> b ;
if (c == '+') result = a + b;
else if (c == '-') result = a - b;
else if (c == '*') result = a * b;
else if (c == '/') result = a / b;
real = result.simplify();
cout << real << endl;
cout << endl;
}
if (m == 2)
{
cout << endl;
cout << "请输入一个分数:";
cin >> a ;
cout << "请输入运算符";
cin >> c ;
cout << "请输入一个整数:";
cin >> z ;
if (c == '+') result = a + z;
if (c == '-') result = a - z;
if (c == '*') result = a * z;
if (c == '/') result = a / z;
real = result.simplify();
cout << real << endl;
cout << endl;
}
if (m == 3)
{
cout << endl;
cout << "请输入一个整数:";
cin >> z ;
cout << "请输入运算符";
cin >> c ;
cout << "请输入一个分数:";
cin >> b ;
if (c == '+') result = b + z;
if (c == '-') result = b - z;
if (c == '*') result = b * z;
if (c == '/') result = b / z;
real = result.simplify();
cout << real << endl;
cout << endl;
}
}
return 0;
}
//first
#ifndef _First_H_
#define _First_H_
#include<iostream>
using namespace std;
class Integer //整数类
{
public:
int numerator; //分子
int denominator; //分母
Integer(int a = 0, int b = 1) :numerator(a), denominator(b) {}
~Integer() {}
void display();
};
class Fraction :public Integer //分数类
{
public:
Fraction(int a = 0, int b = 1) :Integer(a, b) {}
friend istream &operator>>(istream &, Fraction &); //重载流插入
friend ostream &operator<<(ostream &, Fraction &); //重载流读取
Fraction operator+(Fraction &c1); //分分
Fraction operator+(int c2); //分整
Fraction operator-(Fraction &c1); //分分
Fraction operator-(int c2); //分整
friend Fraction operator-(int c2, Fraction &c1);
Fraction operator*(Fraction &c1); //分分
Fraction operator*(int c2); //分整
Fraction operator/(Fraction &c1); //分分
Fraction operator/(int c2); //分整
friend Fraction operator/(int c2, Fraction &c1);
Fraction simplify(); //化简函数
void display();
};
#endif
//second
#ifndef _SECOND_H_
#define _SECOND_H_
#include<iostream>
using namespace std;
void Integer::display()
{
cout << endl;
cout << "------请输入数字0-3选择计算器的功能------" << endl;
cout << "--------------0、退出计算器--------------" << endl;
cout << "---------1、分数与分数的四则运算---------" << endl;
cout << "---------2、分数与整数的四则运算---------" << endl;
cout << "---------3、整数与分数的四则运算---------" << endl;
}
Fraction Fraction::operator+(Fraction &c1)
{
Fraction c;
c.numerator = numerator * c1.denominator + c1.numerator*denominator;
c.denominator = denominator * c1.denominator;
return c;
}
Fraction Fraction::operator+(int c2)
{
Fraction c;
c.numerator = denominator * c2 + numerator;
c.denominator = denominator;
return c;
}
Fraction Fraction::operator-(Fraction &c1)
{
Fraction c;
c.numerator = numerator * c1.denominator - c1.numerator*denominator;
c.denominator = denominator * c1.denominator;
return c;
}
Fraction Fraction::operator-(int c2)
{
Fraction c;
c.numerator = denominator * c2 - numerator;
c.denominator = denominator;
return c;
}
Fraction operator-(int c2, Fraction &c1)
{
Fraction c;
c.numerator = c1.denominator*c2 - c1.numerator;
c.denominator = c1.denominator;
return c;
}
Fraction Fraction::operator*(Fraction &c1)
{
Fraction c;
c.numerator = numerator * c1.numerator;
c.denominator = denominator * c1.denominator;
return c;
}
Fraction Fraction::operator*(int c2)
{
Fraction c;
c.numerator = numerator * c2;
c.denominator = denominator;
return c;
}
Fraction Fraction::operator/(Fraction &c1)
{
Fraction c;
if (c1.numerator == 0) return *this;
c.numerator = numerator * c1.denominator;
c.denominator = denominator * c1.numerator;
return c;
}
Fraction Fraction::operator/(int c2)
{
Fraction c;
c.numerator = numerator;
c.denominator = denominator * c2;
return c;
}
Fraction operator/(int c2, Fraction &c1)
{
Fraction c;
c.numerator = c2 * c1.denominator;
c.denominator = c1.numerator;
return c;
}
istream &operator>>(istream &input, Fraction &x)//重载输入符
{
int a, b;
char t;
input >> a >> t >> b;
if (t == '/'&&b != 0)
{
x.numerator = a;
x.denominator = b;
}
else
{
cout << "分数格式错误或分母为零!请重新输入:";
}
return input;
}
ostream &operator<<(ostream &output, Fraction &x)//重载输出符
{
int a, b;
a = x.numerator;
b = x.denominator;
if (a == 0) cout << "0" << endl;
if (b == 0) cout << "计算错误!" << endl;
else output << x.numerator << "/" << x.denominator;
return output;
}
Fraction Fraction::simplify()//化简函数
{
int nume, deno, r, t;
int s = 1;
Fraction c;
if (numerator < 0 && denominator < 0)
{
numerator = -numerator;
denominator = -denominator;
}
if (numerator > 0 && denominator < 0)
{
denominator = -denominator;
s = -1;
}
if (numerator < 0 && denominator>0)
{
numerator = -numerator;
s = -1;
}
nume = numerator;
deno = denominator;
if (nume < deno)
{
t = nume;
nume = deno;
deno = t;
}
while (deno != 0)
{
r = nume % deno;
nume = deno;
deno = r;
}
c.numerator = s * (numerator / deno);
c.denominator = s * (denominator / deno);
return c;
}
#endif