首页 > 其他 > 详细

Linux C一站式学习习题答案 剪刀石头布

时间:2014-03-10 18:20:05      阅读:520      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char arr[3][10] = { "scissor", "stone", "cloth"};
int man, comp, result;
int man_w = 0;
int	com_w = 0;

int get_man()
{
	printf("Please chose your gesture: 0-scissor,1-stone,2-cloth\n");
	scanf("%d",&man);
	if (man < 0 || man > 2) {
		man = get_man();
	}
	return man;
}

void game()
{
	srand(time(NULL));

	//用户选择出什么
	man = get_man();

	//电脑随机出
	comp = rand() % 3;

	//输出结果
	printf("your gesteure is %s\n", arr[man]);
	printf("comp‘s gesteure is %s\n", arr[comp]);

	//比较输赢
	result = (man - comp + 4) % 3 - 1;

	if (result > 0)
	{
		printf("You Win!!!\n");
		man_w++;
	} else if (0 == result){
		printf("Draw!!!\n");
	} else {
		printf("You Lose!!!\n");
		com_w++;
	}
	return;
}

int main()
{
	int i, N;
	printf("please enter the num you want to play:");
	scanf("%d", &N);
	for (i = 0; i < N; i++)
	{
		printf ("************Round %d*************\n", i+1);
		game();
		printf ("\n\n");
	}

	if (man_w > com_w)
		printf ("Congratulation!\n%d to %d You WIN!\n",man_w, com_w);
	else if (man_w = com_w)
		printf("Draw!!!\n%d to %d\n",man_w, com_w);
	else
		printf ("Sorry,\n%d to %d You lose!Try again?\n",man_w, com_w);
	return 0;
}

Linux C一站式学习习题答案 剪刀石头布,布布扣,bubuko.com

Linux C一站式学习习题答案 剪刀石头布

原文:http://blog.csdn.net/whorus1/article/details/20923075

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!