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

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            非常簡單的石頭,剪刀,布程序

            沒有注釋且有死循環:
            //?Game.cpp?:?Defines?the?entry?point?for?the?console?application.
            //

            #include?
            <math.h>
            #include?
            <stdio.h>
            #include?
            <iostream>

            using?namespace?std;

            enum?Result{win,loss,draw};
            enum?Show{scissors,stone,cloth};

            struct?CPerson
            {
            ????Show?isWin;????
            ????CPerson(Show?res)
            ????
            {
            ????????isWin?
            =?res;
            ????}
            ;
            ????Result?IsWin(?CPerson?b)
            {
            ????Result?res?;
            ????
            ????
            switch(abs((int)this->isWin?-?(int)b.isWin))
            ????
            {
            ????
            case?0:?
            ????????res?
            =?(Result)2;
            ????????
            break;
            ????
            case?1:
            ????????
            if((int)this->isWin?>?(int)b.isWin)
            ????????????res?
            =?(Result)0;
            ????????
            else?
            ????????????res?
            =?(Result)1;
            ????????
            break;
            ????
            case?2:?
            ????????
            if((int)this->isWin?>?(int)b.isWin)
            ????????????res?
            =?(Result)1;
            ????????
            else
            ????????????res?
            =?(Result)0;
            ????????
            break;
            ????
            default?:
            ????????
            break;
            ????}

            ????
            return?res;
            }
            ;

            }
            ;

            int?main()
            {
            ????cout
            <<"---------------------"<<endl;
            ????cout
            <<"please?input?two?int?mumbers?,it?must?be?more?equal?0?and?less??equal?2."<<endl;
            ????cout
            <<"0?is?scissors,1?is?stone,and?2?is?cloth."<<endl;
            ????
            int?a,b;
            ????a
            =?-1;b=-1;
            ????
            while(!(a>=0&&a<=2&&b<=2&&b>=0))
            ????
            {
            ????cout
            <<"please?input?A:";
            ????cin
            >>a;
            ????cout
            <<"please?input?B:";
            ????cin
            >>b;
            ????}


            ????CPerson?personA((Show)a);
            ????CPerson?personB((Show)b);
            ????Result?result;
            ????result??
            =?personA.IsWin(personB);
            ????
            if(result?==?(Result)0)
            ????????cout
            <<"A?Win!"<<endl;
            ????
            else?if(result?==?(Result)1)
            ????????cout
            <<"B?Win!"<<endl;
            ????
            else
            ????????cout
            <<"draw"<<endl;
            ????cout
            <<"---------------------"<<endl;
            ????
            return?0;
            }



            修改后(加注釋后的):
            //?Game.cpp?:?Defines?the?entry?point?for?the?console?application.
            //

            #include?
            <math.h>
            #include?
            <stdio.h>
            #include?
            <iostream>

            using?namespace?std;

            enum?Result{win,loss,draw};??//?the?result?of?playing?the?game
            enum?Show{scissors,stone,cloth};?//剪刀,石頭,布

            struct?CPerson
            {
            ????Show?m_show;????
            //the?person's?show.?it?must?be?Show?enum?variable.
            ????CPerson(Show?show)
            ????
            {
            ????????m_show?
            =?show;
            ????}
            ;
            }
            ;
            //IsWin?function?return?personA's?result.
            Result?IsWin(CPerson?a?,CPerson?b)
            {
            ????Result?res?;
            ????
            ????
            switch(abs(a.m_show?-?b.m_show))
            ????
            {
            ????
            case?0:?//personA?and?personB?are?equal.
            ????????res?=?draw;
            ????????
            break;
            ????
            case?1://personA?and?PersonB?are?closed.
            ????????if(a.m_show?>?b.m_show)
            ????????????res?
            =?win;
            ????????
            else?
            ????????????res?
            =?loss;
            ????????
            break;
            ????
            case?2:?//personA?and?personB?are?distant.
            ????????if(a.m_show?>?b.m_show)
            ????????????res?
            =?loss;
            ????????
            else
            ????????????res?
            =?win;
            ????????
            break;
            ????
            default?:
            ????????
            break;
            ????}

            ????
            return?res;
            }
            ;
            int?main()
            {
            ????cout
            <<"---------------------"<<endl;
            ????cout
            <<"please?input?two?int?mumbers?,it?must?be?more?equal?0?and?less??equal?2."<<endl;
            ????cout
            <<"0?is?scissors,1?is?stone,and?2?is?cloth."<<endl;
            ????
            int?a,b;
            ????a
            =?-1;b=-1;
            ????
            while(!(a>=0&&a<=2&&b<=2&&b>=0))
            ????
            {
            ????cout
            <<"please?input?A:";????
            ????cin
            >>a;
            ????cout
            <<"please?input?B:";????
            ????cin
            >>b;
            ????}


            ????CPerson?personA((Show)a);
            ????CPerson?personB((Show)b);
            ????Result?result;
            ????result??
            =?IsWin(personA,personB);

            ????
            if(result?==?(Result)0)
            ????????cout
            <<"A?Win!"<<endl;
            ????
            else?if(result?==?(Result)1)
            ????????cout
            <<"B?Win!"<<endl;
            ????
            else
            ????????cout
            <<"draw"<<endl;
            ????cout
            <<"---------------------"<<endl;
            ????
            return?0;
            }


            可是如果輸入字符還是死循環..............

            最后的終于正確了的:
            主要是要檢查cin的結果,用cin.good()或cin.fail()
            用cin.clear() 和fflush(stdin) 來重新初始化cin .........

            以下是新的代碼:
            //?Game.cpp?:?Defines?the?entry?point?for?the?console?application.
            //

            #include?
            <math.h>
            #include?
            <stdio.h>
            #include?
            <iostream>

            using?namespace?std;

            enum?Result{win,loss,draw};??//?the?result?of?playing?the?game
            enum?Show{scissors,stone,cloth};?//剪刀,石頭,布

            struct?CPerson
            {
            ????Show?m_show;????
            //the?person's?show.?it?must?be?Show?enum?variable.
            ????CPerson(Show?show)
            ????
            {
            ????????m_show?
            =?show;
            ????}
            ;
            }
            ;
            //IsWin?function?return?personA's?result.
            Result?IsWin(CPerson?a?,CPerson?b)
            {
            ????Result?res?;
            ????
            ????
            switch(abs(a.m_show?-?b.m_show))
            ????
            {
            ????
            case?0:?//personA?and?personB?are?equal.
            ????????res?=?draw;
            ????????
            break;
            ????
            case?1://personA?and?PersonB?are?closed.
            ????????if(a.m_show?>?b.m_show)
            ????????????res?
            =?win;
            ????????
            else?
            ????????????res?
            =?loss;
            ????????
            break;
            ????
            case?2:?//personA?and?personB?are?distant.
            ????????if(a.m_show?>?b.m_show)
            ????????????res?
            =?loss;
            ????????
            else
            ????????????res?
            =?win;
            ????????
            break;
            ????
            default?:
            ????????
            break;
            ????}

            ????
            return?res;
            }
            ;
            int?main()
            {
            ????cout
            <<"---------------------"<<endl;
            ????cout
            <<"please?input?two?int?mumbers?,it?must?be?more?equal?0?and?less??equal?2."<<endl;
            ????cout
            <<"0?is?scissors,1?is?stone,and?2?is?cloth."<<endl;
            ????
            int?a,b;
            ????a
            =?-1;b=-1;
            ????
            while(1)
            ????
            {
            ????????cin.clear();
            ????????fflush(stdin);
            ????????cout
            <<"input?A"<<endl;
            ????????cin
            >>a;
            ????????
            if(!(a>=0&&a<=2)||!cin.good())
            ????????
            {
            ????????????cerr
            <<"input?error!"<<endl;????????????????
            ????????????
            continue;
            ????????}

            ????????cout
            <<"input?B"<<endl;
            ????????cin
            >>b;
            ????????
            if(!(b>=0&&b<=2)||!cin.good())
            ????????
            {
            ????????????cerr
            <<"input?error!"<<endl;????????????????????
            ????????????
            continue;
            ????????}
            ????
            ????????CPerson?personA((Show)a);
            ????????CPerson?personB((Show)b);
            ????????Result?result;
            ????????result??
            =?IsWin(personA,personB);

            ????????
            if(result?==?(Result)0)
            ????????????cout
            <<"A?Win!"<<endl;
            ????????
            else?if(result?==?(Result)1)
            ????????????cout
            <<"B?Win!"<<endl;
            ????????
            else
            ????????????cout
            <<"draw"<<endl;
            ????????cout
            <<"---------------------"<<endl;
            ????}

            ????
            return?0;
            }


            posted on 2006-06-24 14:33 夢在天涯 閱讀(1996) 評論(5)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

            # re: 非常簡單的石頭,剪刀,布程序 2006-06-24 14:39 夢在天涯

            輸入字符或字符串的時候會.................  回復  更多評論   

            # re: 非常簡單的石頭,剪刀,布程序 2006-06-24 15:35 夢在天涯

            死循環????為什么不執行循環里的輸入語句cin >> a;和cin >> b;???  回復  更多評論   

            # re: 非常簡單的石頭,剪刀,布程序 2006-06-24 17:41 啊嵩

            會出現錯誤 如果能加一個異常處理會更好。
            作者多寫一些注釋 便于人們讀你的程序。
            游戲規則也不說明。  回復  更多評論   

            # re: 非常簡單的石頭,剪刀,布程序 2006-06-26 13:03 栗子

            對于cin>>(int)a, 當你輸入的不是整型時,會使cin陷入錯誤狀態,這樣以后的cin都不能再讀入,一直處于錯誤狀態。
            所以在輸入前,要檢查錯誤。

            cout<<"Input a:"<<endl;
            a=getchar();
            if(!isdigit(a)){
            cerr<<"Error input, once again!";
            contiue;
            }
            fflush(stdin);
            cout<<"a:"<<a<<endl;  回復  更多評論   

            # re: 非常簡單的石頭,剪刀,布程序 2006-06-26 14:19 夢在天涯

            謝謝樓上的!  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804434
            • 排名 - 5

            最新評論

            閱讀排行榜

            色综合合久久天天综合绕视看| 亚洲人成精品久久久久| 久久久一本精品99久久精品88| 国产精品欧美亚洲韩国日本久久 | 久久国产精品久久久| 漂亮人妻被黑人久久精品| 亚洲国产精品无码久久一区二区| 久久人人爽人人爽人人片AV麻烦| A级毛片无码久久精品免费| 精品无码久久久久国产动漫3d| 久久人人爽人人爽人人片AV高清| 久久99九九国产免费看小说| 亚洲精品97久久中文字幕无码 | 久久福利片| 久久久久综合中文字幕| 久久国产高清一区二区三区| 青春久久| 国产色综合久久无码有码| 色欲av伊人久久大香线蕉影院 | 国产精品久久永久免费| 久久99毛片免费观看不卡| 欧美一区二区精品久久| 久久er国产精品免费观看8| 国产精品一区二区久久精品涩爱| 亚洲人成精品久久久久| 欧美777精品久久久久网| 久久毛片一区二区| 精品一久久香蕉国产线看播放 | 久久久亚洲欧洲日产国码是AV| 亚洲精品乱码久久久久久中文字幕 | 欧美激情精品久久久久| 亚洲精品国产综合久久一线| 影音先锋女人AV鲁色资源网久久| 国产精品久久波多野结衣| 久久久久亚洲AV成人网人人软件 | 四虎影视久久久免费| 精品久久久久中文字幕日本| 日本久久中文字幕| 狠色狠色狠狠色综合久久| 一本久久知道综合久久| 久久久WWW成人免费毛片|