ហ្គែមសាកល្បងភាពរហ័សរបស់អ្នកតាម C++

 

image

#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define sum(x,y) (x+y)
#define mul(x,y) (x*y)
void switched(int x, int y, int ran);
char ch[2]={‘+’, ‘*’}, con;
int ran, x, y, ans, lost=0, win=0;
void main()
{
     do
    {
        clrscr();
        randomize();
        ran=random(2);
        x  =random(100);
        y  =random(100);
      switched(x,y,ran);
     cout<<endl<<"Do you want to play again, Y/N"<<endl;
     con=getch();
    }while(con!=’n’);

    cout<<endl<<"You win "<<win<<" Time!"<<endl<<"You lost "<<lost<<" Time!";
getch();
}
    void switched(int x, int y, int ran)
    {
      switch(ch[ran])
      {
        case ‘+’:
                    cout<<x<<ch[ran]<<y<<" = ?"<<endl;
                    cout<<"Enter your answer = ";
                    cin>>ans; cin.seekg(0);
                    if(ans==sum(x,y))
                    {
                        cout<<"You win!"<<endl;
                        win++;
                    }
                    else
                  {
                        cout<<"You Lost, the answer is "<<sum(x,y)<<endl;
                        lost++;
                  }
                    break;

        case ‘*’:
                    cout<<x<<ch[ran]<<y<<" = ?"<<endl;
                    cout<<"Enter your answer = ";
                    cin>>ans; cin.seekg(0);
                    if(ans==mul(x,y))
                    {
                        cout<<"You win!"<<endl;
                        win++;
                    }
                    else
                  {
                        cout<<"You Lost, the answer is "<<mul(x,y)<<endl;
                        lost++;
                  }
                  break;
      }
    }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s