求高手看一下代码帮偶解释下各个调用函数的作用
求高手看一下代码帮偶解释下各个调用函数的作用,自己一直没看懂额。代码麻烦下一下
shiyan2.rar
(1.15 KB)
程序代码:
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <conio.h> void comp_fis(); int comp_get(int peo_get,int left); void peo_fis(); int apple_num=0,peo_got=0,comp_got=0; void comp_fis() {printf("输入苹果数目:"); scanf("%d",&apple_num); while(1) { comp_got=comp_get(peo_got,apple_num); printf("电脑的选择: %d\n",comp_got); if(comp_got==apple_num) {printf("电脑赢得了比赛\n"); break; } else {apple_num-=comp_got; printf("输出剩余苹果数目: %d\n",apple_num); } printf("输入你的选择:"); scanf("%d",&peo_got); while(1) {if(peo_got>comp_got*2||peo_got>apple_num||peo_got<1) {printf("输入的数不符合要求\n"); printf("输入你的选择:"); scanf("%d",&peo_got);} else break; } if(peo_got==apple_num) {printf(" 人赢得了比赛\n");break;} else { apple_num-= peo_got ; printf("输出剩余苹果数目: %d\n",apple_num); } } } int comp_get(int peo_get,int left) {int temp,get; srand((unsigned)time(NULL)); if(left<=peo_get*2) return left; else{ temp=left/3; if(!peo_get||peo_get*2>=temp) get=rand()%temp; else get=rand()%(peo_get*2); } if(!get) return (get+1); else return get; } void peo_fis() { printf("输入苹果数目:"); scanf("%d",&apple_num); printf("输入你的选择:"); scanf("%d",&peo_got); while(1) {if(peo_got>=apple_num||peo_got<1) {printf("输入的数不符合要求\n"); printf("输入你的选择:"); scanf("%d",&peo_got);} else {apple_num-=peo_got; printf("输出剩余苹果数目: %d\n",apple_num); break;} } while(1) {comp_got=comp_get(peo_got,apple_num); printf("电脑的选择: %d\n",comp_got); if(comp_got==apple_num) { printf("电脑赢得了比赛\n");break;} else {apple_num-=comp_got; printf("输出剩余苹果数目: %d\n",apple_num); } printf("输入你的选择:"); scanf("%d",&peo_got); while(1) {if(peo_got>comp_got*2||peo_got>apple_num||peo_got<1) {printf("输入的数不符合要求\n"); printf("输入你的选择:"); scanf("%d",&peo_got);} else break; } if(peo_got==apple_num) { printf(" 人赢得了比赛\n");break;} else {apple_num-=comp_got; printf("输出剩余苹果数目: %d\n",apple_num); } } } void main() { int i; printf("取苹果游戏开始!\n"); while(1) {printf("\n请输入:0,人先选;\n1,电脑先选;\n2,结束.\n"); scanf("%d",&i); switch(i) {case 0:peo_fis();break; case 1:comp_fis();break; case 2:exit(0); } } getch(); }
[ 本帖最后由 tonlin 于 2009-10-21 22:13 编辑 ]