再贴个程序,大家看看,结果又有点想不通
程序代码:#include<stdio.h>
#include<stdlib.h>
int roll_count=0; /*外部链接*/
static int rollem(int sides) /*这个文件的私有函数*/
{
int roll;
roll=rand()%sides+1;
printf("roll=%d.\n",roll);
++roll_count; /*计数函数的调用*/
return roll;
}
int roll_n_dice(int dice,int sides)
{
int d;
int total=0;
printf("dice=%d,sides=%d.\n",dice,sides);
if(sides<dice)
{
printf("The dice can't be bigger than sides.\n");
return -3;
}
if(sides<2)
{
printf("Need at lease 2 sides.\n");
return -2;
}
if(dice<1)
{
printf("Need at lease 1 die.\n");
return -1;
}
for(d=0; d<dice; d++)
total+=rollem(sides); //子函数的调用dice次
return total;
}#include<stdio.h>
#include<stdlib.h>
#include<time.h>
extern int roll_count;
extern int roll_n_dice(int dice,int sedes);
int main(void)
{
int dice,roll;
int sides;
srand((unsigned int)time(0)); //随机化种子
printf("Enter the number of sides per die,0 to stop.\n");
while(scanf("%d",&sides)==1 && sides>0)
{
printf("How many dice?\n");
scanf("%d",&dice);
roll=roll_n_dice(dice,sides);
printf("You have rolled a %d using %d %d-sided dice.\n",roll,dice,sides);
printf("How many sides? Enter 0 to stop.\n");
}
printf("The rollem()function was called %d times.\n",roll_count); /*使用外部变量*/
printf("GOOD FORTUNE TO YOU!.\n");
return 0;
}运行结果截图:大家帮忙看看哈







