• <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 夢在天涯 閱讀(1998) 評論(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

            搜索

            •  

            積分與排名

            • 積分 - 1807518
            • 排名 - 5

            最新評論

            閱讀排行榜

            精品国产91久久久久久久a| 99久久精品国产毛片| 国内精品久久久久久不卡影院 | 欧美激情一区二区久久久| 午夜视频久久久久一区 | 香蕉久久av一区二区三区 | 久久久黄片| 成人久久免费网站| 久久精品嫩草影院| 成人久久免费网站| 欧美亚洲日本久久精品| 久久久久久亚洲精品影院| 99久久夜色精品国产网站| 久久精品国产亚洲av麻豆小说 | 久久久精品免费国产四虎| 18岁日韩内射颜射午夜久久成人| 99久久99久久精品国产片果冻| 国产午夜精品理论片久久| 欧美久久天天综合香蕉伊| 波多野结衣久久精品| 久久国产精品成人免费| 无码任你躁久久久久久老妇| 久久免费观看视频| 26uuu久久五月天| 69国产成人综合久久精品| 久久久久久亚洲精品无码| 人人狠狠综合久久88成人| 99久久精品免费观看国产| 中文字幕无码精品亚洲资源网久久 | 亚洲国产成人久久精品99| 久久香蕉超碰97国产精品| 国产亚州精品女人久久久久久 | 久久综合丁香激情久久| 伊人久久综合精品无码AV专区| 国产成人久久精品二区三区| 久久久精品国产Sm最大网站| 久久婷婷色香五月综合激情| 国产精品久久久久久一区二区三区| 亚洲国产精品成人AV无码久久综合影院| 久久综合中文字幕| 国产精品伦理久久久久久|