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

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产成人精品免费久久久久| 国产精品99久久不卡| 久久精品亚洲乱码伦伦中文| 久久w5ww成w人免费| 亚洲av成人无码久久精品| 性高朝久久久久久久久久| 久久精品视频91| 亚洲欧美另类日本久久国产真实乱对白| 国产叼嘿久久精品久久| 99久久精品免费| 久久精品亚洲乱码伦伦中文| 日韩va亚洲va欧美va久久| 午夜精品久久影院蜜桃| 久久久久久免费视频| 久久久免费精品re6| 69久久夜色精品国产69| 国产精品热久久毛片| 久久无码精品一区二区三区| 久久精品久久久久观看99水蜜桃| 亚洲日韩中文无码久久| 国产一级做a爰片久久毛片| 久久国产影院| 国色天香久久久久久久小说| 久久久老熟女一区二区三区| 国产精品嫩草影院久久| 婷婷久久五月天| av无码久久久久不卡免费网站| 香蕉久久一区二区不卡无毒影院| 中文字幕久久亚洲一区| 国产精品久久99| 久久天天躁狠狠躁夜夜2020一| 99久久精品国内| 国产精品美女久久福利网站| 精品免费久久久久久久| 日日狠狠久久偷偷色综合免费| 99精品国产99久久久久久97 | 国色天香久久久久久久小说 | 久久久久久av无码免费看大片| 青青草原综合久久大伊人| 国产69精品久久久久99| 久久久久免费看成人影片|