4.8 程序應(yīng)用舉例
[例4-16] 字符串的顯示及反向顯示。
#include
#include /* 包含字符串庫函數(shù)說明的頭文件* /
#include
void forward_and_backwards(char line_of_char[] ,int index); /* 函數(shù)聲明* /
void main()
{
char line_of_char[80]; / *定義字符數(shù)組* /
int index = 0;
strcpy(line_of_char,"This is a string."); / *字符串拷貝* /
forward_and_backwards(line_of_char,index); / *函數(shù)調(diào)用* /
}
void forward_and_backwards(char line_of_char[],int index) /*函數(shù)定義* /
{
if(line_of_char[index])
{
printf("%c",line_of_char[index]); / *輸出字符* /
forward_and_backwards(line_of_char,index+1); / * 遞歸調(diào)用* /
printf("%c",line_of_char[index]); / * 輸出字符* /
}
}
這是一個遞歸函數(shù)調(diào)用的例子。程序中函數(shù)forward_and_backwards( )的功能是顯示一個字符串后反向顯示該字符串。
[例4-17] 計算1~7的平方及平方和。
#include
# include
void header(); / *函數(shù)聲明* /
void square(int number);
void ending();
int sum; /* 全局變量* /
main( )
{
int index;
header( ); / *函數(shù)調(diào)用* /
for(index = 1;index <= 7;index ++)
square(index);
ending( ); / *結(jié)束* /
}
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在多個函數(shù)中出現(xiàn)過。
全局變量在header中被初始化為零;在函數(shù)square中,sum對number的平方值進(jìn)行累加,也就是說,每調(diào)用一次函數(shù)sq uare和sum就對number的平方值累加一次;全局變量sum在函數(shù)ending中被打印。
[例4-18] 全局變量與局部變量的作用。
#include
void head1(void);
void head2(void);
void head3(void);
int count; /* 全局變量* /
main( )
{
register int index; / *定義為主函數(shù)寄存器變量* /
head1( );
head2( );
head3( );
for (index = 8;index > 0;index--) /* 主函數(shù)"for" 循環(huán)* /
{
int stuff; /* 局部變量* /
[例4-16] 字符串的顯示及反向顯示。
#include
#include
#include
void forward_and_backwards(char line_of_char[] ,int index); /* 函數(shù)聲明* /
void main()
{
char line_of_char[80]; / *定義字符數(shù)組* /
int index = 0;
strcpy(line_of_char,"This is a string."); / *字符串拷貝* /
forward_and_backwards(line_of_char,index); / *函數(shù)調(diào)用* /
}
void forward_and_backwards(char line_of_char[],int index) /*函數(shù)定義* /
{
if(line_of_char[index])
{
printf("%c",line_of_char[index]); / *輸出字符* /
forward_and_backwards(line_of_char,index+1); / * 遞歸調(diào)用* /
printf("%c",line_of_char[index]); / * 輸出字符* /
}
}
這是一個遞歸函數(shù)調(diào)用的例子。程序中函數(shù)forward_and_backwards( )的功能是顯示一個字符串后反向顯示該字符串。
[例4-17] 計算1~7的平方及平方和。
#include
# include
void header(); / *函數(shù)聲明* /
void square(int number);
void ending();
int sum; /* 全局變量* /
main( )
{
int index;
header( ); / *函數(shù)調(diào)用* /
for(index = 1;index <= 7;index ++)
square(index);
ending( ); / *結(jié)束* /
}
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在多個函數(shù)中出現(xiàn)過。
全局變量在header中被初始化為零;在函數(shù)square中,sum對number的平方值進(jìn)行累加,也就是說,每調(diào)用一次函數(shù)sq uare和sum就對number的平方值累加一次;全局變量sum在函數(shù)ending中被打印。
[例4-18] 全局變量與局部變量的作用。
#include
void head1(void);
void head2(void);
void head3(void);
int count; /* 全局變量* /
main( )
{
register int index; / *定義為主函數(shù)寄存器變量* /
head1( );
head2( );
head3( );
for (index = 8;index > 0;index--) /* 主函數(shù)"for" 循環(huán)* /
{
int stuff; /* 局部變量* /