题目:创建一个名为Rational的类,进行分数运算。
创建一个名为Rational的类,进行分数运算。用整数变量表示类的private数据-numerator(分子)和denominator(分母)。提供一个带默认值的构造函数,并且它应该以简化的形式保存分数。
例如分数2/4应在对象中保存为numerator为1,denominator为2的形式。
对下列任务,提供完成它们的public成员函数:
a)两个Rational值相加,结果应以简化的形式保存。
b)两个Rational值相减,结果应以简化的形式保存。
c)两个Rational值相乘,结果应以简化的形式保存。
d)两个Rational值相除,结果应以简化的形式保存。
e)以a/b形式打印Rational值,其中a是分子,b是分母。
f)以浮点数的形式打印Rational值。
主函数分别输入两个分数的分子和分母,以及将要进行的运算。分别以分数形式和小数形式输出运算结果。
注意,负分数输出时负号后有一个空格,小数的负号后没有空格。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Rational {
private:
void simplify() {
int flag = abs(this->numerator);
while(flag > 1) {
if((this->numerator % flag) == 0 && (this->denominator % flag) == 0) {
this->numerator /= flag;
this->denominator /= flag;
}
flag--;
}
}
public:
int numerator;
int denominator;
Rational(int c_num = 0, int c_den = 0) {
if ((c_num > 0 && c_den > 0) || (c_num < 0 && c_den < 0)) {
this->numerator = abs(c_num);
this->denominator = abs(c_den);
} else {
this->numerator = -abs(c_num);
this->denominator = abs(c_den);
}
c_num = abs(c_num);
while(c_num > 1) {
if((this->numerator % c_num) == 0 && (this->denominator % c_num) == 0) {
this->numerator /= c_num;
this->denominator /= c_num;
}
c_num--;
}
}
void add(Rational a, Rational b) {
this->numerator += (a.numerator * b.denominator + a.denominator * b.numerator);
this->denominator += (a.denominator * b.denominator);
this->simplify();
}
void minus(Rational a, Rational b) {
this->numerator += (a.numerator * b.denominator - a.denominator * b.numerator);
this->denominator += (a.denominator * b.denominator);
this->simplify();
}
void multi(Rational a, Rational b) {
this->numerator += (a.numerator * b.numerator);
this->denominator += (a.denominator * b.denominator);
this->simplify();
}
void divide(Rational a, Rational b) {
this->numerator += (a.numerator * b.denominator);
this->denominator += (a.denominator * b.numerator);
this->simplify();
}
void printFormal() {
cout << numerator << "/" << denominator << endl;
}
void printBoth() {
cout << "the Formal format of the rational is : " << numerator << "/" << denominator << endl;
cout << "the Fixed format of the rational is : " << setprecision(2) << fixed << (double)numerator/denominator << endl;
}
};
int main() {
int firstN,firstD,secondN,secondD;
char op;
// cout<<"Please input the numerator of first Rational: ";
cin>>firstN;
// cout<<"Please input the denominator of first Rational: ";
cin>>firstD;
// cout<<"Please input the numerator of second Rational: ";
cin>>secondN;
// cout<<"Please input the denominator of second Rational: ";
cin>>secondD;
Rational r1(firstN,firstD),r2(secondN,secondD),r3;
cin>>op;
cout<<"the Formal format of the first rational is : ";
r1.printFormal();
cout<<"the Formal format of the second rational is : ";
r2.printFormal();
cout<<endl;
switch(op) {
case '+' :
r3.add(r1,r2);
r3.printBoth();
break;
case '-' :
r3.minus(r1,r2);
r3.printBoth();
break;
case '*':
r3.multi(r1,r2);
r3.printBoth();
break;
case '/':
r3.divide(r1,r2);
r3.printBoth();
break;
default:
cout<<"Invalid operator!";
}
return 0;
}
运行结果:
标签:denominator,void,编程,numerator,C++,flag,num,Rational From: https://blog.51cto.com/u_16532251/9561059