2007年12月4日 星期二

C 程式設計作業三,迴圈與隨機數應用:解題


/* C Programming Project 3 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
int dice = 0, currentPos = 0, roundNo = 0;
int money = 10000;
int lucky;
char finger[3] = "↖";

srand(time(NULL));
while (roundNo < 3)
{
printf("\n春分 機會 夏至\n\n");
printf("命運 %s 川堂\n\n", finger);
printf("冬至 大門 秋分\n");
printf("================\n");
printf("現存現金 %d 元\n\n", money);
printf("按任意鍵來丟骰子...");
getch();
system("cls");
dice = rand()%6 + 1;
printf("骰子為 %d\n\n", dice);
currentPos += dice;
if (currentPos/8 == 1)
roundNo++;
currentPos %= 8;
switch(currentPos)
{
case 0:
printf("由此過得2000元\n");
money += 2000;
break;
case 1:
printf("機會:");
lucky = rand()%4 + 1;
switch (lucky)
{
case 1:
dice = rand()%3 + 1;
printf("前進 %d 格\n", dice);
currentPos += dice;
break;
case 2:
dice = rand()%3 + 1;
printf("後退 %d 格\n", dice);
currentPos -= dice;
break;
case 3:
printf("損失 500 元\n");
money -= 500;
break;
case 4:
printf("獲利 600 元\n");
money += 600;
break;
}
break;
case 2:
printf("損失 1000元\n");
money -= 1000;
break;
case 3:
printf("獲利 1500元\n");
money += 1500;
break;
case 4:
printf("前進 二格\n");
currentPos += 2;
break;
case 5:
printf("後退 二格\n");
currentPos -= 2;
break;
case 6:
printf("損失 800元\n");
money -= 800;
break;
case 7:
printf("命運:");
lucky = rand()%4 + 1;
switch (lucky)
{
case 1:
dice = rand()%3 + 1;
printf("前進 %d 格\n", dice);
currentPos += dice;
break;
case 2:
dice = rand()%3 + 1;
printf("後退 %d 格\n", dice);
currentPos -= dice;
break;
case 3:
printf("損失 600 元\n");
money -= 600;
break;
case 4:
printf("獲利 500 元\n");
money += 500;
break;
}
break;
default:
break;
}

switch(currentPos)
{
case 0:
strcpy(finger, "↖");
break;
case 1:
strcpy(finger, "↑");
break;
case 2:
strcpy(finger, "↗");
break;
case 3:
strcpy(finger, "→");
break;
case 4:
strcpy(finger, "↘");
break;
case 5:
strcpy(finger, "↓");
break;
case 6:
strcpy(finger, "↙");
break;
case 7:
strcpy(finger, "←");
break;
default:
break;
}
}
system("pause");
return 0;
}

作業三題目
回到首頁

沒有留言: