/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱:
* 作 者: 苗影
* 完成日期: 2012 年 4 月 10 日
* 版 本 號:
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:
* 程序輸出:
* 程序頭部的註釋結束
*/
#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();
void display();
CFraction operator+( CFraction &c); //兩個分數相加,結果要化簡
CFraction operator-( CFraction &c); //兩個分數相減,結果要化簡
CFraction operator*( CFraction &c); //兩個分數相乘,結果要化簡
CFraction operator/( CFraction &c); //兩個分數相除,結果要化簡
CFraction operator+(); //取正一目運算
CFraction operator-(); //取反一目運算
bool operator>(CFraction &c);
bool operator<( CFraction &c);
bool operator==( CFraction &c);
bool operator!=( CFraction &c);
bool operator>=( CFraction &c);
bool operator<=( CFraction &c);
};
void CFraction::display()
{
cout<<"("<<nume<<"/"<<deno<<")";
}
void CFraction::simplify()
{
int h,m,i,j;
for(i=2;i<=6;i++)
{
{
h=nume%i;
m=deno%i;
}
while(h==0&&m==0)
{ j=i;
nume=nume/j;
deno=deno/j;
h=nume%i;
m=deno%i;
}
}
cout<<nume<<"/"<<deno<<endl;
}
CFraction CFraction::operator+( CFraction &c)
{
CFraction t;
t.nume=nume*c.deno+c.nume*deno;
t.deno=deno*c.deno;
return t;
}
CFraction CFraction:: operator-( CFraction &c)
{
CFraction t;
t.nume=nume*c.deno-c.nume*deno;
t.deno=deno*c.deno;
return t;
}
CFraction CFraction:: operator/( CFraction &c)
{
CFraction t;
t.nume=nume*c.nume;
t.deno=deno*c.deno;
return t;
}
CFraction CFraction:: operator*( CFraction &c)
{
CFraction t;
t.nume=nume*c.deno;
t.deno=deno*c.nume;
return t;
}
CFraction CFraction:: operator+()
{ CFraction t;
nume=nume;
deno=deno;
return t;
}
CFraction CFraction:: operator-()
{
CFraction t;
t.nume=-nume;
t.deno=deno;
return t;
}
bool CFraction::operator>( CFraction &c)
{
int t,g,h;
t=nume*c.deno;
g=c.nume*deno;
h=deno*c.deno;
if(t>g&&h>0||t<g&&h<0)
return true;
else return false;
}
bool CFraction::operator<( CFraction &c)
{
int t,g,h;
t=nume*c.deno;
g=c.nume*deno;
h=deno*c.deno;
if(t<g&&h>0||t>g&&h<0)
return true;
else return false;
}
bool CFraction::operator==( CFraction &c)
{
int t,g,h;
t=nume*c.deno;
g=c.nume*deno;
h=deno*c.deno;
if(t==g)
return true;
else return false;
}
bool CFraction::operator!=( CFraction &c)
{
int t,g,h;
t=nume*c.deno;
g=c.nume*deno;
h=deno*c.deno;
if(t!=g)
return true;
else return false;
}
bool CFraction::operator>=( CFraction &c) //這種方法很方便哦
{
if (*this<c) return false;
else return true;
}
bool CFraction::operator<=( CFraction &c)
{
if (*this>c) return false;
else return true;
}
void main()
{
CFraction x(3,4),y(2,6),s;
cout<<"分數x=3/4 y=2/6"<<endl;
s=x+y;
cout<<"x+y=";
s.simplify();
cout<<endl;
s=x-y;
cout<<"x-y=";
s.simplify();
cout<<endl;
s=x*y;
cout<<"x*y=";
s.simplify();
cout<<endl;
s=x/y;
cout<<"x/y=";
s.simplify();
cout<<endl;
if(x>y)
{
x.display();
cout<<">";
y.display ();
cout<<endl;
}
if(x<y)
{
x.display();
cout<<"<";
y.display ();
}
if(x==y)
{
x.display();
cout<<"=";
y.display ();
}
if(x!=y)
{
x.display();
cout<<"!=";
y.display ();
cout<<endl;
}
if(x<=y)
{
x.display();
cout<<"<=";
y.display ();
}
cout<<endl;
s=-x;
cout<<"(3/4)的相反數是";
s.display();
cout<<endl;
system("pause");
}
經驗積累:在編>=的程序時可以用<的判斷。
把函數的化簡定以爲一個函數。
在比較時x是調用函數的,不能直接寫x.