你们要的C语言实例大全-基础知识篇三
目录
实例24 函数的值调用
tranvalue.c
# include <stdio.h>
/* 子函数声明 */
int square(int x);
int cube(int y);
void main()
{
int m = 12;
int n = 4;
printf("%d %d\n", square(m), m);
printf("%d %d\n", cube(n), n);
}
int square(int x)
{
x = x*x;
return x;
}
int cube(int y)
{
y = y*y*y;
return y;
}
演示
144 12
64 4
实例25 函数的引用调用
tranpoint.c
# include <stdio.h>
void swap(int *x, int *y);
void main()
{
int i, j;
i = 12;
j = 36;
printf("i and j before swapping: %d %d\n", i, j);
swap(&i, &j); /* 传递变量i和j的地址 */
printf("i and j after swapping: %d %d\n", i, j);
}
void swap(int *x, int *y)
{
int temp;
temp = *x; /* 存储变量x的值 */
*x = *y; /* 将y的值放入到x中 */
*y = temp; /* 将x的值放入到y中 */
}
演示
./a.out
i and j before swapping: 12 36
i and j after swapping: 36 12
实例26 数组函数的调用 转置数组
tranarray.c
/* 矩阵的转置 */
# include <stdio.h>
# define N 3
/* 转置函数声明 */
void convert(int element[N][N]);
void main()
{
/* 定义一个二维数组 */
int array[N][N];
int i, j;
/* 给数组符初值 */
printf("输入数组元素:\n");
for(i=0; i<N; i++)
for(j=0; j<N; j++)
scanf("%d", &array[i][j]);
printf("\n数组是:\n");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%5d", array[i][j]);
printf("\n");
}
/* 对数组进行转置工作 */
convert(array);
printf("转置数组是:\n");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%5d", array[i][j]);
printf("\n");
}
}
/* 转置函数定义 */
void convert(int element[N][N])
{
int i, j, t;
for(i=0; i<N; i++)
for(j=i+1; j<N; j++)
{
t = element[i][j];
element[i][j] = element[j][i];
element[j][i] = t;
}
}
演示
输入数组元素:
2
4
5
7
9
11
33
56
43
数组是:
2 4 5
7 9 11
33 56 43
转置数组是:
2 7 33
4 9 56
5 11 43
实例27 命令行变元
countdown.c
/* Countdown program. */
# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
# include <string.h>
void main(int argc, char* argv[])
{
int disp, count;
if(argc < 2)
{
printf("You must enter the length of the count\n");
printf("on the command line. Try again\n");
exit(1); /* 非正常跳出程序 */
}
if(argc==3 && !strcmp(argv[2], "display"))
disp = 1;
else
disp = 0;
for(count = atoi(argv[1]); count; --count)
if(disp)
printf("%d\n", count);
putchar('\a'); /* 将产生蜂鸣 */
printf("Down");
return;
}
演示
guo@ubuntu:~/test/Clanguage/1/Exam027$ ./a.out
You must enter the length of the count
on the command line. Try again
guo@ubuntu:~/test/Clanguage/1/Exam027$ ./a.out display
Downguo@ubuntu:~/test/Clanguage/1/Exam027$ ./a.out 2 display
2
1
实例28 函数的返回值
substr.c
# include <stdio.h>
int find_substr(char* s1, char* s2);
void main()
{
if(find_substr("C is fun", "is") != -1)
printf("Substring is found.\n");
}
/* 定义子函数 */
int find_substr(char* s1, char* s2)
{
register int t;
char *p, *p2;
for(t=0; s1[t]; t++)
{
p = &s1[t];
p2 = s2;
while(*p2 && *p2==*p)
{
p++;
p2++;
}
if(! *p2)
return t;
}
return -1;
}
演示
guo@ubuntu:~/test/Clanguage/1/Exam028$ ./a.out
Substring is found.
实例29 函数的嵌套调用
thrice.c
/* 用弦截法求解方程的根 */
# include <stdio.h>
# include <math.h>
float f(float x)
{
float y;
y = ((x-8.0)*x+12.0)*x - 30.0;
return y;
}
float xpoint(float x1, float x2) /* 定义函数xpoint,求出弦与x轴的焦点 */
{
float y;
y = (x1*f(x2)-x2*f(x1)) / (f(x2)-f(x1));
return y;
}
float root(float x1, float x2) /* 定义函数root,求近似根 */
{
float x, y, y1;
y1 = f(x1);
do
{
x = xpoint(x1, x2);
y = f(x);
if(y*y1 > 0) /* f(x)和f(x1)同符号 */
{
y1 = y;
x1 = x;
}
else
x2 = x;
} while(fabs(y) >= 0.0001);
return x;
}
void main() /* 主函数 */
{
float x1, x2, f1, f2, x;
do
{
printf("Please input x1, x2:\n");
scanf("%f, %f", &x1, &x2);
f1 = f(x1);
f2 = f(x2);
} while(f1*f2 > 0);
x = root(x1, x2);
printf("A root of equation is %9.6f\n", x);
}
演示
guo@ubuntu:~/test/Clanguage/1/Exam029$ ./a.out
Please input x1, x2:
6 7
Please input x1, x2:
A root of equation is 6.890316
实例30 函数的递归调用
recursion.c
/* 递归法将整数转换成字符 */
# include <stdio.h>
void convert(int n)
{
int i;
if((i=n/10) != 0)
convert(i);
putchar(n%10+'0');
}
void main()
{
int number;
printf("输入整数:");
scanf("%d", &number);
printf("输出是:");
if(number < 0)
{
putchar('-');
number = -number;
}
convert(number);
putchar('\n');
}
演示
输入整数:35
输出是:35
实例31 局部和全局变量
outer.c
# include <stdio.h>
int count; /* count是全局变量 */
void func1(); /* 函数声明 */
void func2();
void main()
{
count = 100;
func1();
}
void func1() /* 函数定义 */
{
int temp; /* temp是局部变量 */
temp = count;
func2();
printf(" count is %d\n", count); /* 打印100 */
func2();
}
void func2()
{
int count; /* 定义局部变量count */
for(count = 1; count < 20; count++)
printf("."); /* 打印出"." */
printf("\n");
}
演示
...................
count is 100
...................
实例32 变量的存储类别
static.c
/* 给出年、月、日,计算该日是该年的第几天 */
# include <stdio.h>
int sum_day(int month, int day);
int leap(int year);
void main()
{
int year, month, day;
int days;
printf("请输入日期(年,月,日):");
scanf("%d, %d, %d", &year, &month, &day);
printf("%d年%d月%d日", year, month, day);
days = sum_day(month, day); /* 调用函数sum_day() */
if(leap(year) && month>=3) /* 调用函数leap() */
days = days + 1;
printf("是该年的第%d天.\n", days);
}
/* 定义静态存储变量并赋初值 */
static int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int sum_day(int month, int day) /* 计算日期 */
{
int i;
for(i=1; i<month; i++)
day = day + day_tab[i];
return day;
}
int leap(int year)
{
int leap;
leap = (year%4==0&&year%100!=0)||(year%400==0);
return leap;
}
演示
请输入日期(年,月,日):2020,2,12
2020年2月12日是该年的第43天.
实例33 内部和外部函数
extern.c
# include <stdio.h>
void main()
{
/* 说明本文件将要使用其它文件中的函数 */
extern int multiply();
extern int sum();
int a, b;
int result;
printf("Please input a and b: ");
scanf("%d, %d", &a, &b);
result = multiply(a, b);
printf("The result of multiply is: %d", result);
result = sum(a, b);
printf("\nThe result of sum is: %d\n", result);
}
file1.c
# include <stdio.h>
extern int multiply(int a, int b) /* 定义外部函数multiply() */
{
int c;
c = a * b;
return c; /* 返回参数的乘积 */
}
file2.c
# include <stdio.h>
extern int sum(int a, int b) /* 定义外部函数sum() */
{
int c;
c = a + b;
return c; /* 返回参数的商 */
}
演示
guo@ubuntu:~/test/Clanguage/1/Exam033$ gcc extern.c file1.c file2.c -o test
guo@ubuntu:~/test/Clanguage/1/Exam033$ ./test
Please input a and b: 4,5
The result of multiply is: 20
The result of sum is: 9
实例34 综合实例1
integration1.c
# include <stdio.h>
void head1();
void head2();
void head3();
int count; /* 全局变量 */
void 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()
{
int index; /* 此变量只用于head1 */
index = 23;
printf("The header1 value is %d\n", index);
}
void head2()
{
int count; /* 此变量是函数head2()的局部变量 */
/* 此变量名与全局变量count重名 */
/* 故全局变量count不能在函数head2()中使用 */
count = 53;
printf("The header2 value is %d\n", count);
counter = 77;
}
void head3()
{
printf("The header3 value is %d\n", counter);
}
演示
guo@ubuntu:~/test/Clanguage/1/Exam034$ ./a.out
The header1 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
实例35 综合实例2
integration2.c
/* 学生成绩统计 */
# include <stdio.h>
# define M 5
# define N 10
float score[N][M];
float a_stu[N], a_cor[M];
/* 声明子函数 */
void input_stu();
void avr_stu();
void avr_cor();
float highest(int *r, int *c);
float s_diff();
void main() /* 主函数 */
{
int i, j, r, c;
float h;
r = 0;
c = 1;
input_stu(); /* 调用函数input_stu,输入学生各门功课的成绩 */
avr_stu(); /* 调用函数avr_stu,求出每个学生的平均分 */
avr_cor(); /* 调用函数avr_cor,找出学生成绩中的最高分*/
printf("\n 序号 课程1 课程2 课程3 课程4 课程5 平均分");
for(i=0; i<N; i++)
{
printf("\n NO%2d", i+1);
for(j=0; j<M; j++)
printf("%8.2f", score[i][j]);
printf("%8.2f", a_stu[i]);
}
printf("\n课平均");
for(j=0; j<M; j++)
printf("%8.2f", a_cor[j]);
h = highest(&r, &c);
printf("\n\n最高分%8.2f是 %d号学生的第%d门课\n", h, r, c);
printf(" 方差 %8.2f\n", s_diff());
}
void input_stu() /* 输入学生的成绩 */
{
int i, j;
for(i=0; i<N; i++)
{
printf("请输入学生%2d的5个成绩:\n", i+1);
for(j=0; j<M; j++)
scanf("%f", &score[i][j]);
}
}
void avr_stu() /* 计算学生的平均分 */
{
int i, j;
float s;
for(i=0; i<N; i++)
{
s = 0;
for(j=0; j<M; j++)
s = s + score[i][j];
a_stu[i] = s/M;
}
}
void avr_cor() /* 计算课程的平均分 */
{
int i, j;
float s;
for(j=0; j<M ;j++)
{
s = 0;
for(i=0; i<N; i++)
s = s + score[i][j];
a_cor[j] = s/(float)N;
}
}
float highest(int *r, int *c) /* 找最高分 */
{
float high;
int i, j;
high = score[0][0];
for(i=0; i<N; i++)
for(j=0; j<M; j++)
if(score[i][j]>high)
{
high = score[i][j];
*r = i + 1;
*c = j + 1;
}
return high;
}
float s_diff() /* 求方差 */
{
int i;
float sumx, sumxn;
sumx = 0.0;
sumxn = 0.0;
for(i=0; i<N; i++)
{
sumx = sumx + a_stu[i]*a_stu[i];
sumxn = sumxn + a_stu[i];
}
return (sumx/N-(sumxn/N)*(sumxn/N));
}
演示
guo@ubuntu:~/test/Clanguage/1/Exam035$ ./a.out
请输入学生 1的5个成绩:
66
77
88
99
100
请输入学生 2的5个成绩:
76
68
79
89
88
请输入学生 3的5个成绩:
90
91
93
94
7
请输入学生 4的5个成绩:
68
98
97
99
100
请输入学生 5的5个成绩:
77
84
92
96
84
请输入学生 6的5个成绩:
77
88
99
67
68
请输入学生 7的5个成绩:
97
80
98
97
96
请输入学生 8的5个成绩:
87
96
67
68
69
请输入学生 9的5个成绩:
76
73
83
88
779
请输入学生10的5个成绩:
77
77
99
87
76
序号 课程1 课程2 课程3 课程4 课程5 平均分
NO 1 66.00 77.00 88.00 99.00 100.00 86.00
NO 2 76.00 68.00 79.00 89.00 88.00 80.00
NO 3 90.00 91.00 93.00 94.00 7.00 75.00
NO 4 68.00 98.00 97.00 99.00 100.00 92.40
NO 5 77.00 84.00 92.00 96.00 84.00 86.60
NO 6 77.00 88.00 99.00 67.00 68.00 79.80
NO 7 97.00 80.00 98.00 97.00 96.00 93.60
NO 8 87.00 96.00 67.00 68.00 69.00 77.40
NO 9 76.00 73.00 83.00 88.00 779.00 219.80
NO10 77.00 77.00 99.00 87.00 76.00 83.20
课平均 79.10 83.20 89.50 88.40 146.70
最高分 779.00是 9号学生的第5门课
方差 1698.37