| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 293 人关注过本帖
标题:再贴个程序,大家看看,结果又有点想不通
只看楼主 加入收藏
c821101017
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2011-9-21
结帖率:100%
收藏
 问题点数:0 回复次数:0 
再贴个程序,大家看看,结果又有点想不通
程序代码:
#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;
}
运行结果截图:
图片附件: 游客没有浏览图片的权限,请 登录注册

大家帮忙看看哈

搜索更多相关主题的帖子: color 
2012-01-08 20:45
快速回复:再贴个程序,大家看看,结果又有点想不通
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018975 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved