第九周实验报告3.0

时间:2021-11-23 08:06:26

/* (程序头部注释开始)   
* 程序的版权和版本声明部分   
* Copyright (c) 2011, 烟台大学计算机学院学生    
* All rights reserved.   
* 文件名称:022                              
* 作    者:任小宁                          
* 完成日期:  2012   年   4   月    16 日   
* 版 本 号:201158504431          
* 对任务及求解方法的描述部分   
* 输入描述:

#include<iostream.h>   
#include"stdlib.h"    
  
int gcd(int m, int n);    
  
class CFraction  
{  
private:  
    int nume;  // 分子  
    int deno;  // 分母  
public:  
    //构造函数及运算符重载的函数声明  
    CFraction(int nu=0,int de=1);   //构造函数,初始化用    
    void Simplify();                    //化简(使分子分母没有公因子)      
    void output();           //输出:以8/6为例,style为0时,输出8/6;    
    bool operator > (CFraction &t);    
    bool operator < (CFraction &t);    
    bool operator >= (CFraction &t);    
    bool operator <= (CFraction &t);    
    bool operator == (CFraction &t);    
    bool operator != (CFraction &t);    
    CFraction operator+(CFraction &c);     
    CFraction operator-(CFraction &c);  
    CFraction operator*(CFraction &c);    
    CFraction operator/(CFraction &c);  
    CFraction operator-();  
	friend ostream& operator << (ostream&,CFraction &);
	friend istream& operator >> (istream&,CFraction &);
      
};  
istream& operator >> (istream& input,CFraction & c)
{ 
	input>>c.nume>>c.deno;
	return input;
}
ostream& operator << (ostream& output,CFraction & c)
{
	output<<c.nume<<'/'<<c.deno<<endl; 
	return output;
}
CFraction::CFraction(int nu,int de)   //构造函数,初始化用     
{  
       if (de!=0)    
       {    
           nume=nu;    
           deno=de;    
       }    
       else    
       {    
           cerr<<"初始化中发生错误,程序退出\n";        
           exit(0);    
       }    
}  
void CFraction::Simplify()                    //化简(使分子分母没有公因子)   
{   
    int n;  
    if(nume < 0)    
    {    
        n = gcd(-nume, deno);    
    }    
    else    
    {    
        n = gcd(nume, deno);    
    }    
    
    nume = nume / n;      
    
    deno = deno / n;  
}  
// 求m,n的最大公约数    
int gcd(int m, int n)    
{    
    int r;    
    if (m<n){r=m;m=n;n=r;}    
    while(r=m%n)  // 求m,n的最大公约数    
    {    
        m=n;    
        n=r;    
    }    
    return n;    
}    

bool CFraction::operator > (CFraction &t)  
{  
    CFraction c2,c3;  
    c2.nume =nume*t.deno ;  
    c3.nume =t.nume *deno;  
    if(c2.nume >c3.nume )  
        return true;  
    else  
        return false;  
}  
bool CFraction::operator < (CFraction &t)   
{  
    CFraction c2,c3;  
    c2.nume =nume*t.deno ;  
    c3.nume =t.nume *deno;  
    if(c2.nume <c3.nume )  
        return true;  
    else  
        return false;  
}  
bool CFraction::operator >= (CFraction &t)  
{    
    CFraction c1;    
    c1.nume =nume;  
    c1.deno =deno;  
    if (c1<t)    
        return false;    
    return true;    
}    
  
bool CFraction::operator <= (CFraction &t)  
{    
    CFraction c1;    
    c1.nume =nume;  
    c1.deno =deno;  
    if (c1>t)    
        return false;    
    return true;    
}    
bool CFraction::operator == (CFraction &t)   
{  
    CFraction c1;    
    c1.nume =nume;  
    c1.deno =deno;  
    if (c1<t)    
        return false;    
    if (c1>t)    
        return false;    
    return false;  
}  
  
bool CFraction::operator != (CFraction &t)  
{  
    CFraction c1;    
    c1.nume =nume;  
    c1.deno =deno;  
    if (c1==t)    
        return false;  
    return true;  
}  
CFraction CFraction::operator+(CFraction &c)  
{  
    CFraction c2,c3,c4;  
    c2.nume =nume*c.deno ;  
    c3.nume =c.nume *deno;  
    c2.deno =deno*c.deno ;  
    c3.deno =c.deno *deno;  
    c4.nume=c2.nume +c3.nume ;  
    c4.deno =c2.deno ;  
    c4.Simplify ();  
    return c4;  
}  
CFraction CFraction::operator-(CFraction &c)  
{  
    CFraction c2,c3,c4;  
    c2.nume =nume*c.deno ;  
    c3.nume =c.nume *deno;  
    c2.deno =deno*c.deno ;  
    c3.deno =c.deno *deno;  
    c4.nume=c2.nume -c3.nume ;  
    c4.deno =c2.deno ;  
    c4.Simplify ();  
    return c4;  
}  
CFraction CFraction::operator*(CFraction &c)   
{  
    CFraction c2,c3,c4;  
    c2.nume =nume*c.nume  ;  
    c2.deno =deno*c.deno ;  
    c2.Simplify ();  
    return c2;  
}  
CFraction CFraction::operator/(CFraction &c)  
{  
    CFraction c2,c3;  
    c2.nume =c.deno ;  
    c2.deno =c.nume ;  
    c3.nume =nume*c2.nume ;  
    c3.deno =deno*c2.deno ;  
    c3.Simplify ();  
    return c3;  
}  
CFraction CFraction::operator-()  
{  
    CFraction c2;  
    c2.nume =nume;  
    c2.deno =deno;  
    c2.Simplify ();  
    if(c2.nume<0 || c2.deno<0)  
    {  
        if(c2.nume <0)  
        {  
            c2.nume =-nume;  
        }  
        else  
        {  
            c2.deno =-deno;  
        }  
    }  
    else  
    {  
        c2.nume =-nume;  
        c2.deno =deno;  
    }  
    return c2;  
}  
  
//用于测试的main()函数  
void main()    
{    
    CFraction c1,c2,c;
	cout<<"请您输入一个分数c1:(以a b的形式输入)";
	cin>>c1;
	cout<<"请您输入一个分数c1:(以a b的形式输入)";
	cin>>c2;
    cout<<"c1为:";    
    cout<<c1;    
    cout<<"c2为:";    
    cout<<c2;    
    cout<<"下面比较两个时间大小:\n";    
    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;    
    cout<<"c1+c2的数值为:";    
    c=c1+c2;    
    cout<<c;  
    cout<<endl;  
    cout<<"c1-c2的数值为:";    
    c=c1-c2;    
    cout<<c;    
    cout<<endl;    
    cout<<"c1*c2的数值为:";    
    c=c1*c2;    
    cout<<c;  
    cout<<endl;  
    cout<<"c1/c2的数值为:";    
    c=c1/c2;    
    cout<<c;   
    cout<<endl;  
    cout<<"对c1取反的结果为:";  
    c=-c1;  
    cout<<c;  
    cout<<endl;     
}      


 
* 问题描述:  

第九周实验报告3.0 

 

前三个题都是让定义《和》的运算重载,从而来实现输入,输出,进而改变程序中对运算结果的现实方式,是程序读起来更加自然。总的来说达到了预期的结果,但是我总是觉的单凭麻烦程度上两种做法差不多的啊??????