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 梦在天涯 阅读(1932) 评论(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 梦在天涯

谢谢楼上的!  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

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

搜索

  •  

积分与排名

  • 积分 - 1784849
  • 排名 - 5

最新评论

阅读排行榜