1个回答
展开全部
别人写的
//送你一个吧.
#include "stdio.h"
#include "stdlib.h"
struct line{
char text[81];
int num; /*行号*/
struct line *next; /*指向下一个输入项目的指针 */
struct line *prior; /*指向前一个项目的指针 */
};
struct line *start; /*指向表中第一个项目的指针 */
struct line *last; /*指向表中作后一个项目的指针 */
struct line *find(int),*dls_store(struct line *);
void patchup(int,int),delete_text(),list(),save(char *),load(char *);
menu_select();
enter(int linenum);
void main(int argc,char *argv[])
{
char s[80],choice,fname[80];
// struct line *info;
int linenum=1;
start=NULL;
last=NULL;
if(argc==2) load(argv[1]);/*装入命令行上的文件 */
do{
choice=menu_select();
switch(choice){
case 1:printf("\t行号:");
gets(s);
linenum=atoi(s);
enter(linenum);
break;
case 2:delete_text();
break;
case 3:list();
break;
case 4:printf("\t文件名:");
gets(fname);
save(fname);
break;
case 5:printf("\t文件名:");
gets(fname);
load(fname);
break;
case 6:exit(0);
}
}while(1);
}
/* 显示菜单,供用户选择 */
menu_select()
{
char s[80];
int c;
printf("\t\t1.输入\n");
printf("\t\t2.删除一行\n");
printf("\t\t3.显示一行\n");
printf("\t\t4.文件存盘\n");
printf("\t\t5.装入文件\n");
printf("\t\t6.退出\n");
do{
printf("\n\n\t\t请按数字选择:");
gets(s);
c=atoi(s);
}while(c<0||c>6);
return(c);
}
/*将文本插在指定行端部 */
enter(int linenum)
{
struct line *info;
//char t[81];
for(;;){
/* */
info=(struct line *)malloc(sizeof(struct line));
if(!info){
printf("\t! 内存不够!\n");
return(NULL);
}
printf("%d:",linenum);
gets(info->text);
info->num=linenum;
if(*info->text){
if(find(linenum)) patchup(linenum,1);
if(*info->text) start=dls_store(info);
}
else break;
linenum++;
}
return(linenum);
}
/*当文本内容插在文件中间时其下面的内容的行号必须增加1,而 */
/*删除时,被删除的文本后面的行号必须减1 */
void patchup(int n,int incr)
{
struct line *i;
i=find(n);
while(i){
i->num=i->num+incr;
i=i->next;
}
}
/*按行号排序后插入 */
struct line *dls_store(struct line *i)
{
struct line *old,*p;
if(last==NULL){
i->next=NULL;
i->prior=NULL;
last=i;
return(i);
}
p=start;
old=NULL;
while(p){
if(p->num){
old=p;
p=p->next;
}
else{
if(p->prior){
p->prior->next=i;
i->next=p;
p->prior=i;
return start;
}
i->next=p;
i->prior=NULL;
p->prior=i;
return(i);
}
}
old->next=i;
i->next=NULL;
i->prior=old;
last=i;
return start;
}
/*删除一行 */
void delete_text()
{
struct line *info;
char s[80];
int linenum;
printf("\t行号:");
gets(s);
linenum=atoi(s);
info=find(linenum);
if(info){
if(start==info){
start=info->next;
if(start) start->prior=NULL;
else last=NULL;
}
else{
info->prior->next=info->next;
if(info!=last)
info->next->prior=info->prior;
else last=info->prior;
}
free(info);
patchup(linenum+1,-1);
}
}
/*查找一行文本 */
struct line *find(int linenum)
{
struct line *info;
info=start;
while(info){
if(linenum==info->num) return(info);
info=info->next;
}
return(NULL);
}
/*显示文本 */
void list()
{
struct line *info;
info=start;
while(info){
printf("%d:%s\n",info->num,info->text);
info=info->next;
}
printf("\n\n");
}
/*存文件 */
void save(char *fname)
{
struct line *info;
char *p;
FILE *fp;
if((fp=fopen("text.txt","w"))==NULL){
printf("\t文件打不开!\n");
exit(0);
}
printf("\t正在存入文件:\n");
info=start;
while(info){
p=info->text;
while(*p) putc(*p++,fp);
//putc('\r',fp);
putc('\n',fp);
info=info->next;
}
fclose(fp);
}
/*装入文件 */
void load(char *fname)
{
struct line *info,*temp;
char *p;
FILE *fp;
//int t,size,inct;
int size,inct;
if((fp=fopen("text.txt","r"))==NULL){
printf("\t文件打不开!\n");
exit(0);
}
while(start){
temp=start;
start=start->next;
free(temp);
}
printf("\n\t正装入文件 !\n");
size=sizeof(struct line);
start=(struct line*)malloc(size);
if(!start){
printf("\n\t内存已经用完!");
return;
}
info=start;
p=info->text;
inct=1;
while((*p=getc(fp))!=EOF){
p++;
while((*p=getc(fp))!='\n') p++;
//getc(fp); /*丢掉'\n' */
*p='\0';
info->num=inct++;
info->next=(struct line*)malloc(size);
if(!info->next){
printf("\n\t内存已经用完!");
return;
}
info->prior=temp;
temp=info;
info=info->next;
p=info->text;
}
temp->next=NULL;
last=temp;
free(info);
start->prior=NULL;
fclose(fp);
}
//送你一个吧.
#include "stdio.h"
#include "stdlib.h"
struct line{
char text[81];
int num; /*行号*/
struct line *next; /*指向下一个输入项目的指针 */
struct line *prior; /*指向前一个项目的指针 */
};
struct line *start; /*指向表中第一个项目的指针 */
struct line *last; /*指向表中作后一个项目的指针 */
struct line *find(int),*dls_store(struct line *);
void patchup(int,int),delete_text(),list(),save(char *),load(char *);
menu_select();
enter(int linenum);
void main(int argc,char *argv[])
{
char s[80],choice,fname[80];
// struct line *info;
int linenum=1;
start=NULL;
last=NULL;
if(argc==2) load(argv[1]);/*装入命令行上的文件 */
do{
choice=menu_select();
switch(choice){
case 1:printf("\t行号:");
gets(s);
linenum=atoi(s);
enter(linenum);
break;
case 2:delete_text();
break;
case 3:list();
break;
case 4:printf("\t文件名:");
gets(fname);
save(fname);
break;
case 5:printf("\t文件名:");
gets(fname);
load(fname);
break;
case 6:exit(0);
}
}while(1);
}
/* 显示菜单,供用户选择 */
menu_select()
{
char s[80];
int c;
printf("\t\t1.输入\n");
printf("\t\t2.删除一行\n");
printf("\t\t3.显示一行\n");
printf("\t\t4.文件存盘\n");
printf("\t\t5.装入文件\n");
printf("\t\t6.退出\n");
do{
printf("\n\n\t\t请按数字选择:");
gets(s);
c=atoi(s);
}while(c<0||c>6);
return(c);
}
/*将文本插在指定行端部 */
enter(int linenum)
{
struct line *info;
//char t[81];
for(;;){
/* */
info=(struct line *)malloc(sizeof(struct line));
if(!info){
printf("\t! 内存不够!\n");
return(NULL);
}
printf("%d:",linenum);
gets(info->text);
info->num=linenum;
if(*info->text){
if(find(linenum)) patchup(linenum,1);
if(*info->text) start=dls_store(info);
}
else break;
linenum++;
}
return(linenum);
}
/*当文本内容插在文件中间时其下面的内容的行号必须增加1,而 */
/*删除时,被删除的文本后面的行号必须减1 */
void patchup(int n,int incr)
{
struct line *i;
i=find(n);
while(i){
i->num=i->num+incr;
i=i->next;
}
}
/*按行号排序后插入 */
struct line *dls_store(struct line *i)
{
struct line *old,*p;
if(last==NULL){
i->next=NULL;
i->prior=NULL;
last=i;
return(i);
}
p=start;
old=NULL;
while(p){
if(p->num){
old=p;
p=p->next;
}
else{
if(p->prior){
p->prior->next=i;
i->next=p;
p->prior=i;
return start;
}
i->next=p;
i->prior=NULL;
p->prior=i;
return(i);
}
}
old->next=i;
i->next=NULL;
i->prior=old;
last=i;
return start;
}
/*删除一行 */
void delete_text()
{
struct line *info;
char s[80];
int linenum;
printf("\t行号:");
gets(s);
linenum=atoi(s);
info=find(linenum);
if(info){
if(start==info){
start=info->next;
if(start) start->prior=NULL;
else last=NULL;
}
else{
info->prior->next=info->next;
if(info!=last)
info->next->prior=info->prior;
else last=info->prior;
}
free(info);
patchup(linenum+1,-1);
}
}
/*查找一行文本 */
struct line *find(int linenum)
{
struct line *info;
info=start;
while(info){
if(linenum==info->num) return(info);
info=info->next;
}
return(NULL);
}
/*显示文本 */
void list()
{
struct line *info;
info=start;
while(info){
printf("%d:%s\n",info->num,info->text);
info=info->next;
}
printf("\n\n");
}
/*存文件 */
void save(char *fname)
{
struct line *info;
char *p;
FILE *fp;
if((fp=fopen("text.txt","w"))==NULL){
printf("\t文件打不开!\n");
exit(0);
}
printf("\t正在存入文件:\n");
info=start;
while(info){
p=info->text;
while(*p) putc(*p++,fp);
//putc('\r',fp);
putc('\n',fp);
info=info->next;
}
fclose(fp);
}
/*装入文件 */
void load(char *fname)
{
struct line *info,*temp;
char *p;
FILE *fp;
//int t,size,inct;
int size,inct;
if((fp=fopen("text.txt","r"))==NULL){
printf("\t文件打不开!\n");
exit(0);
}
while(start){
temp=start;
start=start->next;
free(temp);
}
printf("\n\t正装入文件 !\n");
size=sizeof(struct line);
start=(struct line*)malloc(size);
if(!start){
printf("\n\t内存已经用完!");
return;
}
info=start;
p=info->text;
inct=1;
while((*p=getc(fp))!=EOF){
p++;
while((*p=getc(fp))!='\n') p++;
//getc(fp); /*丢掉'\n' */
*p='\0';
info->num=inct++;
info->next=(struct line*)malloc(size);
if(!info->next){
printf("\n\t内存已经用完!");
return;
}
info->prior=temp;
temp=info;
info=info->next;
p=info->text;
}
temp->next=NULL;
last=temp;
free(info);
start->prior=NULL;
fclose(fp);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询