• <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++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            非常簡(jiǎn)單的石頭,剪刀,布程序

            沒有注釋且有死循環(huán):
            //?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;
            }


            可是如果輸入字符還是死循環(huán)..............

            最后的終于正確了的:
            主要是要檢查cin的結(jié)果,用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 夢(mèng)在天涯 閱讀(1996) 評(píng)論(5)  編輯 收藏 引用 所屬分類: CPlusPlus

            評(píng)論

            # re: 非常簡(jiǎn)單的石頭,剪刀,布程序 2006-06-24 14:39 夢(mèng)在天涯

            輸入字符或字符串的時(shí)候會(huì).................  回復(fù)  更多評(píng)論   

            # re: 非常簡(jiǎn)單的石頭,剪刀,布程序 2006-06-24 15:35 夢(mèng)在天涯

            死循環(huán)????為什么不執(zhí)行循環(huán)里的輸入語(yǔ)句cin >> a;和cin >> b;???  回復(fù)  更多評(píng)論   

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

            會(huì)出現(xiàn)錯(cuò)誤 如果能加一個(gè)異常處理會(huì)更好。
            作者多寫一些注釋 便于人們讀你的程序。
            游戲規(guī)則也不說明。  回復(fù)  更多評(píng)論   

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

            對(duì)于cin>>(int)a, 當(dāng)你輸入的不是整型時(shí),會(huì)使cin陷入錯(cuò)誤狀態(tài),這樣以后的cin都不能再讀入,一直處于錯(cuò)誤狀態(tài)。
            所以在輸入前,要檢查錯(cuò)誤。

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

            # re: 非常簡(jiǎn)單的石頭,剪刀,布程序 2006-06-26 14:19 夢(mèng)在天涯

            謝謝樓上的!  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804603
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            国产精品内射久久久久欢欢| 国产精品久久久久AV福利动漫| 久久久久无码精品国产app| 国产香蕉97碰碰久久人人| 久久人人超碰精品CAOPOREN| 久久综合亚洲色一区二区三区| 性欧美丰满熟妇XXXX性久久久 | 久久人做人爽一区二区三区| 亚洲精品无码久久一线| 久久成人精品视频| 久久综合亚洲色HEZYO国产| 中文无码久久精品| 久久综合丁香激情久久| 无码8090精品久久一区| 97精品国产91久久久久久| 久久天天日天天操综合伊人av| 亚洲国产精品无码久久一线| 国产精品热久久无码av| 亚洲色大成网站www久久九| 成人精品一区二区久久久| 欧美日韩久久中文字幕| 99久久国产主播综合精品| 久久久久av无码免费网| 国产精品丝袜久久久久久不卡| 亚洲精品午夜国产VA久久成人| 国产女人aaa级久久久级| 亚洲av伊人久久综合密臀性色| 久久精品免费大片国产大片| 久久久久亚洲AV无码专区体验| 欧美日韩精品久久久免费观看| 99久久99久久| 亚洲精品乱码久久久久66| 国产综合成人久久大片91| 国产精品久久午夜夜伦鲁鲁| 国产99久久久国产精品小说| 久久99精品国产麻豆婷婷| 国产精品久久波多野结衣| 亚洲AV日韩AV永久无码久久| 色综合久久夜色精品国产| 久久国产免费| 天天爽天天爽天天片a久久网|