设计出一个合理的分数计算器,可以实现以分数形式输入分数并以含分数的等式输出结果
时间: 2024-02-24 19:59:21 浏览: 60
下面是一个可以以分数形式输入、输出分数结果的 C++ 分数计算器实现:
```
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
struct Fraction {
int numerator; // 分子
int denominator; // 分母
Fraction(int n = 0, int d = 1) {
numerator = n;
denominator = d;
reduce();
}
void reduce() {
int g = gcd(numerator, denominator);
numerator /= g;
denominator /= g;
}
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
Fraction operator+(const Fraction& f) const {
int l = lcm(denominator, f.denominator);
int m1 = l / denominator;
int m2 = l / f.denominator;
int n = numerator * m1 + f.numerator * m2;
return Fraction(n, l);
}
Fraction operator-(const Fraction& f) const {
int l = lcm(denominator, f.denominator);
int m1 = l / denominator;
int m2 = l / f.denominator;
int n = numerator * m1 - f.numerator * m2;
return Fraction(n, l);
}
Fraction operator*(const Fraction& f) const {
int n = numerator * f.numerator;
int d = denominator * f.denominator;
return Fraction(n, d);
}
Fraction operator/(const Fraction& f) const {
int n = numerator * f.denominator;
int d = denominator * f.numerator;
return Fraction(n, d);
}
bool operator==(const Fraction& f) const {
return (numerator == f.numerator && denominator == f.denominator);
}
bool operator!=(const Fraction& f) const {
return !(*this == f);
}
bool operator<(const Fraction& f) const {
int l = lcm(denominator, f.denominator);
int m1 = l / denominator;
int m2 = l / f.denominator;
int n1 = numerator * m1;
int n2 = f.numerator * m2;
return (n1 < n2);
}
bool operator>(const Fraction& f) const {
return (f < *this);
}
bool operator<=(const Fraction& f) const {
return (*this < f || *this == f);
}
bool operator>=(const Fraction& f) const {
return (*this > f || *this == f);
}
};
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int lcm(int a, int b) {
return a * b / gcd(a, b);
}
string toString(const Fraction& f) {
ostringstream oss;
if (f.denominator == 1) {
oss << f.numerator;
} else if (abs(f.numerator) > f.denominator) {
int q = f.numerator / f.denominator;
int r = f.numerator % f.denominator;
oss << q << " " << abs(r) << "/" << f.denominator;
} else {
oss << f.numerator << "/" << f.denominator;
}
return oss.str();
}
Fraction parseFraction(const string& s) {
int n, d;
char c;
stringstream ss(s);
if (ss >> n >> c >> d) {
return Fraction(n, d);
} else {
return Fraction(n);
}
}
int main() {
string input;
getline(cin, input);
int pos = input.find('=');
if (pos == string::npos) {
cout << "Invalid input\n";
return 0;
}
Fraction left = parseFraction(input.substr(0, pos));
Fraction right = parseFraction(input.substr(pos + 1));
Fraction result;
char op;
if (input.find('+') != string::npos) {
op = '+';
result = left + right;
} else if (input.find('-') != string::npos) {
op = '-';
result = left - right;
} else if (input.find('*') != string::npos) {
op = '*';
result = left * right;
} else if (input.find('/') != string::npos) {
op = '/';
result = left / right;
} else {
cout << "Invalid input\n";
return 0;
}
cout << toString(left) << " " << op << " " << toString(right) << " = " << toString(result) << endl;
return 0;
}
```
这个程序可以读入一个包含分数的等式,并计算出其结果,并将结果以含分数的形式输出到屏幕上。
在主函数中,我们首先读入一个等式,然后使用字符串的 find() 方法查找等式中的运算符,并将等式分成左右两个部分,分别用 parseFraction() 函数将它们转化为分数对象,然后使用 operator+()、operator-()、operator*() 和 operator/() 方法进行加、减、乘、除运算,将结果保存到 result 对象中,并用 toString() 函数将分数对象转化为字符串形式输出到屏幕上。
parseFraction() 函数可以将形如 "a/b" 或 "a" 的字符串转化为分数对象,toString() 函数则可以将分数对象转化为含分数的字符串形式。
注意,为了防止输出的分数有多余的约分,我们在 Fraction 类中定义了 reduce() 方法,在构造函数中调用它以化简分数。同时,为了方便比较大小,我们还重载了比较运算符。
阅读全文