• <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

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

            一個(gè)簡(jiǎn)單的分?jǐn)?shù)類

            就是寫得太長(zhǎng)了 可以更精簡(jiǎn)些的
            #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 
            & ); //約分函數(shù)  
                  int gcd(int a,int b); //求最大公約數(shù)函數(shù)

            }
            ;  


            //類成員函數(shù)的定義 
              
            CFraction::CFraction(
            int fractionSon ,int fractionMother )  
            {  
             m_fractionSon 
            = fractionSon; //構(gòu)造函數(shù)不能有返回類型,
             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; //除以一個(gè)數(shù)等價(jià)于乘上這個(gè)數(shù)的倒數(shù)  
                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
            <<"請(qǐng)輸入分?jǐn)?shù)a的分子:"<<endl;
             cin
            >>ason;
             cout
            <<"請(qǐng)輸入分?jǐn)?shù)a的分母:"<<endl;
             cin
            >>amom;
             
            if ( amom == 0 )
             
            {
                 cout
            <<"分母不能為0!"<<endl;
                 
            return 0;
             }

             cout
            <<"請(qǐng)輸入分?jǐn)?shù)b的分子:"<<endl;
             cin
            >>bson;
             cout
            <<"請(qǐng)輸入分?jǐn)?shù)b的分母:"<<endl;
             cin
            >>bmom;
             
            if ( bmom == 0 )
             
            {
                 cout
            <<"分母不能為0!"<<endl;
                 
            return 0;
             }

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

             cout
            <<"分?jǐn)?shù) a = "<< a <<endl;  
             cout
            <<"分?jǐn)?shù) 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;  
            }
             


            轉(zhuǎn)自:http:
            //blog.csdn.net/marine_lich/archive/2008/05/15/2449778.aspx

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


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久精品国产欧美日韩| 欧美日韩精品久久免费| 97精品国产97久久久久久免费| 久久久久久亚洲精品不卡| 久久精品亚洲福利| 亚洲伊人久久成综合人影院 | 久久国产成人午夜aⅴ影院| 久久久久久亚洲精品无码| 午夜视频久久久久一区| 97r久久精品国产99国产精| 国产成人无码精品久久久久免费| 一级a性色生活片久久无| 亚洲午夜久久久久久噜噜噜| 久久综合中文字幕| 伊人久久精品无码av一区| www.久久精品| 2020国产成人久久精品| 女人香蕉久久**毛片精品| 日韩欧美亚洲综合久久 | 成人久久久观看免费毛片| 久久综合九色欧美综合狠狠| 成人免费网站久久久| 性做久久久久久免费观看| 久久亚洲国产午夜精品理论片| 国产毛片欧美毛片久久久| 久久国产免费直播| 曰曰摸天天摸人人看久久久| 91久久精品91久久性色| 99精品久久精品一区二区| 久久人妻少妇嫩草AV无码蜜桃| 精品久久久久中文字幕一区| 久久99精品国产99久久| 欧美一区二区精品久久| 亚洲国产精品久久电影欧美| 久久亚洲精品国产精品| 久久精品国产亚洲欧美| 国产精品禁18久久久夂久| 国产精品欧美久久久久天天影视| 久久99国产亚洲高清观看首页| 久久婷婷五月综合色高清| 精品熟女少妇av免费久久|