C++编程实现

2025-02-25 12:38:19
推荐回答(5个)
回答1:

第4题正好刚才有人要的,我写了一个,给你吧:
4、实现一个两个人玩“石头,剪子,布”的游戏,游戏规则大家都知道,要求用枚举类型定义“石头,剪子,布”。

#include
#include
#include

enum Choose
{
stone=1,
scissors,
cloth
};

int a,b;
// a: 用来存放用户的选择
// b: 用来存放电脑的选择

void customer(); // 用来获取用户的合法输入,其值保存在a中
void computer();// 用来获取计算机的输入,其值保存在b中

// 该函数用来获取用户的选择,并保存在a中
void customer()
{
// 获取用户的有效输入
do
{
cout<<"Please enter your choice?(1--stone 2--scissors 3--cloth):"< cin>>a;

if(acloth)
{
cout<<"Your choice is invalid!"< }
}while(acloth);

// 显示用户的选择
if(a==stone)
{
cout<<"Your choice is:stone"< }
else if(a==scissors)
{
cout<<"Your choice is:scissors"< }
else if(a==cloth)
{
cout<<"Your choice is:cloth"< }
}

void computer()
{
srand((unsigned)time(NULL));
b=rand()%3+1;
if(b==stone)
cout<<"My choice is:stone"< else if(b==scissors)
cout<<"My choice is:scissors"< else if(b==cloth)
cout<<"My choice is:cloth"<}

void compare(int u , int v)
{
if(b==a)
cout<<"Same!"< else if(b==stone)
{
if(a==scissors) cout<<"You Lose!"< else if(a==cloth) cout<<"You Win!"< }
else if(b==scissors)
{
if(a==stone) cout<<"You Win!"< else if(a==cloth) cout<<"You Lose!"<
}
else if(b==cloth)
{
if(a==stone) cout<<"You Lose!"< else if(a==scissors) cout<<"You Win!"< }
}
void main()
{
char x;
do
{
customer();
computer();
compare(a,b);
cout<<"Do you want to continue?(Y/N)"< cin>>x;
}while(x=='y' || x=='Y');
}

回答2:

第一题
#include
using namespace std;

int dice1()
{
return rand()%6+1;
}

void main()
{
int a, b, total;
float average;

a=dice1();
b=dice1();

total=a+b;
average=(float)(a+b)/2;

cout<<"The fiest die comes up "<cout<<"The second die comes up "<cout<<"Your total roll is "<}

第二题的函数
int snakeEyes()
{
int a,b,count=0;
do{
a=dice();
b=dice();
count++;
}while(a!=1||b!=1);
return count;
}
第三题:
#include
#include
using namespace std;

bool handYear(int year)
{
if((year%100==0)&&(year%400==0))
return true;
else
return false;
}

void main()
{
int year=0, month=0, day=0;
string mon[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
cout<<"Input year: ";
cin>>year;
cout<<"Input the month: ";
cin>>month;

if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
else if(month==2)
{
if(handYear(year))
day=29;
else
day=28;
}
else
day=30;

cout<}
一些限制条件还需要自己加。

回答3:

你是在找免费劳力吗~~!!!!!!!!!

回答4:

自己好好写

回答5:

算法简单,代码字多