源程序:
/*(文件註釋頭部開始)
*程序的版權和版本聲明部分
*Copyright (c) 2011,煙臺大學計算機學院學生
*All rights reserved.
*文件名稱:分數類流運算符的重載
*作 者:2011級計114-3張宗佳
*完成日期:2011年4月17號
*版本號:vc
* 對任務及求解方法的描述部分
* 輸入描術:輸入兩個分數
* 問題描述:定義一個分數類重載運算符+、-、*、/ << >>,使之能用於分數的加減乘除與輸入輸出。
* 程序輸出:輸出分數的加減乘除
* 程序頭部的註釋結束
*/
#include <iostream>
using namespace std;
class CFraction
{private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //構造函數,初始化用
void Set(int nu=0,int de=1); //置值,改變值時用
void Simplify(); //化簡(使分子分母沒有公因子)
CFraction operator+(const CFraction &c);//分數相加
CFraction operator-(const CFraction &c);//分數相減
CFraction operator*(const CFraction &c);//分數相乘
CFraction operator/(const CFraction &c);//分數相除
CFraction operator+();//取正運算
CFraction operator-();//取反運算
friend istream & operator >> (istream &input,CFraction &c);
friend ostream & operator << (ostream &output,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);
bool operator<=(const CFraction &c);
};
CFraction::CFraction(int nu,int de)
{
nume=nu;
deno=de;
}
void CFraction::Set(int nu,int de)
{
nume=nu;
deno=de;
}
istream & operator >> (istream &input,CFraction &c)
{
cout << "輸入分子和分母:";
input >> c.nume >> c.deno;
return input;
}
ostream & operator << (ostream &output,CFraction &c)
{
output << c.nume << "/" << c.deno << endl;
return output;
}
void CFraction::Simplify()
{
int a,b,c,p;
if(nume<=deno)
{
p=nume;
}
else
p=deno;
for(a=2;a<=p;a++)
{
b=nume%a;
c=deno%a;
if(b==0&&c==0)
{
do
{
nume=nume/a;
deno=deno/a;
b=nume%a;
c=deno%a;
}while(b==0&&c==0);
}
}
}
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;
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.Simplify();
return t;
}
CFraction CFraction::operator+()//取正運算
{
return *this;
}
CFraction CFraction::operator-()//取反運算
{
CFraction t;
t.nume=-nume;
t.deno=deno;
return t;
}
//比較運算
bool CFraction::operator>(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume >t2.nume )
return true;
else
return false;
}
bool CFraction::operator<(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume <t2.nume )
return true;
else
return false;
}
bool CFraction::operator==(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume ==t2.nume )
return true;
else
return false;
}
bool CFraction::operator!=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume ==t2.nume )
return false;
else
return true;
}
bool CFraction::operator>=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume <t2.nume )
return false;
else
return true;
}
bool CFraction::operator<=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume >t2.nume )
return false;
else
return true;
}
void main()
{
CFraction c1,c2,c3;
cin >> c1 >> c2;
cout << "c1=" << c1;
cout << "c2=" << c2;
c3 = c1 + c2;
cout << "c1+c2=" << c3;
c3 = c1 - c2;
cout << "c1-c2=" << c3;
c3 = c1 * c2;
cout << "c1*c2=" << c3;
c3 = c1 / c2;
cout<<"c1/c2=" << c3;
c3 = -c1;
cout << "-c1=" << c3;
cout << "兩分數比較得:" << 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;
system("pause");
}
實驗結果:
上機感言:
終於傳上了,快無語了,改了n便,沒辦法...關於流運算符的重載問題,還是很好用的,只是其他運算符的輸出問題嘛...還是有點鬱悶(僅對於這個程序來說...)