您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> C语言 >> 辅导 >> 正文    
  C语言程序设计(第4章函数)7 【注册男护士专用博客】          

C语言程序设计(第4章函数)7

www.nanhushi.com     佚名   不详 

4.8 程序应用举例
[例4-16] 字符串的显示及反向显示。
#include<stdio.h>
#include<string.h> /* 包含字符串库函数说明的头文件* /
#include<stdio.h>
void forward_and_backwards(char line_of_char[] ,int index); /* 函数声明* /
void main()
{
    char line_of_char[80]; / *定义字符数组* /
    int index = 0;
    strcpy(line_of_char,"This is a string."); / *字符串拷贝* /
    forward_and_backwards(line_of_char,index); / *函数调用* /
}
void forward_and_backwards(char line_of_char[],int index) /*函数定义* /
{
    if(line_of_char[index])
    {
        printf("%c",line_of_char[index]); / *输出字符* /
        forward_and_backwards(line_of_char,index+1); / * 递归调用* /
        printf("%c",line_of_char[index]); / * 输出字符* /
    }
}
    这是一个递归函数调用的例子。程序中函数forward_and_backwards( )的功能是显示一个字符串后反向显示该字符串。
[例4-17] 计算1~7的平方及平方和。
#include <stdio.h>
# include<math.h>
void header(); / *函数声明* /
void square(int number);
void ending();
int sum; /* 全局变量* /
main( )
{
    int index;
    header( ); / *函数调用* /
    for(index = 1;index <= 7;index ++)
        square(index);
    ending( ); / *结束* /
}
void header()
{
    sum = 0; /* 初始化变量"sum" */
    printf("This is the header for the square program\n\n"); 
}
void square(int number)
{
    int numsq;
    numsq = number * number;
    sum += numsq;
    printf("The square of %d is %d\n",number,numsq);
}
void ending()
{
    printf("\nThe sum of the squares is %d\n",sum);
}
运行程序:
This is the header for the square program
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The sum of the squares is 140
这个程序打印出1到7的平方值,最后打印出1到7的平方值的和,其中全局变量sum在多个函数中出现过。
全局变量在header中被初始化为零;在函数square中,sum对number的平方值进行累加,也就是说,每调用一次函数sq uare和sum就对number的平方值累加一次;全局变量sum在函数ending中被打印。

[例4-18] 全局变量与局部变量的作用。
#include <stdio.h>
void head1(void);
void head2(void);
void head3(void);
int count; /* 全局变量* /
main( )
{
    register int index; / *定义为主函数寄存器变量* /
    head1( );
    head2( );
    head3( );
    for (index = 8;index > 0;index--) /* 主函数"for" 循环* /
    {
        int stuff; /* 局部变量* /


        /* 这种变量的定义方法在Turbo C 中是不允许的* /
        /* stuff 的可见范围只在当前循环体内* /
        for(stuff = 0;stuff <= 6;stuff ++)
            printf("%d ",stuff);
        printf(" index is now %d\n",index);
    }
}
int counter; /* 全局变量* /
/* 可见范围为从定义之处到源程序结尾* /
void head1(void)
{
    int index; / * 此变量只用于head1 */
    index = 23;
    printf("The header1 value is %d\n",index);
}
void head2(void)
{
    int count; /* 此变量是函数head2( ) 的局部变量* /
    /* 此变量名与全局变量count 重名* /
    /* 故全局变量count 不能在函数head2( ) 中使用* /
    count = 53;
    printf("The header2 value is %d\n",count);
    counter = 77;
}
void head3(void)
{
    printf("The header3 value is %d\n",counter);
}
运行程序:

The headerl value is 23
The header2 value is 53
The header3 value is 77
0 1 2 3 4 5 6 index is now 8
0 1 2 3 4 5 6 index is now 7
0 1 2 3 4 5 6 index is now 6
0 1 2 3 4 5 6 index is now 5
0 1 2 3 4 5 6 index is now 4
0 1 2 3 4 5 6 index is now 3
0 1 2 3 4 5 6 index is now 2
0 1 2 3 4 5 6 index is now 1

该程序的演示帮助读者来了解全局变量、局部变量的作用域,请仔细理解体会。

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目