本文所列例题的程序均不含注释文本,但均为课本上的源代码。
第1章 程序设计和C语言
1.4.1 最简单的C语言程序举例
【例 1.1】要求在屏幕上输出以下一行信息。
This is a C program.
编写程序:
1 2 3 4 5 6
| #include <stdio.h> int main() { printf("This is a C program.\n"); return 0; }
|
运行结果:
【例 1.2】求两个整数之和
编写程序:
1 2 3 4 5 6 7 8 9 10
| #include <stdio.h> int main() { int a,b,sum; a=123; b=456; sum=a+b; printf("sum is %d\n",sum); return 0; }
|
运行结果:
【例 1.3】求两个整数中的较大者。
编写程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("max=%d\n",c); return 0; } int max(int x,int y) { int z; if(x>y)z=x; else z=y; return(z); }
|
运行结果:
1 2 3 4
| ===Input=== 8,5 ===Output=== max=8
|
第2章 算法——程序的灵魂
第3章 最简单的C程序设计——顺序程序设计
第4章 选择结构程序设计
第5章 循环结构程序设计
【例 5.7】用公式 $\frac{\pi}{4} \approx 1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\cdots$ 求 $\pi$ 的近似值,直到发现某一项的绝对值小于 $10^{-6}$ 为止(该项不累加)。
编写程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <stdio.h> #include <math.h> int main() { int sign=1; double pi=0.0,n=1.0,term=1.0; while(fabs(term)>=1e-6) { pi=pi+term; n=n+2; sign=-sign; term=sign/n; } pi=pi*4; printf("pi=%10.8f\n",pi); return 0; }
|
运行结果:
第6章 利用数组处理批量数据
第7章 用函数实现模块化程序设计
第8章 善于利用指针
第9章 用户自己建立数据类型
【例 9.7】有n个结构体变量,内含学生学号、姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息(包括学号、姓名、3门课程成绩和平均成绩)。
编写程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <stdio.h> #define N 3 struct Student { int num; char name[20]; float score[3]; float aver; };
int main() { void input(struct Student stu[]); struct Student max(struct Student stu[]); void print(struct Student stu); struct Student stu[N], * p=stu; input(p); print(max(p)); return 0; }
void input(struct Student stu[]) { int i; printf("请输入各学生的信息:学号、姓名、3门课成绩:\n"); for(i=0;i<N;i++) {scanf("%d %s %f %f %f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0; } }
struct Student max(struct Student stu[]) { int i,m=0; for(i=0;i<N;i++) if(stu[i].aver>stu[m].aver)m=i; return stu[m]; }
void print(struct Student stud) { printf("\n成绩最高的学生是:\n"); printf("学号:%d\n姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver); }
|
运行结果:
1 2 3 4 5 6 7 8 9 10
| 请输入各学生的信息:学号、姓名、3门课成绩: 10101 Li 78 89 98 10103 Wang 98.5 87 69 10106 Sun 88 76.5 89
成绩最高的学生是: 学号:10101 姓名:Li 三门课成绩:78.0,89.0,98.0 平均成绩:88.33
|
【例 9.8】建立一个如图所示的简单链表,它由3个学生数据的结点组成,要求输出各结点中的数据。

编写程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <stdio.h> struct Student { long num; float score; struct Student * next; }; int main() { struct Student a,b,c, * head, * p; a.num=10101;a.score=89.5; b.num=10103;b.score=90; c.num=10108;c.score=85; head=&a; a.next=&b; b.next=&c; c.next=NULL; p=head; do { printf("%ld %5.1f\n",p->num,p->score); p=p->next; }while(p!=NULL); return 0; }
|
运行结果:
1 2 3
| 10101 89.5 10103 90.0 10108 85.0
|
【例 9.9】写一函数建立一个有3名学生数据的单向动态链表。
编写程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <stdio.h> #include <stdlib.h> #define LEN sizeof(struct Student) struct Student { long num; float score; struct Student * next; }; int n; struct Student * creat(void) { struct Student * head; struct Student * p1, * p2; n=0; p1=p2=(struct Student *)malloc(LEN); scanf("%ld,%f",&p1->num,&p1->score); head=NULL; while(p1->num!=0) {n=n+1; if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(struct Student *)malloc(LEN); scanf("%ld,%f",&p1->num,&p1->score); } p2->next=NULL; return(head); } int main() { struct Student *pt; pt=creat(); printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score); return 0; }
|
运行结果:
1 2 3 4 5 6 7
| 1001,67.5 1003,87 1004,99.5 0,0
num:1001 score: 67.5
|
【例 9.10】编写一个输出链表的函数print。
编写程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <stdio.h> #include <stdlib.h> #define LEN sizeof(struct Student) struct Student { long num; float score; struct Student * next; }; int n; struct Student * creat(void) { struct Student * head; struct Student * p1, * p2; n=0; p1=p2=(struct Student *)malloc(LEN); scanf("%ld,%f",&p1->num,&p1->score); head=NULL; while(p1->num!=0) {n=n+1; if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(struct Student *)malloc(LEN); scanf("%ld,%f",&p1->num,&p1->score); } p2->next=NULL; return(head); } void print(struct Student * head) { struct Student *p; printf("\nNow,These %d records are:\n",n); p=head; if(head!=NULL) do {printf("%ld %5.1f\n",p->num,p->score); p=p->next; }while(p!=NULL); } int main() { struct Student *head; head=creat(); print(head); return 0; }
|
运行结果:
1 2 3 4 5 6 7 8 9
| 1001,67.5 1003,87 1005,99 0,0
Now,These 3 records are: 1001 67.5 1003 87.0 1005 99.0
|
第10章 对文件的输入输出
To be completed…
编写程序:
运行结果:
编写程序:
运行结果:
编写程序:
运行结果:


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。