⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileedit.h

📁 用c语言编写的一个文本编辑器,实现编辑文字
💻 H
字号:
//对整个文本的操作


PBigStr delete_line(int n,PBigStr pstr)//删除第n行
{
	int i=1;
	PBigStr node,temp;
	if (n<=0)//判断n的合法性
	{
		printf("Integer Error!\n");
		return NULL;
	}
	node=pstr;
	if (n==1&&node!=NULL)//第一行特殊处理
	{
		pstr=pstr->link;
		return pstr;
	}
	temp=locate_line(n,pstr);//定位到第n-1行
	if (temp==NULL||temp->link==NULL)//对n的溢出处理
	{
		printf("Out of range!\n");
		return pstr;
	}
	else
	{//删除第n行
		node=temp->link;
		temp->link=temp->link->link;
		free(node);
		return pstr;
	}

}



PBigStr insert_line(int n,char *str,PBigStr pstr)//插入字符串str作为第n行
{
	int i=0;
	PBigStr node=create_nullstr();//申请新结点
	PBigStr temp1,temp2;
	if (node==NULL)
		return pstr;
	if (n<=0)//对n的溢出处理
	{
		printf("Integer error!\n");
		return pstr;
	}
	if (strlen(str)>=79)//对字符串长度的控制
	{
		printf("The string is too long!\n");
		return pstr;
	}
	while (str[i]!='\0')//从新结点的最后插入字符
	{
		insert_char(node,str[i]);
		i++;
	}
	insert_char(node,'\n');//插入换行符
	if (n==1)//插入新结点到表里面
	{
		node->link=pstr;
		return node;
	}
	temp1=locate_line(n,pstr);
	if (temp1==NULL)
		return pstr;
	temp2=temp1->link;
	temp1->link=node;
	node->link=temp2;
	return pstr;
}

void print_line(int n,PBigStr pstr)//打印第n行
{
	PBigStr node;
	int i=0;
	if (n<=0)//检查n的合法性
	{
		printf("Integer Error!\n");
		return;
	}
	if (n==1)
	{
		node=pstr;
		while (node->pc[i]!='\0'&&node->pc[i]!='\n')
		{
			printf("%c",node->pc[i]);
			i++;
		}
		if (i!=80)//若字符串长度不足一行进行换行
			printf("\n");
		return;
	}
	node=locate_line(n,pstr);
	if (node==NULL)
		return;
	if (node->link==NULL)
	{
		printf("Out of range!\n");
		return;
	}
	node=node->link;
	while (node->pc[i]!='\0'&&node->pc[i]!='\n')
	{
		printf("%c",node->pc[i]);
		i++;
	}
	if (i!=80)//若字符串长度不足一行进行换行
		printf("\n");
	return;
}

PBigStr replace(char *prestr,char *nextstr,PBigStr pstr)//替换整个文本的prestr为nextstr
{
	int i;
	char *pc;//临时的字符数组
	PBigStr temp,newnode,node=pstr;
	pc=(char *)malloc(sizeof(char)*80);
	for (i=0;i<80;i++)//初始化临时字符串
		pc[i]='\0';
	newnode=create_nullstr();
	if (node==NULL)
		return NULL;
	while (node!=NULL)
	{
		for (i=0;node->pc[i]!='\0';i++)
			pc=back_insert_char(pc,node->pc[i]);
		if (node->link==NULL)
			break;
		while(node->pc[79]!='\0')//检查连在一起的行,将它们按顺序插入到临时字符串中
		{
			if (node->link==NULL)
				break;
			node=node->link;
			for (i=0;node->pc[i]!='\0';i++)
				pc=back_insert_char(pc,node->pc[i]);
		}
		pc=replace_line(prestr,nextstr,pc);//对临时字符串进行替换处理
		insert_str(pc,newnode);//将临时字符串插入到新结点中
		temp=newnode;
		while (temp->link!=NULL)//将新结点插入到新表中
		{
			temp=temp->link;
		}
		temp->link=create_nullstr();
		free(pc);//清空临时字符串
		pc=(char *)malloc(sizeof(char)*80);
		for (i=0;i<80;i++)
			pc[i]='\0';
		if (node->link==NULL)
			break;
		else
			node=node->link;
	}
	return newnode;//返回新表
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -