龟兔赛跑c语言课程设计报告,C语言程序设计--龟兔赛跑

/*所需头文件*/

#include

#include

#include

#include

void guitu(void);

main()

{

srand(time(NULL)); /*时间触发函数*/

guitu(); /*调用guitu函数*/

return 0;

}

void guitu() /*定义龟兔函数*/

{

int j, count = 0,cou = 0, number; /*定义变量*/

char printfun[71];

while (1)

{

for (j = 0; j <70;j ++)

{

printfun[j] = ' ';

printfun[70] = '|';

}

number = rand() % 10 + 1; /*产生随机数*/

printf("\n");

if (number >= 1 && number <= 5)

{

count += 3; /*乌龟右移3格*/

if (count > 70)

{

count = 0; /*如超过70格,从头开始计数*/

}

printfun[count] = 'G';

}

if (number >= 6 && number <= 7)

{

count -= 6; /*乌龟左移6格*/

if (count < 0)

{

count = 0; /*如退到0,则从0重新计数*/

}

printfun[count] = 'G';

}

if (number >= 8 && number <= 10)

{

count += 1; /*乌龟右移1格*/

if (count > 70)

{

count = 0; /*如超过70格,从头开始计数*/

}

printfun[count] = 'G';

}

if (number >= 1 && number <= 2)

{

cou += 0; /*兔子不动*/

if (cou > 70)

{

cou = 0;

}

printfun[cou] = 'T';

}

if (number >= 3 && number <= 4)

{

cou += 9; /*兔子右移9格*/

if (cou > 70)

{

cou = 0; /*如超过70格,从头开始计数*/

}

printfun[cou] = 'T';

}

if (cou == 5)

{

cou -= 12; /*兔子左移12格*/

if (cou < 0)

{

cou = 0; /*如退到0,则从0重新计数*/

}

printfun[cou] = 'T';

}

if (number > 6 && number <= 8)

{

cou += 1; /*兔子右移一格*/

if (cou < 70)

{

cou = 0; /*如超过70格,从头开始计数*/

}

printfun[cou] = 'T';

}

if (number >= 9 && number <= 10)

{

cou -= -2; /*兔子左移两格*/

if (cou < 0)

{

cou = 0; /*如退到0,则从0重新计数*/

}

printfun[cou] = 'T';

}

if (count == cou) /*同时到达*/

{

printfun[count] = 'P';

}

printf("%s\n",printfun);

printf("\n");

if (count == 70)

{

printf("gui win !!!\n");/*乌龟胜出*/

break;

}

if (cou == 70)

{

printf("tui win !!!\n");/*兔子胜出*/

break;

printf("ID: 2013914201\n");

}

Sleep(1);

}

printf("ID: 2013914201\n");

}

你可能感兴趣的:(龟兔赛跑c语言课程设计报告)