📄 分别用行处理和串处理建立和修改文本的源程序.txt
字号:
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#define chunksize 80
struct chunk{
char ch[chunksize];
struct chunk *next;
int chunklen;};
struct lstring { struct chunk *head;
struct chunk*tail;
int curlen;
};/*结构体的定义:chunk表示行,chunklen表示行的长度,行与行之间是链式存储lstring是包含行头指针和尾指针和行数的结构体*/
/****************************************************************************/
void delete(struct lstring *s) /*删除函数*/
{ int i;
struct chunk *l, *m,*p;
int j;
printf("enter the line you want to delete:");
scanf("%d",&i); /*输入要删除的行*/
if(i<0||i>s->curlen) /*输入不合法*/
{printf("error"); return; }
l=s->head; p=l;
for(j=1;j<i;j++) l=l->next; /*查找要删除的结点*/
if(l==s->tail) {if(s->curlen==1){s->tail=s->head=0;free(l);}
else {if(s->curlen==2)
{s->tail=s->head;s->tail->next=0;free(l);}
else
{while(p->next!=l) p=p->next; s->tail=p;
p->next=0; free(l);}}
} /*当要删除的结点为尾结点分三种情况分别讨论*/
else{if(l==s->head&&s->curlen!=1) {s->head=l->next;free(l);}
else {while(p->next!=l){p=p->next;}
p->next=l->next;
free(l);
}
} /*否则分两种情况讨论*/
s->curlen--; /*总行数减一*/
return;}
/*****************************************************************************/
void move(struct lstring *s) /*行移动 */
{struct chunk *m,*n,*l,*p; int i,j,k;
n=m=s->head; l=m;
printf("enter the line number you want to move:");
scanf("%d",&i);
printf("enter the line number you want to move to:");
scanf("%d",&j);
if(i==1) {if(j==s->curlen) {s->head=m->next;s->tail->next=m; m->next=0; s->tail=m;}
else {p=l->next; for(k=1;k<j;k++){m=m->next;}
while(n->next!=m) {n=n->next;}
l->next=m->next; m->next=l;
s->head=p;}} /*当移动的行为头结点,分插入为尾结点和其他结点*/
else {if(i==s->curlen){if(j==1) {l=s->tail; while(m->next!=l){m=m->next;}
l->next=s->head;
s->head=l;
s->tail=m;
m->next=0;}
else {l=s->tail; while(m->next!=l) {m=m->next;}
s->tail=m;m->next=0; m=s->head;
for (k=1;k<j;k++){m=m->next;}
while(n->next!=m) {n=n->next;}
n->next=l;
l->next=m;}}/*移动尾结点,分插入的为头结点和其他结点*/
else {if(j==1) {for(k=1;k<i;k++) {l=l->next;}
while(m->next!=l){m=m->next;}
m->next=l->next;
m->next=s->head;
s->head=m;}
else {if(j==s->curlen) {for(k=1;k<i;k++) {l=l->next;}
while(m->next!=l) {m=m->next;}
m->next=l->next;
while(n->next!=s->tail) {n=n->next;}
n->next=l;
l->next=s->tail;}
else { for(k=1;k<i;k++) {l=l->next;}
while(m->next!=l) {m=m->next;}
m->next=l->next; m=s->head;
for(k=1;k<j;k++) {m=m->next;}
while(n->next!=m) {n=n->next;}
n->next=l;
l->next=m;}}}}/*移动的为其他结点,分插入为尾结点,头结点和其他结点*/
return;}
/*****************************************************************************/
void display(struct lstring*s) /*显示函数*/
{ struct chunk*p;
p=s->head;
If(p==0) /*如果为空,显示null*/
{printf ("null");return;}
while(p->ch!=0) /*不为空时,输出数据*/
{puts(p->ch);
p=p->next;}
return; }
/*****************************************************************************/
void insert(struct lstring*s) /*插入函数*/
{ int i,j=0;struct chunk *m,*p,*l=s->head; char a; p=l;
printf("Enter the line number you want to insert:\n");
scanf("%d",&i);
if(i<0||i>s->curlen+1) /*错误判断*/
{printf("error:overflow\n");
return;}
printf("enter the line:");
m=(struct chunk*)malloc(sizeof(struct chunk)); /*分配结点空间*/
m->chunklen=0;m->next=0;
a=getchar();
a=getchar();
while(a!='\n') /*当输入不为回车时*/
{m->ch[j]=a;m->chunklen++;j++;scanf("%c",&a);}
m->ch[j]='\0';
if(l==0) {l=m;l->next=0; s->head=s->tail=l;}
else{if(i!=s->curlen+1){
if(i==1) {m->next=l;s->head=m;}
else{ for(j=1;j<i&&l->next!=s->tail;j++) l=l->next;
while(p->next!=l){p=p->next;}
m->next=p->next;
p->next=m;}
}
else{ s->tail->next=m;
s->tail=m;} }
s->curlen++;
return;
} /*当插入为头结点,尾结点和其他结点时分别讨论*/
/*****************************************************************************/
void inptr(struct lstring *s) /*将两行连接*/
{struct chunk*m,*n,*l;
int i,j;
l=s->head;
printf("enter the line:");
scanf("%d",&i);
for(j=1;j<i;j++) {l=l->next;} /*查找连接的头结点*/
m=l; n=l->next;
if(n==s->tail){strcat(m->ch,n->ch);m->chunklen=m->chunklen+n->chunklen; m->next=0;free(n);s->tail=m;}
else {strcat (m->ch,n->ch); /*连接行*/
m->chunklen=m->chunklen+n->chunklen; m->next=n->next; free(n);}
s->curlen--;}
/*****************************************************************************/
void modefine(struct lstring*s){delete(s); /*修改行,包括删除和插入函数*/
insert(s);}
/*****************************************************************************/
void menu()
{ textbackground(BLACK);
textcolor(WHITE);clrscr(); / *设置屏幕背景*/
printf(" ******************welcome to the programe*******************\n\n");
printf(" # 1-- display the file\n \n");
printf(" # 2-- delete a line \n\n");
printf(" # 3-- insert a line \n\n");
printf(" # 4-- edit one line \n \n");
printf(" # 5-- move a line \n \n");
printf(" # 6-- connect two lines \n \n");
printf(" # 7-- quit the program \n \n");
printf(" # Please Select: \n");
gotoxy(38,19);
} /*选项栏*/
/*****************************************************************************/
void replace (struct lstring s) /*进行行编辑,替换行字符(也可以超出范围)*/
{
int i,j,k,q;
struct chunk *m,*n,*l; char c;
l=s->head;
printf("Enter the line number you want to edit:\n");/*输入要编辑的行号*/
scanf("%d",&i);
if(i<=0||i>s->curlen)
{printf("OVERFLOW");return; }/*出错信息*/
for(k=1;k<i;k++){l=l->next;}
printf("You want to edit char number: \n");/*输入光标号,从那个位置开始编辑替代*/
scanf("%d",&j);
if(j<0||j>80)
{printf("overflow"); return;}
if(j>l->chunklen)
{while((l->chunklen+1)!=j)
{l->ch[l->chunklen]='-';
l->chunklen++;}
printf("input the string:");/*输入替代的符号*/
scanf("%c",&c);
scanf("%c",&c);
while(c!='\n')
{l->ch[l->chunklen]=c;
l->chunklen++;
scanf("%c",&c);}
l->ch[l->chunklen]='\0';}
else { printf("input :");
scanf("%c",&c);
scanf("%c",&c);
while(c!='\n')
{l->ch[j-1]=c;
scanf("%c",&c);j++;
}
if((j+k)>l->chunklen)
{l->chunklen=j+k-2;
l->ch[l->chunklen]='\0';}} return;
}
/*****************************************************************************/
void information() /*显示信息*/
{ char c; printf("\nsuccess!press a to continue:");
scanf("%c",&c);
if(c=='a')
menu();}
/*****************************************************************************/
void main()
{ struct lstring *m;struct chunk*n;struct chunk*l;int i;
char p[]="asddfg";
m=(struct lstring*)malloc(sizeof(struct lstring));
n=(struct chunk *)malloc(sizeof(struct chunk));
l=(struct chunk *)malloc(sizeof(struct chunk));
strcpy(n->ch,p); /*分配空间及串拷贝*/
strcpy(l->ch,p);
n->next=l;
l->next=0;
n->chunklen=6;
l->chunklen=6;
m->head=n;
m->tail=l;
m->curlen=2; /*赋值语句*/
menu();
scanf("%d",&i); if(i<0||i>7){printf("error"); menu(); scanf("%d",&i);}
while(i!=7) /*根据输入,决定操作*/
{ switch(i) {case 1: display(m);information(); break;
case 2: delete(m);information(); break;
case 3: insert(m); information();break;
case 4: replace(m); information();break;
case 5: move(m);information(); break;
case 6: inptr(m);information();break; }
scanf("%d",&i);}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -