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(\)= =NULL)
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 for(j=i+1;j 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); }