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

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

            帶括號浮點型計算器final

            課程設計一:                                                                               計算器

             

                                                        選題難以級別:A

             

            1.1 程序目的

            實現(xiàn)計算器的功能。用戶根據(jù)程序的提示,輸入數(shù)字,選擇要進行的計算,可以進行加、減、乘、除運算,簡單的四則運算及其測試,開平方,華氏溫度與攝氏溫度之間的相互轉換等運算。

             

            1.2 程序內容

            (1)           完善計算器程序,改為可對實數(shù)操作。

            (2)           完善程序,改為多個操作數(shù)基本四則運算,遇到0為止。

            (3)           增加函數(shù),完成四則混合運算,注意算法要考慮運算符的優(yōu)先級,增加相應的主菜單選項。

            (4)           添加語句,使四則運算具有測試功能。使四則運算具有測試功能,即計算機能夠自動出題,并要求用戶計算的對錯打分,要求十題為一個單元,加減運算時最高為兩位數(shù)運算,乘法為兩位數(shù)乘一位數(shù),除法是兩位數(shù)或三位數(shù)除一位數(shù),且要求沒有余數(shù)。

            (5)           可擴充其功能。

            1.3 源代碼

            //Copyright by abilitytao
            //All right not reserved!!
            #include <process.h>
            #include 
            <iostream.h>
            #include 
            <conio.h>
            #include 
            <stdlib.h>
            #include 
            <math.h>
            #include 
            <stack>//添加的頭文件,用于進行后綴表達式計算操作;
            #include <cstring>//添加的頭文件,用于字符串處理;
            #include <ctype.h>//添加的頭文件,僅僅為了使用isdigit函數(shù);
            using namespace std;

            struct formula
            {
                
            char numordigit[100];
            };



            ///////////////////////////////////////////////////////////////////////////////////
            int formulalen;
            char temp[1000];
            formula 
            in[1000];
            formula pos[
            1000];
            double number1,number2,answer,val;
            ///////////////////////////////////////////////////////////////////////////////////
            class oopcalc 
            {
                
            public:
                
            void calcadd();    
                
            void calcsub();
                
            void calcdiv();
                
            void calcmult();
                
            void calcfartocel();
                
            void calcceltofar();
                
            void calcsroot();
                
            void calcmixed();//添加函數(shù),用于混合四則運算;
                void test();//添加函數(shù),用于測試;
                void exitprog();
                
            void menu();
                
            void badinput();
                
            private:
                
            double add(double x, double y);
                
            double sub(double x, double y);
                
            double div(double x, double y);
                
            double mult(double x, double y);
                
            double fartocel(double x);
                
            double celtofar(double x);
                
            double sqroot(double x);
                
            void intoin();//添加函數(shù),用于將中綴表達式放入結構體數(shù)組,從而實現(xiàn)數(shù)字與運算符的分離;
                void intopos();//添加函數(shù),用于將中綴表達式轉化成后綴表達式;
                double calculate();//添加函數(shù),用于計算后綴表達式;
                };
                
            void oopcalc::calcadd()
                {
                cout 
            << "The Add Function\n";
                cout 
            << "First number: ";
                cin 
            >> number1;
                cout 
            << "Second number: ";
                cin 
            >> number2;
                answer 
            = add(number1,number2);
                cout 
            << number1 << " + " << number2 << " = " << answer << endl;
                cout 
            <<"===============================================================================\n";
                cerr
            << "Press any key to continue\n";
                getch();
                menu();
                }
                
            void oopcalc::calcsub()
                {
                    cout 
            << "The Subtract Function\n";
                    cout 
            << "First number: ";
                    cin 
            >> number1;
                    cout 
            << "Second number: ";
                    cin 
            >> number2;
                    answer 
            = sub(number1,number2);
                    cout 
            << number1 << " - " << number2 << " = " << answer << endl;
                    cout 
            <<"===============================================================================\n";
                    cerr 
            << "Press any key to continue\n";
                    getch();
                    menu();
                }
                
            void oopcalc::calcdiv()
                {
                cout 
            << "The Divide Function\n";
                cout 
            << "First number: ";
                cin 
            >> number1;
                cout 
            << "Second number: ";
                cin 
            >> number2;
                answer 
            = div(number1,number2);
                cout 
            << number1 << " / " << number2 << " = " << answer << endl;
                cout 
            <<"===============================================================================\n";
                cerr 
            << "Press any key to continue\n";
                getch();
                menu();
                }
                
            void oopcalc::calcmult()
                {
                cout 
            << "The Multiply Function\n";
                cout 
            << "First number: ";
                cin 
            >> number1;
                cout 
            << "Second number: ";
                cin 
            >> number2;
                answer 
            = mult(number1,number2);
                cout 
            << number1 << " x " << number2 << " = " << answer << endl;
                cout 
            <<"===============================================================================\n";
                cerr 
            << "Press any key to continue\n";
                getch();
                menu();
                }
                
            void oopcalc::calcfartocel()
                {
                cout 
            << "The Farenheit to Celsius Function\n";
                cout 
            << "Enter a tempature in Farenheit: ";
                cin 
            >> number1;
                answer 
            = fartocel(number1);
                cout 
            << "The tempature in Celsius is " << answer << endl;
                cout 
            <<"===============================================================================\n";
                cerr 
            << "Press any key to continue\n";
                getch();
                menu();
                }
                
            void oopcalc::calcceltofar()
                {
                cout 
            << "The Celsius to Farenheit Function\n";
                cout 
            << "Enter a tempature in Celsius: ";
                cin 
            >> number1;
                answer 
            = celtofar(number1);
                cout 
            << "The tempature in Farenheit is " << answer << endl;
                cout 
            <<"===============================================================================\n";
                cerr 
            << "Press any key to continue\n";
                getch();
                menu();
                }
                
            void oopcalc::calcsroot()
                    {
                    cout 
            << "The Square Root Function\n";
                    cout 
            << "First number: ";
                    cin 
            >> number1;
                    answer 
            = sqroot(number1);
                    cout 
            << "The square root of " << number1 << " is " << answer << endl;
                    cout 
            <<"===============================================================================\n";
                    cerr 
            << "Press any key to continue\n";
                    getch();
                    menu();
                    }
                
            void oopcalc::calcmixed()
                    {
                    cout
            <<"Please Input The Formula(You Can Use The '(' And ')'As Well)\n";
                    intoin();
                    intopos();
                    
            int i;
                    
            for(i=0;i<formulalen;i++)
                        cout
            <<in[i].numordigit;
                    cout
            <<'='<<calculate()<<endl;
                    cout 
            <<"===============================================================================\n";
                    cerr 
            << "Press any key to continue\n";
                    getch();
                    menu();
                    }
                
            void oopcalc::test() //添加內容   簡單四則運算的測試
                {
                cout
            <<"簡單四則運算的測試\n:" ;
                
            int i,a,b,c,e,g,h,m,sum=0 ;
                
            for(i=0;i<10;i++)
                { 
                    a
            =rand()%100 ;  //應用的函數(shù)rand()產生所需的隨機數(shù)
                    b=rand()%100 ;
                    c
            =rand()%10 ;
                    g
            =a*c;  //先使g=a*c,然后在case'3'中g/c以獲得沒有余數(shù)的除法
                    m=rand()%4 ;
                    
            switch((int)m)
                    {
                    
            case 0 :
                        cout
            <<a<<'+'<<b<<'=' ; 
                        e
            =a+b ;
                        
            break ;
                    
            case 1 : 
                        cout
            <<a<<'-'<<b<<'=' ;
                        e
            =a-b ;
                        
            break ;    
                    
            case 2 :
                        cout
            <<a<<'*'<<c<<'=' ;
                        e
            =a*c ;
                        
            break ;
                    
            case 3 :
                        cout
            <<g<<'/'<<c<<'=' ;
                        e
            =g/c ;
                        
            break ;
                    } 
                    cin
            >>h ; 
                    
            if(h==e) 
                    { 
                        cout
            <<"正確,得十分!\n" ; 
                        sum
            +=10 ;
                    } 
                    
            else cout<<"不正確,不得分!\n" ; 
                } 
                cerr
            <<"十題中,答對:"<<sum/10<<""<<'\t'<<"得分:"<<sum<<'\n' ;
                cerr
            <<"Press any key to continue\n" ; 
                getch();   
                menu();
                }
                
            void oopcalc::exitprog()
                {
                exit(
            -1);
                }
                
            void oopcalc::menu()
                {
                
            int input;
                oopcalc a;
                system(
            "cls"); //執(zhí)行系統(tǒng)命令:cls-清屏
                cerr <<"=======================================MENU====================================\n";
                cerr 
            <<"            *            1: Add two numbers                      *            \n";
                cerr 
            <<"            *            2: Subtract two numbers                 *            \n";
                cerr 
            <<"            *            3: Divide two numbers                   *            \n";
                cerr 
            <<"            *            4: Multiply two numbers                 *            \n";
                cerr 
            <<"            *            5: Convert Farenheit to Celsius         *            \n";
                cerr 
            <<"            *            6: Convert Celsius to Farenheit         *            \n";
                cerr 
            <<"            *            7: Find the square root of a number     *            \n";
                cerr 
            <<"            *            8: Complex Caculation('+','-','*','/')  *            \n"
                cerr 
            <<"            *            9: Test For You                         *            \n";
                cerr 
            <<"            *            0: Exit this program                    *              ";
                cerr 
            <<"===============================================================================\n";
                cerr 
            <<"Choice:";
                cin 
            >> input;
                cout 
            <<"===============================================================================\n";

                
                
            switch (input)
                {
                
            case 1: a.calcadd();
                
            break;
                
            case 2:    a.calcsub();
                
            break;
                
            case 3:    a.calcdiv();
                
            break;
                
            case 4:    a.calcmult();
                
            break;
                
            case 5:    a.calcfartocel();
                
            break;
                
            case 6:    a.calcceltofar();
                
            break;
                
            case 7:    a.calcsroot();
                
            break;
                
            case 8:    a.calcmixed();
                
            break;
                
            case 9:    a.test();
                
            break;
                
            case 0:    a.exitprog();
                
            break;
                
            default:a.badinput();
                }
            }
            void oopcalc::badinput()
            {    
                cout 
            << "BAD INPUT!\n";
                cout 
            << "Press any key to continue\n";
                getch();
                menu();
            }
            double oopcalc::add(double x, double y)
            {
                val 
            = x + y;
                
            return val;
            }
            double oopcalc::sub(double x, double y)
            {
                val 
            = x - y;
                
            return val;
            }
            double oopcalc::div(double x, double y)
            {
                val 
            = x / y;
                
            return val;
            }
            double oopcalc::mult(double x, double y)
            {
                val 
            = x * y;
                
            return val;
            }
            double oopcalc::fartocel(double x)
            {
                
            double cel = ((x - 32* 5/ 9;
                
            return cel;
            }
            double oopcalc::celtofar(double x)
            {
                
            double f;
                f 
            = x * 9 / 5 + 32;
                
            return f;
            }
            double oopcalc::sqroot(double x)
            {
                
            double g = sqrt(x);
                
            return g;
            }
            void oopcalc::intoin()
            {
                formulalen
            =0;
                
            int templen;
                
            int temlen;
                
            char tem[100];

                cin
            >>temp;
                templen
            =strlen(temp);
                
            int i;
                
            int j;
                j
            =0;
                
            for(i=0;i<templen;i+=temlen)
                {
                    
            if(isdigit(temp[i]))
                    {
                        sscanf(
            &temp[i],"%[^-^+^*^/^(^)]",tem);
                        temlen
            =strlen(tem);
                        strcpy(
            in[j].numordigit,tem);
                        formulalen
            ++;
                        j
            ++;
                    }
                    
            else
                    {
                        temlen
            =1;
                        
            in[j].numordigit[0]=temp[i];
                        
            in[j].numordigit[1]='\0';
                        formulalen
            ++;
                        j
            ++;
                    }
                }
            }




            void oopcalc::intopos()//這是用于將中綴表達式轉化成后綴表達式的函數(shù)
            {
                
            /////////////////'(' ')''+''-''*''/''=' 
                static int isp[7]=0,19,12,12,13,13,0};//為了編程方便,標記出各運算符的優(yōu)先級;
                static int icp[7]={20,19,12,12,13,13,0};//同上;
                int precedence_sta;
                
            int precedence_token;

                stack
            <formula>sta;
                
            int i;
                
            int j;
                
            char token[100];
                formula start;
                strcpy(start.numordigit,
            "\0");
                sta.push(start);
                j
            =0;
                
            for(i=0;i<formulalen;i++)
                {
                    
                    strcpy(token,
            in[i].numordigit);
                    
            if(strcmp(token,"\0")==0)
                        
            break;
                    
            if(isdigit(token[0]))
                    {
                        strcpy(pos[j].numordigit,token);
                        j
            ++;
                    }
                    
            else if(strcmp(token,")")==0)
                    {
                        
            while(strcmp(sta.top().numordigit ,"(")!=0)
                        {
                            strcpy(pos[j].numordigit,sta.top().numordigit);
                            j
            ++;
                            sta.pop();
                        }
                        sta.pop();
                    }
                    
            else
                    {
                            
            if(strcmp(sta.top().numordigit,"(")==0)
                                precedence_sta
            =0;
                            
            if(strcmp(sta.top().numordigit,")")==0)
                                precedence_sta
            =19;
                            
            if(strcmp(sta.top().numordigit,"+")==0)
                                precedence_sta
            =12;
                            
            if(strcmp(sta.top().numordigit,"-")==0)
                                precedence_sta
            =12;
                            
            if(strcmp(sta.top().numordigit,"*")==0)
                                precedence_sta
            =13;
                            
            if(strcmp(sta.top().numordigit,"/")==0)
                                precedence_sta
            =13;
                            
            if(strcmp(sta.top().numordigit,"\0")==0)
                                precedence_sta
            =0;
                            
            if(strcmp(token,"(")==0)
                                precedence_token
            =20;
                            
            if(strcmp(token,")")==0)
                                precedence_token
            =19;
                            
            if(strcmp(token,"+")==0)
                                precedence_token
            =12;
                            
            if(strcmp(token,"-")==0)
                                precedence_token
            =12;
                            
            if(strcmp(token,"*")==0)
                                precedence_token
            =13;
                            
            if(strcmp(token,"/")==0)
                                precedence_token
            =13;
                            
            if(strcmp(token,"\0")==0)
                                precedence_token
            =0;
                        
            while(precedence_sta>=precedence_token)
                        {
                                pos[j]
            =sta.top();
                                j
            ++;
                                sta.pop();
                            
            if(strcmp(sta.top().numordigit,"(")==0)
                                precedence_sta
            =0;
                            
            if(strcmp(sta.top().numordigit,")")==0)
                                precedence_sta
            =19;
                            
            if(strcmp(sta.top().numordigit,"+")==0)
                                precedence_sta
            =12;
                            
            if(strcmp(sta.top().numordigit,"-")==0)
                                precedence_sta
            =12;
                            
            if(strcmp(sta.top().numordigit,"*")==0)
                                precedence_sta
            =13;
                            
            if(strcmp(sta.top().numordigit,"/")==0)
                                precedence_sta
            =13;
                            
            if(strcmp(sta.top().numordigit,"\0")==0)
                                precedence_sta
            =0;
                            
            if(strcmp(token,"(")==0)
                                precedence_token
            =20;
                            
            if(strcmp(token,")")==0)
                                precedence_token
            =19;
                            
            if(strcmp(token,"+")==0)
                                precedence_token
            =12;
                            
            if(strcmp(token,"-")==0)
                                precedence_token
            =12;
                            
            if(strcmp(token,"*")==0)
                                precedence_token
            =13;
                            
            if(strcmp(token,"/")==0)
                                precedence_token
            =13;
                            
            if(strcmp(token,"\0")==0)
                                precedence_token
            =0;
                        }
                        strcpy(start.numordigit,token);
                        
                            sta.push(start);
                    }
                }
                
            while(strcpy(token,sta.top().numordigit))

                {
                    
            if(strcmp(token,"\0")==0)
                        
            break;
                    pos[j]
            =sta.top();
                    j
            ++;
                    sta.pop();
                }
                strcpy(pos[j].numordigit,
            "\0");

            }


            double oopcalc::calculate()//這是用于后綴表達式計算的函數(shù)
            {
                stack
            <double>sta;
                
            char token[100];
                
            double result;
                
            double op1;
                
            double op2;
                
            int i;
                
            double tem;
                

                
            for(i=0;i<=formulalen;i++)
                {

                    strcpy(token,pos[i].numordigit);
                    
            if(strcmp(token,"\0")==0)
                        
            break;
                    
            if(isdigit(token[0]))
                    {
                        sscanf(token,
            "%lf",&tem);
                        sta.push(tem);
                    }
                    
            else
                    {
                        op2
            =sta.top();
                        sta.pop();
                        op1
            =sta.top();
                        sta.pop();
                        
            if(strcmp(token,"+")==0)
                            sta.push(op1
            +op2);
                            
                        
            else if(strcmp(token,"-")==0)
                            sta.push(op1
            -op2);
                            
                        
            else if(strcmp(token,"*")==0)
                            sta.push(op1
            *op2);
                            
                        
            else if(strcmp(token,"/")==0)
                            sta.push(op1
            /op2);
                        
                        
                    }
                }
                result
            =sta.top();
                sta.pop();
                
            return result;
            }







            ////////////////////////////////////////////////
            /////////////////以下是主程序///////////////////
            void main()
            {
                oopcalc s;
                s.menu();
            }

            1.4小結:

                 必須承認,C++課程設計并沒有想像中的那么簡單。在上個學期的c++的學習中,我們主要研究的是面向過程的程序設計,而這次的課程設計卻需要我們用類來完成整個程序。所以,在設計過程中也遇到了不少的麻煩,盡管如此,我還是收益頗豐。

                首先,我學會了面向對象程序設計的基本思想,并了解到許多面向對象程序設計的優(yōu)點,讓我的編程能力有了一個新的突破。比如說,類封裝的特性讓一個類成為一個獨立的個體,使所有的函數(shù)都封裝在其中,這對于程序員對整個程序的理解有非常大的幫助。在大型程序的設計過程中,如過還是使用以往C語言的設計風格,會使得整個程序變得極為冗長,難懂,會給給修改及維護程序帶來諸多不便。而面向對象的設計理念則妥善的解決了這一問題。

                其次,這次課程設計也讓我見到并掌握了許多以前未曾見到過的c++程序功能,很多即使在ACM集訓隊里也未曾見過的頭文件,如<process.h>,<ctype.h>,<conio.h>等等,使我進一步了解到c++底層的強大功能。這著實令人驚嘆!

                再次,通過這次課程設計,我掌握了許多算法,如深度優(yōu)先搜索和廣度優(yōu)先搜索,quicksort,堆排序,樹結構的便歷,等等。

                我想,這次的課程設計使我收益良多。在今后的時間里,我會繼續(xù)不斷的,深入的去研究c++的算法,數(shù)據(jù)結構,以及數(shù)論,圖論和組合數(shù)學里的有關知識,爭取成為南理工計算機專業(yè)的優(yōu)秀畢業(yè)生。       



            posted on 2009-02-19 01:12 abilitytao 閱讀(2140) 評論(3)  編輯 收藏 引用

            評論

            # re: 帶括號浮點型計算器final 2009-02-19 09:11 飯中淹

            不用爭取了,你已經是了.  回復  更多評論   

            # re: 帶括號浮點型計算器final 2009-02-19 09:31 xxxxx

            <process.h>, <conio.h> 是系統(tǒng)相關的函數(shù), ACM主要是考慮算法.

            我覺得你應該拆分為兩個類, 一個是計算類, 各函數(shù)接受輸入并計算結果, 這個類完全不應該有IO操作, 一個是界面類進行輸入輸出. 界面類包含這計算類.

            界面和邏輯分離是基本的設計原則, 這樣你修改輸入輸出界面(比如你以后會寫個GUI的界面)的時侯不用重寫邏輯功能.  回復  更多評論   

            # re: 帶括號浮點型計算器final 2009-02-19 11:46 羅偉濤

            @xxxxx
            哦 原來如此 謝謝呵O(∩_∩)O~  回復  更多評論   

            国产精品永久久久久久久久久| 国产69精品久久久久久人妻精品| 久久99精品国产麻豆不卡| 久久无码精品一区二区三区| 97精品依人久久久大香线蕉97| 国产精品欧美久久久天天影视| 久久精品这里只有精99品| 思思久久好好热精品国产| 狼狼综合久久久久综合网| 成人国内精品久久久久影院VR | 久久夜色精品国产网站| 91性高湖久久久久| 久久九九久精品国产免费直播| 精品一区二区久久| 一本一本久久A久久综合精品| 亚洲国产精品婷婷久久| 奇米综合四色77777久久| 国产亚洲成人久久| 精品久久一区二区三区| 97精品依人久久久大香线蕉97| 99久久精品九九亚洲精品| 久久午夜伦鲁片免费无码| 欧美亚洲日本久久精品| 嫩草影院久久99| 亚洲精品无码久久一线| 一本色综合久久| 久久精品国产99久久久香蕉| 久久这里只有精品久久| 久久综合九色综合网站| 无码伊人66久久大杳蕉网站谷歌| 亚洲精品美女久久久久99小说| 精品欧美一区二区三区久久久| 九九99精品久久久久久| 久久99亚洲网美利坚合众国| 午夜不卡久久精品无码免费| 久久人人爽人人人人片av| 亚洲а∨天堂久久精品9966| 久久亚洲中文字幕精品一区| 久久久久这里只有精品| 久久青青草原亚洲av无码 | 久久人人爽人人人人爽AV |