分数除法

//fraction.h

 

#include"utility.h"

#include<iostream.h>

class Fraction

{

private:

         int nume;

         int deno;

         int Gcf(int m,int n);

public:

         Fraction(int n=1,int d=1);

         virtual ~Fraction(){};

         bool dight(char ch);

         void Reduction();

         int GetNume(){return nume;}

         int GetDeno(){return deno;}

         void SetNume(int n);

         void SetDeno(int d);

         Fraction operator +(Fraction&a);

         Fraction operator -(Fraction&a);

         Fraction operator *(Fraction&a);

         Fraction operator /(Fraction&a);

         friend ostream& operator <<(ostream&out,Fraction&a);

         friend istream& operator >>(istream&out,Fraction&a);

};

int Fraction::Gcf(int m,int n)

{

         if(n==0)

                   return m;

         else

                   return Gcf(n,m%n);

}

Fraction::Fraction(int n,int d)

{

         nume=(n);

         deno=(d);

         if(deno==0)

                   throw Error("分母为0");

         Reduction();

}

bool Fraction::dight(char ch)

{

         if(ch>='1')

                   return true;

         else

                   return false;

}

 

void Fraction::Reduction()

{

         if(deno<0)

         {

                   nume=-nume;

                   deno=-deno;

         }

         int f=Gcf(abs(nume),deno);

         nume=nume/f;

         deno=deno/f;

}

void Fraction::SetNume(int n)

{

         nume=n;

         Reduction();

}

 

 

void Fraction::SetDeno(int d)

{

         deno=d;

         Reduction();

}

Fraction Fraction::operator +(Fraction&a)

{

         Fraction result;

         result.nume=nume*a.deno+a.nume*deno;

         result.deno=deno*a.deno;

         result.Reduction();

         return result;

}

Fraction Fraction::operator -(Fraction&a)

{

         Fraction result;

         result.nume=nume*a.deno-a.nume*deno;

         result.deno=deno*a.deno;

         result.Reduction();

         return result;

        

}

Fraction Fraction::operator *(Fraction&a)

{

         Fraction result;

         result.nume=nume*a.nume;

         result.deno=deno*a.deno;

         result.Reduction();

         return result;

        

}

Fraction Fraction::operator /(Fraction&a)

{

         if(a.nume==0)

                   throw Error("除数为零");

         Fraction result;

         result.nume=nume*a.deno;

         result.deno=deno*a.nume;

         result.Reduction();

         return result;

}

ostream& operator <<(ostream&out,Fraction&a)

{

         out<<a.GetNume()<<"/"<<a.GetDeno();

         return out;

}

istream& operator >>(istream&in,Fraction&a)

{

         char ch;

         int m,n;

        

         in>>m;

         ch=GetChar();

         if(ch!='/')

                   throw Error("非法字符");

         in>>n;

         if(n==0)

                   throw Error("分母为零");

         while(in.get()!='\n');

         a.SetDeno(n);

         a.SetNume(m);

         a.Reduction();

         return in;

}

//main.cpp

 

#include"fraction.h"

void main()

{

         try

         {

                   Fraction result;

                   Fraction cur;

                   char select;

                   cout<<"输入一个分数:";

                   cin>>result;

                   do

                   {

                            cout<<"当前结果:"<<result<<endl;

                            do

                            {

                                     cout<<"请选择【加,减,乘,除,等于,清零和退出(+,-,*,/,=,C,X)】:";

                                     select=GetChar();

                                     while(cin.get()!='\n');

                                     select=toupper(select);

                            }while(select!='+'&&select!='-'&&select!='*'&&select!='/'&&select!='='&&select!='C'&&select!='X');

                            switch(select)

                            {

                            case'+':

                                     cout<<"输入一个分数:";

                                     cin>>cur;

                                     result=result+cur;

                                     break;

                            case '-':

                                     cout<<"输入一个分数:";

                                     cin>>cur;

                                      result=result-cur;

                                     break;

                            case '*':

                                     cout<<"输入一个分数:";

                                     cin>>cur;

                                result=result*cur;

                                     break;

                            case '/':

                                     cout<<"输入一个分数:";

                                     cin>>cur;

                                     result=result/cur;

                                     break;

                            case '=':

                                     break;

                            case 'C':

                                     result=0;

                                     break;

                            }

                   }while(select!='X');

         }

         catch(Error err)

         {

                   err.Show();

         }

         system("pause");

}

 

posted @ 2012-11-22 23:05  ♂咱說 ろ算  阅读(244)  评论(0编辑  收藏  举报