现在的位置: 首页 > 综合 > 正文

分数计数器 C++

2013年08月15日 ⁄ 综合 ⁄ 共 6434字 ⁄ 字号 评论关闭

/*创建一个工程:里面包括一个主函数,两个头文件如下*/

//主函数

Code:
  1. #include "utility.h"   
  2. #include "fraction.h"   
  3.   
  4. int main()   
  5. {   
  6.     try  
  7.     {   
  8.         Fraction result;   
  9.         Fraction cur;   
  10.         char select;   
  11.         cout<<"输入一个分数:";   
  12.         cin>>result;   
  13.         do  
  14.         {   
  15.             cout<<"当前结果:"<<result<<endl;   
  16.             do  
  17.             {   
  18.                 cout<<"请选择[加、减、乘、除、等于、清零和退出(+、-、*、/、=、C、X)]:";   
  19.                 select=GetChar();   
  20.                 while (cin.get()!='/n');   
  21.                 select=toupper(select);   
  22.             }while (select!='+'&&select!='-'&&select!='*'&&select!='/'&&   
  23.                 select!='='&&select!='C'&&select!='X');   
  24.             switch(select)   
  25.             {   
  26.             case '+':   
  27.                 cout<<"输入一个分数:";   
  28.                 cin>>cur;   
  29.                 result=result+cur;   
  30.                 break;   
  31.             case '-':   
  32.                 cout<<"输入一个分数:";   
  33.                 cin>>cur;   
  34.                 result=result-cur;   
  35.                 break;   
  36.             case '*':   
  37.                 cout<<"输入一个分数:";   
  38.                 cin>>cur;   
  39.                 result=result*cur;   
  40.                 break;   
  41.             case '/':   
  42.                 cout<<"输入一个分数:";   
  43.                 cin>>cur;   
  44.                 result=result/cur;   
  45.                 break;   
  46.             case '=':   
  47.                 break;   
  48.             case 'C':   
  49.                 result=0;   
  50.                 break;   
  51.             }   
  52.         }while (select!='X');   
  53.     }   
  54.   
  55.         catch (Error err)   
  56.         {   
  57.             err.Show();   
  58.         }   
  59.        
  60.   
  61.     return 0;   
  62. }   

//头文件utility.h

Code:
  1. #include <iostream>   
  2. #ifndef _ _UTILITY_H_ _       
  3. #define _ _UTILITY_H_ _      
  4. #include <time.h>   
  5. using namespace std;   
  6.   
  7. bool UserSaysYes();   
  8.   
  9.   
  10. template <class ElemType>   
  11. void Swap(ElemType &e1,ElemType &e2);   
  12. template<class ElemType>   
  13. void Display(ElemType elem[],int n);   
  14.   
  15.   
  16. class Timer;   
  17. class Error;   
  18. class Rand;   
  19.   
  20. char GetChar(istream &in=cin)   
  21. {   
  22.     char ch;   
  23.     while ((ch=in.peek())!=EOF   
  24.         &&((ch=in.get())==' '  
  25.         ||ch=='/t'));   
  26.     return ch;   
  27. }   
  28.   
  29. bool UserSaysYes()   
  30. {   
  31.     char ch;   
  32.     bool initialResponse=true;   
  33.   
  34.     do    
  35.     {   
  36.         if(initialResponse)    
  37.             cout<<"(y,n)?";   
  38.         else    
  39.             cout<<"用y或n回答:";   
  40.         while((ch=GetChar())=='/n');   
  41.         initialResponse=false;   
  42.     }while (ch!='y'&&ch!='Y'&&ch!='n'&&ch!='N');   
  43.     while (GetChar() !='/n');   
  44.   
  45.     if (ch=='y'||ch=='Y')   
  46.         return true;   
  47.     else    
  48.         return false;   
  49. }   
  50.   
  51. template <class ElemType>   
  52. void Swap(ElemType &e1,ElemType &e2)   
  53. {   
  54.     ElemType temp;   
  55.     temp=e1;e1=e2;e2=temp;   
  56. }   
  57.   
  58. template<class ElemType>   
  59. void Show(ElemType elem[],int n)   
  60. {   
  61.     for(int i=0;i<n;i++)   
  62.     {   
  63.         cout<<elem[i]<<" ";   
  64.     }   
  65.     cout<<endl;   
  66. }   
  67.   
  68.   
  69.   
  70.   
  71. //计时器类Timer   
  72. class Timer   
  73. {   
  74. private:   
  75.     clock_t startTime;   
  76. public:   
  77.     Timer(){startTime=clock();}   
  78.     double ElapsedTime() const  
  79.     {   
  80.         clock_t endTime=clock();   
  81.         return (double)(endTime-startTime)/(double)CLK_TCK;   
  82.     }   
  83.     void Reset(){startTime=clock();}   
  84. };   
  85.   
  86.   
  87. #define MAX_ERROR_MESSAGE_LEN 100   
  88. class Error   
  89. {   
  90. private:   
  91.     char message [MAX_ERROR_MESSAGE_LEN];   
  92.   
  93. public:   
  94.     Error(char mes[]="一般性异常!"){strcpy(message,mes);}   
  95.     void Show() const {cout<<message<<endl;}   
  96. };   
  97.   
  98.   
  99. //随机数类Rand   
  100. class Rand   
  101. {   
  102. public:   
  103.     static void SetRandSeed() {srand((unsigned)time(NULL));}//设置当前时间为随机数种子   
  104.     static int GetRand(int n){return rand()%n;}   
  105.     static int GetRand(){return rand();}   
  106. };   
  107.   
  108. #endif  

//头文件faction.h

Code:
  1. #include <iostream>   
  2. #include <cmath>   
  3. //#ifndef _ _FRACTION_H_ _   
  4. #define _ _FRACTION_H_ _   
  5. using namespace std;   
  6.   
  7. class Fraction   
  8. {   
  9. private:   
  10.     int nume;   
  11.     int deno;   
  12.     int Gcf(int m,int n);   
  13.   
  14.   
  15. public:   
  16.     Fraction(int n=1,int d=1);   
  17.     virtual ~Fraction(){}   
  18.     void Reduction();   
  19.     int GetNume() const{return nume;}   
  20.     int GetDeno() const{return deno;}   
  21.     void SetNume(int n);   
  22.     void SetDeno(int d);   
  23.     Fraction operator+ (const Fraction &a) const;   
  24.     Fraction operator- (const Fraction &a) const;   
  25.     Fraction operator* (const Fraction &a) const;   
  26.     Fraction operator/ (const Fraction &a) const;   
  27. };   
  28.     ostream &operator<<(ostream &out,const Fraction &a);   
  29.     istream &operator>>(ostream &in,const Fraction &a);   
  30.   
  31.   
  32. int Fraction::Gcf(int m,int n)   
  33. {   
  34.     if(n==0) return m;   
  35.     else return Gcf(n,m%n);   
  36. }   
  37.   
  38.   
  39. void Fraction::Reduction()   
  40. {   
  41.     if(deno<0) {nume=-nume;deno=-deno;}   
  42.     int f=Gcf(abs(nume),deno);   
  43.     nume=nume/f;   
  44.     deno=deno/f;   
  45. }   
  46.   
  47. Fraction::Fraction(int n,int d):nume(n),deno(d)   
  48. {   
  49.     if(deno==0) throw Error("分母为0");   
  50.     deno=d;   
  51.     Reduction();   
  52. }   
  53.   
  54. void Fraction::SetNume(int n)   
  55. {   
  56.     nume=n;   
  57.     Reduction();   
  58. }   
  59.   
  60. void Fraction::SetDeno(int d)   
  61. {   
  62.     if(d==0) throw Error("分母为0");   
  63.     deno=d;   
  64.     Reduction();   
  65. }   
  66.   
  67.   
  68.   
  69. Fraction Fraction::operator+(const Fraction &a) const  
  70. {   
  71.     Fraction result;   
  72.   
  73.     result.nume=this->nume*a.deno+this->deno*a.nume;   
  74.     result.deno=this->deno*a.deno;   
  75.     result.Reduction();   
  76.     return result;   
  77. }   
  78.   
  79. Fraction Fraction::operator- (const Fraction &a) const  
  80. {   
  81.     Fraction result;   
  82.     result.nume=this->nume*a.deno-this->deno*a.nume;   
  83.     result.deno=this->deno*a.deno;   
  84.     result.Reduction();   
  85.     return result;   
  86. }   
  87. Fraction Fraction::operator* (const Fraction &a) const  
  88. {   
  89.     Fraction result;   
  90.   
  91.     result.nume=this->nume*a.nume;   
  92.     result.deno=this->deno*a.deno;   
  93.     result.Reduction();   
  94.     return result;   
  95. }   
  96.   
  97.   
  98. Fraction Fraction::operator/ (const Fraction &a) const  
  99. {   
  100.     if(a.nume==0)throw Error("除数为0!");   
  101.   
  102.   
  103.     Fraction result;   
  104.   
  105.     result.nume=this->nume*a.deno;   
  106.     result.deno=this->deno*a.nume;   
  107.     result.Reduction();   
  108.     return result;   
  109. }   
  110.   
  111. ostream &operator<<(ostream &out,const Fraction &a)   
  112. {   
  113.     out<<a.GetNume()<<"/"<<a.GetDeno();   
  114.     return out;   
  115. }   
  116.   
  117. istream &operator>>(istream &in,Fraction &a)   
  118. {   
  119.     char ch;   
  120.     int m,n;   
  121.        
  122.     ch=GetChar(in);   
  123.     if(ch!='+'&&ch!='-'&&!isdigit(ch))   
  124.         throw Error("非法字符");   
  125.     else    
  126.     {   
  127.         in.putback(ch);   
  128.         in>>m;   
  129.         ch=GetChar(in);   
  130.         if(ch!='/'throw Error("非法字符");   
  131.         ch=GetChar(in);   
  132.         if(ch!='+'&&ch!='-'&&!isdigit(ch))   
  133.             throw Error("非法字符");   
  134.         in.putback(ch);   
  135.         in>>n;   
  136.         if(n==0) throw Error("分母为0");   
  137.     }   
  138.     while(in.get()!='/n');   
  139.   
  140.     a.SetNume(m);   
  141.     a.SetDeno(n);   
  142.     a.Reduction();   
  143.        
  144.     return in;   
  145. }   
  146.   
  147.   
  148. //#endif   

 

【上篇】
【下篇】

抱歉!评论已关闭.