非常简单的石头,剪刀,布程序

非常简单的石头,剪刀,布程序

没有注释且有死循环:
//  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;
}


你可能感兴趣的:(非常简单的石头,剪刀,布程序)