C++用类的方法实现加减乘除演算
C++用类的方法实现加减乘除运算

如图,新人求解
------解决思路----------------------
首先求两个分母最小公倍数(对于加法和减法)
分数的加法 (分子+分子)/(分母+分母)(需要最小公倍数,把分母变成最小公倍数,分子记得相应改变)
分数的减法 (分子+分子)/(分母+分母)(需要最小公倍数)
分数的乘法 (分子*分子)/(分母*分母)
分数的除法 (分子1*分母2)/(分子2*分母1)
最后求结果数分子和分母的最小公约数,然后分别出去最小公约数,即简化后的答案
------解决思路----------------------
没做错误处理,也没编译,仅供参考
------解决思路----------------------
如图,新人求解
------解决思路----------------------
首先求两个分母最小公倍数(对于加法和减法)
分数的加法 (分子+分子)/(分母+分母)(需要最小公倍数,把分母变成最小公倍数,分子记得相应改变)
分数的减法 (分子+分子)/(分母+分母)(需要最小公倍数)
分数的乘法 (分子*分子)/(分母*分母)
分数的除法 (分子1*分母2)/(分子2*分母1)
最后求结果数分子和分母的最小公约数,然后分别出去最小公约数,即简化后的答案
------解决思路----------------------
没做错误处理,也没编译,仅供参考
int gcd(int a,int b) {
return b==0 ? a : gcd(b,a%b);
}
void show(int num,int den){
bool neg=false;
if(num<0) neg=!neg, num=-num;
if(den<0) neg=!neg, den=-den;
if(neg) cout<<'-';
int g =gcd(num,den);
cout<<num/g<<'/'<<den/g;
}
int main()
{
int a,b,c,d;
while(cin>>a>>b>>c>>d){
// a/b+c/d = (ad+bc)/bd
show(a*d+b*c,b*d);
cout<<' ';
// a/b+c/d =(ad-bc)/bd;
show(a*d-b*c,b*d);
cout<<' ';
// a/b * c/d =ac/bd
show(a*c,b*d);
cout<<' ';
// a/b / c/d =ad/bc
show(a*d,b*c);
cout<<endl;
}
return 0;
}
------解决思路----------------------
#include<iostream>
using namespace std;
class Process
{
friend Process read(int &a, int &b);
public:
Process() = default;
Process(int &a, int &b) : zi(a), mu(b){}; //构造函数
private:
int zi = 0, mu = 0; //分子分母
int result[2]; //结果
public:
void operator* (const Process &b) //乘法重载
{
//Process c;
result[0] = this->zi * b.zi;
result[1] = this->mu * b.mu;
int j = result[0];
int k = result[1];
for (auto i = 2; i < ((j < k) ? j : k);)
{
if ((result[0] % i == 0 && result[1] % i == 0))
{
result[0] = result[0] / i;
result[1] = result[1] / i;
continue;
}
else
i++;
}
cout << result[0] <<"/" << result[1] << " ";
//return result;
}
void operator+ (Process&b) //加法重载
{
int temp[2] = { 0 };
temp[0] = this->zi*b.mu;
temp[1] = this->mu*b.zi;
result[0] = temp[0] + temp[1];
result[1] = this->mu*b.mu;
int j = result[0];
int k = result[1];
//分子分母 约分
for (auto i = 2; i < ((j < k) ? j : k) ;)
{
if ((result[0] % i == 0 && result[1] % i == 0))
{
result[0] = result[0] / i;
result[1] = result[1] / i;
continue;
}
else
i++;
}
cout << result[0] << "/"<< result[1] << " ";
}
void operator/ (const Process &b) //减法重载
{
result[0] = this->zi*b.mu;
result[1] = this->mu*b.zi;
int j = result[0];
int k = result[1];
for (auto i = 2; i < ((j < k) ? j : k);)
{
if ((result[0] % i == 0 && result[1] % i == 0))
{
result[0] = result[0] / i;
result[1] = result[1] / i;
continue;
}
else
i++;
}
cout << result[0] << "/" << result[1] << " ";
}
void operator- (const Process &b) //除法重载
{
result[0] = (this->zi * b.mu) - (b.zi * this->mu);
result[1] = this->mu * b.mu;
int j = result[0];
int k = result[1];
for (auto i = 2; i < ((j < k) ? j : k);)
{
if ((result[0] % i == 0 && result[1] % i == 0))
{
result[0] = result[0] / i;
result[1] = result[1] / i;
continue;
}
else
i++;
}
cout << result[0] << "/" << result[1] << " ";
}
};
Process read(int &a, int &b) //在类外定义成员函数
{
Process temp;
temp.zi = a;
temp.mu = b;
return temp;
}
int main(void)
{
int a=0, b=0, c=0, d=0;
int *jie;
Process first, second;
cin >> a >> b >> c >> d; //输入分子分母
first = read(a, b); //赋值给第一个分数
second = read(c, d); //赋值给第二个分数
// + - * / 运算
first.operator+(second);
first.operator-(second);
first.operator*(second);
first.operator/(second);
}