• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            The Fourth Dimension Space

            枯葉北風寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢令

            一個簡單的分數類

            就是寫得太長了 可以更精簡些的
            #include <iostream>  
            using namespace std; 
            class CFraction 
            {  
               
            private:  
                  
            int m_fractionSon; //分子  
                  int m_fractionMother; //分母  

                  friend ostream 
            &operator<<(ostream &output,const CFraction &fraction)  
                  
            {  
                     output
            <<fraction.m_fractionSon<<"/"<<fraction.m_fractionMother<<endl;  
                     
            return output;  
                  }

              
                  friend istream 
            &operator>>(istream &input, CFraction &fraction)  
                  
            {  
                     input
            >>fraction.m_fractionSon;  
                     input
            >>fraction.m_fractionMother;  
                     
            return input;  
                  }
             

               
            public:
                  CFraction(
            int=1 ,int=1 );    
                  
            const CFraction operator+const CFraction & );  
                  
            const CFraction operator-const CFraction & );  
                  
            const CFraction operator*const CFraction & );  
                  
            const CFraction operator/const CFraction & );   
                  
            bool operator<const CFraction & );  
                  
            bool operator>const CFraction & );  
                  
            bool operator==const CFraction & );  
                  
            bool operator!=const CFraction & );    
                  CFraction Easiest(CFraction 
            & ); //約分函數  
                  int gcd(int a,int b); //求最大公約數函數

            }
            ;  


            //類成員函數的定義 
              
            CFraction::CFraction(
            int fractionSon ,int fractionMother )  
            {  
             m_fractionSon 
            = fractionSon; //構造函數不能有返回類型,
             m_fractionMother = fractionMother;
            }
             

            const CFraction CFraction::operator+const CFraction &rightNumber )  

               CFraction resultValue;  
               resultValue.m_fractionSon 
            = m_fractionSon * rightNumber.m_fractionMother + m_fractionMother * rightNumber.m_fractionSon;  
               resultValue.m_fractionMother 
            = m_fractionMother * rightNumber.m_fractionMother;  
               
            return Easiest( resultValue );  
            }
             

            const CFraction CFraction::operator-const CFraction &rightNumber )  

                CFraction resultValue;  
                resultValue.m_fractionSon 
            = m_fractionSon * rightNumber.m_fractionMother - m_fractionMother * rightNumber.m_fractionSon;  
                resultValue.m_fractionMother 
            = m_fractionMother * rightNumber.m_fractionMother; 
                
            return Easiest( resultValue );  
            }


            const CFraction CFraction:: operator*(const CFraction &rightNumber )  

                CFraction resultValue;  
                resultValue.m_fractionSon 
            = m_fractionSon * rightNumber.m_fractionSon;  
                resultValue.m_fractionMother 
            = m_fractionMother * rightNumber.m_fractionMother;  
                
            return Easiest( resultValue );  
            }
              

            const CFraction CFraction::operator/const CFraction &rightNumber )  

                CFraction resultValue;  
                resultValue.m_fractionSon 
            = m_fractionSon * rightNumber.m_fractionMother;  
                resultValue.m_fractionMother 
            = m_fractionMother * rightNumber.m_fractionSon; //除以一個數等價于乘上這個數的倒數  
                return Easiest( resultValue );  
            }


            bool CFraction::operator<const CFraction &rightNumber ) 
            {  
              
            int leftNumberFractionSon,rightNumberFractionSon;
              leftNumberFractionSon 
            = m_fractionSon * rightNumber.m_fractionMother;
              rightNumberFractionSon 
            = rightNumber.m_fractionSon * m_fractionMother;
              
            if ( leftNumberFractionSon < rightNumberFractionSon )
              
            {
                  
            return true;
              }
             
              
            else
              
            {
                  
            return false;
              }

            }
              

            bool CFraction::operator>const CFraction &rightNumber)  
            {  
              
               
            int leftNumberFractionSon,rightNumberFractionSon;
               leftNumberFractionSon 
            = m_fractionSon * rightNumber.m_fractionMother;
               rightNumberFractionSon 
            = rightNumber.m_fractionSon * m_fractionMother;
               
            if ( leftNumberFractionSon > rightNumberFractionSon )
               
            {
                  
            return true;
               }
             
               
            else
               
            {
                  
            return false;
               }


            }
             

            bool CFraction::operator==const CFraction &rightNumber ) 
            {  
                
            int leftNumberFractionSon,rightNumberFractionSon;
                leftNumberFractionSon 
            = m_fractionSon * rightNumber.m_fractionMother;
                rightNumberFractionSon 
            = rightNumber.m_fractionSon * m_fractionMother;
                
            if ( leftNumberFractionSon == rightNumberFractionSon )
                
            {
                  
            return true;
                }
             
                
            else
                
            {
                  
            return false;
                }

              
            }
             

            CFraction CFraction::Easiest( CFraction 
            &fraction )  
            {  
               
            int getgcd = gcd(fraction.m_fractionSon, fraction.m_fractionMother);
               CFraction value(fraction.m_fractionSon
            /getgcd, fraction.m_fractionMother/getgcd);
               
            return value;
            }
               

            int CFraction::gcd(int a,int b)
            {
             
            while(b)
             
            {
              
            int temp = b;
              b 
            = a%b;
              a 
            = temp;
             }

             
            return a;
            }

             
            int main()
            {  
             
            int ason, amom, bson, bmom;
             cout
            <<"請輸入分數a的分子:"<<endl;
             cin
            >>ason;
             cout
            <<"請輸入分數a的分母:"<<endl;
             cin
            >>amom;
             
            if ( amom == 0 )
             
            {
                 cout
            <<"分母不能為0!"<<endl;
                 
            return 0;
             }

             cout
            <<"請輸入分數b的分子:"<<endl;
             cin
            >>bson;
             cout
            <<"請輸入分數b的分母:"<<endl;
             cin
            >>bmom;
             
            if ( bmom == 0 )
             
            {
                 cout
            <<"分母不能為0!"<<endl;
                 
            return 0;
             }

             CFraction a(ason,amom);  
             CFraction b(bson,bmom);  

             cout
            <<"分數 a = "<< a <<endl;  
             cout
            <<"分數 b = "<< b <<endl;   
                
             cout
            <<"a+b="<< a+<<endl;  
             cout
            <<"a-b="<< a-<<endl;  
             cout
            <<"a*b="<< a*<<endl;  
             cout
            <<"a/b="<< a/<<endl;  
              
             
            if(a>b)  
             cout
            <<"a大于b"<<endl;  
             
            else if (a<b)  
             cout
            <<"a小于b."<<endl;   
             
            else if(a==b)  
             cout
            <<"a等于b."<<endl;  
             
            return 0;  
            }
             


            轉自:http:
            //blog.csdn.net/marine_lich/archive/2008/05/15/2449778.aspx

            posted on 2010-04-16 18:09 abilitytao 閱讀(243) 評論(0)  編輯 收藏 引用

            久久99久久99精品免视看动漫| 久久国产香蕉一区精品| 久久这里只有精品18| 国产成人久久精品一区二区三区| 99久久精品午夜一区二区| 国产AⅤ精品一区二区三区久久| 久久精品国产亚洲Aⅴ香蕉| 中文字幕亚洲综合久久菠萝蜜| 亚洲综合熟女久久久30p| 中文字幕亚洲综合久久2| 少妇人妻综合久久中文字幕| 久久久久免费精品国产| 久久午夜免费视频| 久久国产成人| 久久久久久久综合日本亚洲| 久久久国产视频| 久久久精品国产亚洲成人满18免费网站 | 99久久精品免费看国产一区二区三区 | 伊人久久大香线蕉精品不卡 | 久久久久国产日韩精品网站 | 婷婷国产天堂久久综合五月| 99久久精品毛片免费播放| 伊人久久大香线蕉精品不卡| 韩国三级中文字幕hd久久精品 | 青草国产精品久久久久久| 日本加勒比久久精品| 国产午夜精品久久久久九九| 国产成人久久精品一区二区三区| 狠狠综合久久AV一区二区三区| 久久精品国产黑森林| 国产午夜精品久久久久九九| 亚洲成人精品久久| 青青草原1769久久免费播放| 91精品国产综合久久久久久| 日本人妻丰满熟妇久久久久久| 精品国产日韩久久亚洲| 亚洲天堂久久久| 久久亚洲AV无码精品色午夜| 亚洲综合久久久| 亚洲午夜无码久久久久| 久久夜色精品国产噜噜亚洲AV|