华南农业大学C语言程序设计实验指导书参考答案 下载本文

float score; }

main() {

struct student stu;

scanf(\ stu.name);

scanf(\ &stu.sex); scanf(\ &stu.num); scanf(\ &stu.score); printf(\printf(\printf(\printf(\}

*实验11 链表

一、实验目的

1)理解链表的概念。

2)掌握结构体、指针在链表中的运用。

3)掌握链表的常用操作,包括创建、显示、添加等。

二、实验内容

[题目1098:链表结点的插入] 有结构体类型定义,

struct student

{ long num; /*学号*/ int score; /*成绩*/

struct student *next; /*指针*/ };

程序首先完成创建两个链表,要求补充完成按学号顺序插入链表结点的函数:

struct student *insert(struct student *head, struct student *stud) {

struct student *p0,*p1,*p2; p1=head; p0=stud;

if(head==NULL){head=p0;p0->next=NULL;} else

{while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1;

p1=p1->next;}

if(p0->num<=p1->num) { if(head==p1)head=p0; else p2->next=p0; p0->next=p1; }

else {p1->next=p0;p0->next=NULL;} }

return(head); }

[题目1099:链表的合并] 有结构体类型定义,

struct student

{ long num; /*学号*/ int score; /*成绩*/

struct student *next; /*指针*/ };

程序首先完成创建两个链表,要求补充完成实现将第二个链表合并到第一个链表未尾的函数。

struct student *merge(struct student *head, struct student *head2) {

struct student *p1; p1=head;

while(p1->next!=NULL)p1=p1->next; p1->next=head2; return(head); }

[题目1104:链表的倒序] 有结构体类型定义,

struct student

{ long num; /*学号*/ int score; /*成绩*/

struct student *next; /*指针*/ };

程序首先完成程序创建一个链表,要求补充完成实现将链表中各结点变为倒序排列的函数。

struct student *reverse(struct student *head) {

struct student *p1,*p2,*p3; p2=head;p3=head->next; do

{ p1=p2;p2=p3;p3=p2->next;p2->next=p1; }

while(p3!=NULL); head->next=NULL; return(p2); }

[题目1101:链表的排序] 有结构体类型定义,

struct student

{ long num; /*学号*/ int score; /*成绩*/

struct student *next; /*指针*/ };

程序首先完成程序创建一个链表,要求补充完成实现将链表中各结点按学号由小到大排序的函数。

struct student *sort(struct student *head) {

struct student *p1,*p2; p2=head;p1=head; p2=p2->next; p1->next=NULL; p1=p2;

while(p2->next!=NULL) {

p2=p2->next; p1->next=NULL;

head=insert(head,p1); p1=p2; }

head=insert(head,p1); return(head); }

*实验12 文件

一、实验目的

1)学会使用文件打开、关闭、读、写等文件操作函数。

二、实验内容

[题目1105:文本文件操作_字符读入]

在当前目录中存在文件名为\的文本文件,现要求你使用fopen 函数命令打开该文件,读出

里面的所有字符,遇到大写字母的,将其变为小写字母,其它字符不变,最后将所有字符按顺序在屏幕

上输出。请填空完成程序 (如data1.in 内容如下) Hello my Dear: Have a GooD Time!

(在屏幕上输出结果如下) hello my dear: have a good time!

程序如下,请填空, #include \main()

{ FILE *fp; char ch;

if((fp=fopen(\return 0;

while((ch=fgetc(fp))!=EOF )

{ if ('A'<=ch && ch<='Z') ch = ch + 32; putchar(ch) ; }

fclose(fp); }