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); }
[提示] 在提交前要测试自己的代码是否正确,可在源文件所有目录自己创建一个名为 data1.in 的文本文
件,在文件中自己打入一些字母,以便测试自己的代码是否正确
[题目 1106:文本文件操作_字符写入]
由键盘输入任意个字符(以连着的三个小写字符 bye 做为结束标志),将所有字符(包括 bye),写
入新建的文件answer.txt中(注:文件放在当前目录)。请完成该功能, (如键盘输入内容如下) He, can you write the code? Yes, you can.bye
(程序执行后,在文件 answer.txt中内容如下) He, can you write the code? Yes, you can.bye
参考程序:
#include \ main()
{ FILE *fp;
char ch, ch1=' ', ch2=' ', ch3=' ';
if((fp=fopen(\ return 1;
while((ch=getchar())!=EOF) { fputc(ch, fp);
ch1=ch2;ch2=ch3;ch3=ch;
if (ch1=='b'&&ch2=='y'&&ch3=='e') break; }
fclose(fp); }
[题目 1107:文本文件操作_单词的排序] 在当前目录有文件“data1.in”,文件里存放有多个(总个数不超过 10000个)英文单词(每个英文单
词不会超过10个字文字符),每行一个,单词未排序。现要求,将文件中的所有单词按字典顺序排序,
然后将排序好的单词写入新建的文件answer.txt中(注:文件存放于当前目录)。请完成程序,实现该功 能,
(如data1.in文件中原内容如下) hello
bye yes
(程序执行后,在文件 answer.txt中内容如下) bye hello yes
参考程序:
#include \ #include \ main()
{ FILE *fp1,*fp2;
char str[1000][11],str1[11]; int n=0,i,j;
if((fp1=fopen(\ return 0;
if((fp2=fopen(\ return 0;
while(fscanf(fp1,\ for(i=0;i if(strcmp(str[i],str[j])>0) { strcpy(str1,str[i]); strcpy(str[i],str[j]); strcpy(str[j],str1); }; } for(i=0;i fprintf(fp2,\ fclose(fp1); fclose(fp2); }