第九周实验报告(3)
来源:互联网 发布:噩梦会成真吗 知乎 编辑:程序博客网 时间:2024/05/22 16:49
- #include <iostream>
- using namespace std;
- class CFraction
- {
- private:
- int nume;
- int deno;
- public:
- CFraction(int nu=0,int de=1):nume(nu),deno(de){}
- void simplify();
-
-
- friend istream &operator>>(istream &in,CFraction &x);
- friend ostream &operator<<(ostream &out,CFraction x);
-
- CFraction operator+(const CFraction &c);
- CFraction operator-(const CFraction &c);
- CFraction operator*(const CFraction &c);
- CFraction operator/(const CFraction &c);
- CFraction operator+();
- CFraction operator-();
- bool operator>(const CFraction &c);
- bool operator<(const CFraction &c);
- bool operator==(const CFraction &c);
- bool operator!=(const CFraction &c);
- bool operator>=(const CFraction &c);
- bool operator<=(const CFraction &c);
- };
-
-
- void CFraction::simplify()
- {
- int m,n,r;
- m=abs(deno);
- n=abs(nume);
- while(r=m%n)
- {
- m=n;
- n=r;
- }
- deno/=n;
- nume/=n;
- if (deno<0)
- {
- deno=-deno;
- nume=-nume;
- }
- }
-
-
- istream &operator>>(istream &in,CFraction &x)
- {
- char ch;
- while(1)
- {
- cin>>x.nume>>ch>>x.deno;
- if (x.deno==0)
- cerr<<"分母为! 请重新输入\n";
- else if(ch!='/')
- cerr<<"格式错误(形如m/n)! 请重新输入\n";
- else
- break;
- }
- return cin;
- }
-
-
- ostream &operator<<(ostream &out,CFraction x)
- {
- cout<<x.nume<<'/'<<x.deno;
- return cout;
- }
-
-
- CFraction CFraction::operator+(const CFraction &c)
- {
- CFraction t;
- t.nume=nume*c.deno+c.nume*deno;
- t.deno=deno*c.deno;
- t.simplify();
- return t;
- }
-
-
- CFraction CFraction:: operator-(const CFraction &c)
- {
- CFraction t;
- t.nume=nume*c.deno-c.nume*deno;
- t.deno=deno*c.deno;
- t.simplify();
- return t;
- }
-
-
- CFraction CFraction:: operator*(const CFraction &c)
- {
- CFraction t;
- t.nume=nume*c.nume;
- t.deno=deno*c.deno;
- t.simplify();
- return t;
- }
-
-
- CFraction CFraction:: operator/(const CFraction &c)
- {
- CFraction t;
- if (!c.nume) return *this;
- t.nume=nume*c.deno;
- t.deno=deno*c.nume;
- t.simplify();
- return t;
- }
-
-
- CFraction CFraction:: operator+()
- {
- return *this;
- }
-
-
- CFraction CFraction:: operator-()
- {
- CFraction x;
- x.nume=-nume;
- x.deno=deno;
- return x;
- }
-
-
- bool CFraction::operator>(const CFraction &c)
- {
- int this_nume,c_nume,common_deno;
- this_nume=nume*c.deno;
- c_nume=c.nume*deno;
- common_deno=deno*c.deno;
- if ((this_nume-c_nume)*common_deno>0) return true;
- return false;
- }
-
-
- bool CFraction::operator<(const CFraction &c)
- {
- int this_nume,c_nume,common_deno;
- this_nume=nume*c.deno;
- c_nume=c.nume*deno;
- common_deno=deno*c.deno;
- if ((this_nume-c_nume)*common_deno<0) return true;
- return false;
- }
-
-
- bool CFraction::operator==(const CFraction &c)
- {
- if (*this!=c) return false;
- return true;
- }
-
-
- bool CFraction::operator!=(const CFraction &c)
- {
- if (*this>c || *this<c) return true;
- return false;
- }
-
-
- bool CFraction::operator>=(const CFraction &c)
- {
- if (*this<c) return false;
- return true;
- }
-
-
- bool CFraction::operator<=(const CFraction &c)
- {
- if (*this>c) return false;
- return true;
- }
-
- int main()
- {
- CFraction x,y,s;
- cout<<"输入x: ";
- cin>>x;
- cout<<"输入y: ";
- cin>>y;
- s=+x+y;
- cout<<"+x+y="<<s<<endl;
- cout<<"x-y="<<s<<endl;
- s=x*y;
- cout<<"x*y="<<s<<endl;
- s=x/y;
- cout<<"x/y="<<s<<endl;
- s=-x+y;
- cout<<"-x+y="<<s<<endl;
-
- cout<<x;
- if (x>y) cout<<"大于";
- if (x<y) cout<<"小于";
- if (x==y) cout<<"等于";
- cout<<y<<endl;
- system("pause");
- return 0;
- }