项目三(选做)

来源:互联网 发布:法院网络司法拍卖 编辑:程序博客网 时间:2024/05/17 03:14

程序:

//实现分数类中的运算符重载,在分数类中可以完成和整型数的加减乘除(运算后再化简)、比较(6种关系)的运算#include <iostream>using namespace std;class CFraction;int tong(CFraction c1,CFraction c2);            //通分void hua(int *de,int *nu);                      //化简class CFraction{private:    int nume;                                   // 分子    int deno;                                   // 分母public:    CFraction(int n=0,int d=0);    friend int tong(CFraction c1,CFraction c2); //友元函数    friend void hua(int *de,int *nu);           //为了保留计算所得值,用指针    void display()    {        cout<<nume<<"/"<<deno<<endl;    }    //构造函数及运算符重载的函数声明    CFraction operator+(CFraction &c);    CFraction operator-(CFraction &c);    CFraction operator*(CFraction &c);    CFraction operator/(CFraction &c);    CFraction operator+(int n);    CFraction operator-(int );    CFraction operator*(int );    CFraction operator/(int );    bool operator > (CFraction &c);    bool operator < (CFraction &c);    bool operator >= (CFraction &c);    bool operator <= (CFraction &c);    bool operator == (CFraction &c);    bool operator != (CFraction &c);    //与int型的运算函数声明    friend CFraction operator+(int i,CFraction &c );//这里想了很多种方法,不知道该怎么写,看了贺老师的答案,才知道直接给一个CFraction类型的就可以    friend CFraction operator-(int i,CFraction &c );    friend CFraction operator*(int i,CFraction &c );    friend CFraction operator/(int i,CFraction &c );    friend bool operator > (int ,CFraction &c);    friend bool operator < (int ,CFraction &c);    friend bool operator >= (int ,CFraction &c);    friend bool operator <= (int ,CFraction &c);    friend bool operator == (int ,CFraction &c);    friend bool operator != (int ,CFraction &c);};CFraction::CFraction(int n,int d){    nume=n;    deno=d;}//重载函数的实现及用于测试的main()函数CFraction CFraction::operator+(CFraction &c){    int g,n;    g=tong(*this,c);    n=nume*(g/deno)+c.nume*(g/c.deno);    hua(&g,&n);    return CFraction(n,g);                     //直接返回对象并赋值}CFraction CFraction::operator-(CFraction &c){    int g,n;    g=tong(*this,c);    n=nume*(g/deno)-c.nume*(g/c.deno);    hua(&g,&n);    return CFraction(n,g);}CFraction CFraction::operator*(CFraction &c){    int d,n;    d=deno*c.deno;    n=nume*c.nume;    hua(&d,&n);    return CFraction(n,d);}CFraction CFraction::operator/(CFraction &c){    CFraction c1;    int n;    c1=c;    n=c1.deno;    c1.deno=c1.nume;    c1.nume=n;    return (*this)*c1;}int tong(CFraction c1,CFraction c2)            //通分{    int t[8]={2,3,5,7,11,13,17,19},de1=c1.deno,de2=c2.deno,g=1;    for(int i=0;i<8;i++)    {        while(de1%t[i]==0&&de2%t[i]==0)        {            de1/=t[i];            de2/=t[i];            g*=t[i];        }    }    g*=(de1*de2);    if(g<0)    {        g=-g;                                   //如果分数是负数    }    return g;}void hua(int *de,int *nu){    int t[8]={2,3,5,7,11,13,17,19};    for(int i=0;i<8;i++)    {        while(*de%t[i]==0&&*nu%t[i]==0)        {            *de/=t[i];            *nu/=t[i];        }    }}bool CFraction::operator > (CFraction &c){    bool g=false;    double c1,c2;    c1=nume/deno;    c2=c.nume/c.deno;    if(c1<0)    {        c1=-c1;    }    if(c2<0)    {        c2=-c2;    }    if(c1>c2)    {        g=true;    }    return g;}bool CFraction::operator < (CFraction &c){    bool g=false;    double c1,c2;    c1=nume/deno;    c2=c.nume/c.deno;    if(c1<c2)    {        g=true;    }    return g;}bool CFraction::operator >= (CFraction &c){    bool g=false;    double c1,c2;    c1=nume/deno;    c2=c.nume/c.deno;    if(c1>c2)    {        g=true;    }    return g;}bool CFraction::operator <= (CFraction &c){    bool g=false;    double c1,c2;    c1=nume/deno;    c2=c.nume/c.deno;    if(c1<c2)    {        g=true;    }    return g;}bool CFraction::operator == (CFraction &c){    bool g=false;    double c1,c2;    c1=nume/deno;    c2=c.nume/c.deno;    if(c1==c2)    {        g=true;    }    return g;}bool CFraction::operator != (CFraction &c){    bool g=false;    double c1,c2;    c1=nume/deno;    c2=c.nume/c.deno;    if(c1!=c2)    {        g=true;    }    return g;}//与int型的运算函数定义CFraction CFraction ::operator+(int n){    CFraction c1(this->deno*n,this->deno);    return  *this+c1;}CFraction CFraction ::operator-(int n){    CFraction c1(this->deno*n,this->deno);    return  *this-c1;}CFraction CFraction ::operator*(int n){    CFraction c1(this->deno*n,this->deno);    return  *this*c1;}CFraction CFraction ::operator/(int n){    CFraction c1(this->deno*n,this->deno);    return  *this/c1;}CFraction operator+(int n,CFraction &c){    CFraction c1(c.deno*n,c.deno);    return  c1+c;}CFraction operator-(int n,CFraction &c){    CFraction c1(c.deno*n,c.deno);    return  c1-c;}CFraction operator*(int n,CFraction &c){    CFraction c1(c.deno*n,c.deno);    return  c1*c;}CFraction operator/(int n,CFraction &c){    CFraction c1(c.deno*n,c.deno);    return  c1/c;}bool operator > (int n,CFraction &c){    bool g=false;    CFraction c1(c.deno*n,c.deno);    if(c1>c)    {        g=true;    }}bool operator < (int n,CFraction &c){    bool g=false;    CFraction c1(c.deno*n,c.deno);    if(c1<c)    {        g=true;    }}bool operator >= (int n,CFraction &c){    bool g=false;    CFraction c1(c.deno*n,c.deno);    if(c1>=c)    {        g=true;    }}bool operator <= (int n,CFraction &c){    bool g=false;    CFraction c1(c.deno*n,c.deno);    if(c1<=c)    {        g=true;    }}bool operator == (int n,CFraction &c){    bool g=false;    CFraction c1(c.deno*n,c.deno);    if(c1==c)    {        g=true;    }}bool operator != (int n,CFraction &c){    bool g=false;    CFraction c1(c.deno*n,c.deno);    if(c1!=c)    {        g=true;    }}int main(){    CFraction c1(3,4),c2(11,-6),c3;    int n=5;    cout<<"c1:";    c1.display();    cout<<"c2:";    c2.display();    cout<<endl;    cout<<"c1+c2=";    c3=c1+c2;    c3.display();    cout<<"c1-c2=";    c3=c1-c2;    c3.display();    cout<<"c1*c2=";    c3=c1*c2;    c3.display();    cout<<"c1/c2=";    c3=c1/c2;    c3.display();    cout<<endl;    cout<<"compare c1 and c2:"<<endl;    if(c1>c2)    {        cout<<"c1>c2"<<endl;    }    if(c1<c2)    {        cout<<"c1<c2"<<endl;    }    if(c1>=c2)    {        cout<<"c1>=c2"<<endl;    }    if(c1<=c2)    {        cout<<"c1<=c2"<<endl;    }    if(c1==c2)    {        cout<<"c1==c2"<<endl;    }    if(c1!=c2)    {        cout<<"c1!=c2"<<endl;    }    cout<<endl;    //与int型的运算    cout<<"num="<<n<<endl;    cout<<"c1+n";    c3=c1+n;    c3.display();    cout<<"c1-n=";    c3=c1-n;    c3.display();    cout<<"c1*n=";    c3=c1*n;    c3.display();    cout<<"c1/n=";    c3=c1/n;    c3.display();    cout<<endl;    cout<<"n+c1=";    c3=n+c1;    c3.display();    cout<<"n-c1=";    c3=n-c1;    c3.display();    cout<<"n*c1=";    c3=n*c1;    c3.display();    cout<<"n/c1=";    c3=n/c1;    c3.display();    cout<<endl;    cout<<"compare c1 and n:"<<endl;    if(n>c1)    {        cout<<"c1<n"<<endl;    }    if(n<c1)    {        cout<<"c1>n"<<endl;    }    if(n>=c1)    {        cout<<"c1<=n"<<endl;    }    if(n<=c1)    {        cout<<"n>=c1"<<endl;    }    if(n==c1)    {        cout<<"c1==n"<<endl;    }    if(n!=c1)    {        cout<<"c1!=n"<<endl;    }    cout<<endl;    return 0;}

结果:

体会:好麻烦,一开始想用模板做,可是发现不对,模板似乎做不成类和类,类和整数的共同处理。做整型那里时,不知道该怎么声明函数类型,看了贺老师的,原来一个类返回值的就可以搞定。

0 0
原创粉丝点击