日期:2014-05-16  浏览次数:20670 次

Linux C一站式学习习题答案 剪刀石头布
#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;
}