Yu+Chuan

Type in the content of your page here. code //Why do you have 4 extra attributes in class. Redundant implementation.

#include

using namespace std;

class Rational{ protected: int num2,den2; public: int num, den,resultnum,resultden; Rational{ num=0; den=1; }; Rational(int n, int d){ num = n; den = d; }; void Display{ cout << num << "/" << den<<endl; }; void Set(int n, int d){ num = n; den = d; } void Set_num(int n){num = n; }; void Set_den(int d){den = d; }; void set(int x,int y){ num=x; den=y; };

code //The original content for both operands should not be altered. code Rational ADD(Rational a){ num2=a.num; den2=a.den; if(den2==den){ den=den; num=num2+num; }else if(den!=0){ num*=den2; num2*=den; den*=den2; den2=den; num+=num2; }else{ num=num2; den=den2; } int divisor=reduce(num,den); num=num/divisor; den=den/divisor; Rational X(num,den); return X; };

//Similarly, the original content for both operands should not be altered. Rational SUBTRACT(Rational a){ num2=a.num; den2=a.den; if(den2==den){ den=den; num=num2-num; }else if(den!=0){ num*=den2; num2*=den; den*=den2; den2=den; num-=num2; }else{ num=-num2; den=-den2; } int divisor=reduce(num,den); num=num/divisor; den=den/divisor; Rational X(num,den); return X; };

//num2 and den2 redundant. simply direct access to a.num and a.den will do. Rational MULTIPLY(Rational a){ num2=a.num; den2=a.den; num*=num2; den*=den2; int divisor=reduce(num,den); num=num/divisor; den=den/divisor; Rational X(num,den); return X; }; code //num2 and den2 redundant. simply direct access to a.num and a.den will do. code Rational DIVIDE(Rational a){ num2=a.num; den2=a.den; num*=den2; den*=num2; int divisor=reduce(num,den); num=num/divisor; den=den/divisor; Rational X(num,den); return X; };

int reduce(int num1,int num2){ int temp1=num1; int temp2=num2; for(int i=1;i<=num1;i++){ if(temp1%i==0&&temp2%i==0){ temp1=temp1/i; temp2=temp2/i; } } int div=num1/temp1; return div; }; };

int main{ Rational R1(2,3), R2(1,4), R3(0,1); R3=R1.SUBTRACT(R2); R3.Display; return 0; }

code