#include "stdio.h"
#include <stdlib.h>
#include <string.h>
/*定义块链结构,每行作为一个结点,有80个字符*/
typedef struct BLOCK {
char ch[80];
struct BLOCK *next;
}Node,*LinkString;
LinkString L;
void getnum(char sel[],int *n1,int *n2,int line)
{/*将存放在sel中的字符串分解出n1和n2*/
int i,k1,k2,flag=0;
k1=0;k2=0;i=1;
while(sel[i]!='\0')
if (sel[i]>='0'&&sel[i]<='9')
{
k1=k1*10+(sel[i]-'0');
flag=1;
i++;
}
else
if (sel[i]==',')
{
flag=2;
i++;
break;
};
//printf("哈哈");
while(sel[i]!='\0') {
if (sel[i]>='0'&&sel[i]<='9')
{k2=k2*10+(sel[i]-'0');i++;}
else break;
};
switch (sel[0]) {
case 'L':
case 'l':
case 'D':
case 'd':
if (k1==0) *n1=line;
else *n1=k1;
if (k2==0) *n2=*n1+10;
else *n2=k2;
if (flag==1) *n2=*n1;
if (flag==0) {
*n1=line;*n2=line+10;
}
break;
case 'I':
case 'i':
case 'E':
case 'e':
if (k1==0) *n1=line;
else *n1=k1;
break;
}
}
void loadfile(char filename[])
/*从磁盘读取指定的文件并存放在块链表中*/
{
FILE *fp;
int i;
char c;
LinkString p,q;
fp=fopen(filename,"r");
if (fp==NULL) exit(0);
i=0;p=L;
q=(LinkString)malloc(sizeof(Node));
while((c=getc(fp))!=EOF) {
q->ch[i++]=c;
if (c==10) {
q->ch[i]='\0';
q->next=NULL;
p->next=q;p=q;
q=(LinkString)malloc(sizeof(Node));
i=0;
}
}
}
void savefile(char filename[])
/*将存放在块链表中的字符存放到磁盘文件filename中*/
{
FILE *fp;
//int i,j;
//char c;
LinkString p;
fp=fopen(filename,"w");
if (fp==NULL) exit(0);
p=L->next;
while(p) {
fprintf(fp,"%s",p->ch);
p=p->next;
}
fclose(fp);
}
int display(int n1,int n2)
/*在屏幕上显示文件中第n1行到第n2行的内容*/
{
LinkString p,q,s;
int i,k;
if (n1>n2) return(0);
p=L->next;i=0;
while (p!=NULL&&i<n1) {
p=p->next;i++;
}
if (!p) return(0);
q=p->next;
while (q!=NULL&&i<n2) {
q=q->next;i++;
}
s=p;i=n1;
while (s!=q) {
k=0;printf("%2d:",i);
while (s->ch[k]!='\0') {printf("%c",s->ch[k]);k++;}
s=s->next;
i++;
}
return(1);
}
int deletechar(int n1,int n2)
/*删除文件中第n1行到第n2行的内容*/
{
LinkString p,q,s;
int i;
if (n1>n2) return(0);
p=L;i=0;
while (p!=NULL&&i<n1) {
p=p->next;i++;
}
if (!p) return(0);
q=p->next;
while (q!=NULL&&i<=n2) {
q=q->next;i++;
}
s=p->next;p->next=q;
while (s!=q) {
p=s->next;free(s);
s=p;
}
return(1);
}
int edit(int n1)
/*修改文件中第n1行的内容*/
{
LinkString p;
int i,k;
char str[80];
p=L->next;i=0;
while (p!=NULL&&i<n1) {
p=p->next;i++;
}
if (!p) return(0);
printf("Old Line(%d):",n1);
for (k=0;p->ch[k]!='\0';k++) printf("%c",p->ch[k]);
printf("New Line(%d):",n1);gets(str);
for(k=0;str[k]!='\0';k++)
p->ch[k]=str[k];
p->ch[k++]=10;
p->ch[k]='\0';
return(1);
}
int insert(int n1)
/*在文件中第n1行之前插入一行*/
{
LinkString p,s;
int i,k;
char str[80];
p=L;i=0;
while (p!=NULL&&i<n1) {
p=p->next;i++;
}
if (!p) return(0);
s=(LinkString) malloc(sizeof(Node));
printf("New Line(%d):",n1);gets(str);
for(k=0;str[k]!='\0';k++)
s->ch[k]=str[k];
s->ch[k++]=10;
s->ch[k]='\0';
s->next=p->next;
p->next=s;
return(1);
}
void main(int argc,char *argv[])
{
char sel[50],string[80];
int n1,n2,cur_line=0,flag=0;
L=(LinkString)malloc(sizeof(Node));
L->next=NULL;/*生成头结点*/
// strcpy(string,argv[1]);
/*打开文件*/
if (argc>=2) loadfile(string);
else {
printf("Input FileName:");
scanf("%s",string);
getchar();
loadfile(string);
}
/*从键盘接受一个字符串,转换为命令和参数*/
while (1) {
printf(">>");
/*gets(sel);*/
fflush(stdin);
scanf("%s",sel);
getnum(sel,&n1,&n2,cur_line);/*将接受的字符串转换为参数n1,n2*/
switch(sel[0]) {/*分别调用函数完成相应功能*/
case 'S':
case 's': flag=1;savefile(string);break;
case 'Q':
case 'q': flag=1;break;
case 'L':
case 'l': display(n1,n2);break;
case 'D':
case 'd': deletechar(n1,n2);display(0,10);break;
case 'I':
case 'i': insert(n1);break;
case 'E':
case 'e': edit(n1);break;
}
if (flag) break;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
数据结构课件和源代码如鹏网友分享
共418个文件
txt:108个
rar:81个
h:58个
3星 · 超过75%的资源 需积分: 4 14 下载量 53 浏览量
2011-09-20
11:03:31
上传
评论
收藏 5.42MB RAR 举报
温馨提示
这个压缩包中包含如下几个文档:1,数据结构code ,2,数据结构讲义(清华大学 严版) 3,上课用的课件, 4,杨中科一些学习建议.txt 。是学习数据结构的的精华,对学习数据结构非常有帮助。希望能对大家有用
资源推荐
资源详情
资源评论
收起资源包目录
数据结构课件和源代码如鹏网友分享 (418个子文件)
car.c 8KB
3.13.c 2KB
7.15.c 1KB
5.4.c 1KB
ma.c 1KB
7.10.c 1KB
7.1.c 1KB
3.12.c 1KB
kuohaopipei.c 1KB
7.2.c 987B
7.14.c 985B
doctor.c 946B
7.13.c 939B
5.3.c 891B
7.16.c 842B
3.24.c 838B
7.9.c 814B
7.11.c 779B
5.2.c 722B
汉诺塔.c 597B
7.8.c 533B
3.25.c 505B
7.7.c 461B
5.10.c 457B
3.14.c 430B
3.15.c 426B
7.3.c 415B
3.20.c 395B
3.16.c 370B
5.9-1.c 367B
5.8.c 366B
7.4.c 364B
3.7.c 362B
3.19.c 349B
3.6.c 335B
7.12.c 330B
3.10.c 326B
3.8.c 320B
3.11.c 307B
5.9-2.c 300B
7.6.c 295B
3.9.c 285B
5.1.c 278B
7.5.c 268B
3.1.c 266B
5.7.c 265B
3.2.c 263B
3.23.c 263B
3.18.c 260B
3.22.c 256B
3.3.c 251B
3.4.c 211B
5.5.c 192B
5.6.c 189B
3.17.c 152B
3.21.c 96B
polyadd.cpp 3KB
lstrindex.cpp 2KB
polyadd.cpp 2KB
ReverseList.cpp 2KB
AdjustSqlist.cpp 941B
DelList.cpp 878B
BinAdd.cpp 854B
BinAdd.cpp 854B
ReverseList1.cpp 736B
ReverseList.dsp 3KB
ReverseList.dsw 530B
弗洛伊德算法.exe 244KB
lstrindex.exe 180KB
关键路径算法.exe 172KB
普里姆算法.exe 164KB
拓扑排序算法.exe 164KB
图的遍历.exe 164KB
SeqString.exe 164KB
hiffman3.exe 164KB
算法7.1.exe 160KB
算法5.4.exe 160KB
算法7.2.exe 160KB
printtree.exe 160KB
9_1直接插入排序.exe 160KB
strcompare.exe 160KB
StrDelete.exe 160KB
substring.exe 160KB
strlength.exe 160KB
strindex.exe 160KB
strclear.exe 160KB
StrEmpty.exe 160KB
Strcopy.exe 160KB
strcat.exe 160KB
biaodashi.exe 156KB
preTreeDepth.exe 156KB
线索二叉树.exe 156KB
中序非递归.exe 156KB
后序非递归.exe 156KB
算法6_4.exe 156KB
算法6_10.exe 156KB
算法6_6.exe 156KB
算法6_5.exe 156KB
算法6_8.exe 156KB
BiTree.exe 156KB
共 418 条
- 1
- 2
- 3
- 4
- 5
资源评论
- yaoyaomeiying2012-12-12都是课件,看来下错了!
- weblxw2013-01-17晕,我也是下载错了。
- sxsean9212013-06-17搞过啦,是C语言的
燕儿小乖
- 粉丝: 2
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功