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

📄 main.c

📁 用c语言编写的一个文本编辑器,实现编辑文字
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "head.h"
#include "FileOperate.h"
#include "FileEdit.h"
#include "LineEdit.h"
#include "BasicOperate.h"

PBigStr trans_command(char *command,PBigStr pstr)//执行命令
{
	int n,j,i=0;
	char *str[4];
	for (i=0;i<=3;i++)
	{
		str[i]=(char *)malloc(80*sizeof(char));
		for (j=0;j<80;j++)
			str[i][j]='\0';
	}
	i=0;
	j=0;
	PBigStr pstr1=create_nullstr();
	PBigStr help_str=create_nullstr();
	while (command[i]!=0)//分解读入的命令为单独的字符串(不含空格)
	{
		if (command[i]!=' ')
		{
			back_insert_char(str[j],command[i]);
		}
		else
		{
			if (i!=0)
			{
				if (command[i-1]!=' ')
					j++;
			}
		}
		i++;
	}
	if (str[0][1]!='\0')//判断是否是有效命令
	{
		printf("Wrong command!\n");
		return pstr;
	}
	switch (str[0][0])
	{
	case 'i'://打开文件
		if (str[1][0]=='\0')
		{
			printf("No file name!\n");
			break;
		}
		pstr1=input_file(str[1]);
		if (pstr1==NULL)
			pstr1=pstr;
		break;
	case 'o'://写入文件
		if (str[1][0]=='\0')
		{
			printf("No file name!\n");
			break;
		}
		output_file(str[1],pstr);
		pstr1=pstr;
		break;
	case 'h'://帮助
		help_str=input_file("help.txt");
		print_str(help_str);
		pstr1=pstr;
		break;
	case 'q'://退出
		printf("Thank you for use!\n");
		exit(1);
		break;
	case 'd'://删除指定行
		if (str[1][0]=='\0')
		{
			printf("No line number!\n");
			break;
		}
		n=atof(str[1]);
		pstr1=delete_line(n,pstr);
		break;
	case 'l'://插入字符串作为第n行
		if (str[1][0]=='\0')
		{
			printf("No string!\n");
			break;
		}
		n=atof(str[1]);
		pstr1=insert_line(n,str[2],pstr);
		break;
	case 'p'://打印第n行
		if (str[1][0]=='\0')
		{
			printf("No line number!\n");
			break;
		}
		n=atof(str[1]);
		print_line(n,pstr);
		pstr1=pstr;
		break;
	case 'f'://显示包含指定字符串的所有行,在行前给出行的编号
		if (str[1][0]=='\0')
		{
			printf("No string!\n");
			break;
		}
		print_str_line(str[1],pstr);
		pstr1=pstr;
		break;
	case 's'://替换第n行的prestr为nextstr
		if (str[1][0]=='\0')
		{
			printf("No line number!\n");
			break;
		}
		if (str[2][0]=='\0')
		{
			printf("No string to be replaced!\n");
			break;
		}
		if (str[3][0]=='\0')
		{
			printf("No string to replace!\n");
			break;
		}
		n=int atof(str[1]);
		pstr1=replace_n_line(n,str[2],str[3],pstr);
		break;
	case 'r'://替换文本中的所有str[1]为str[2]
		if (str[1][0]=='\0')
		{
			printf("No string to be replaced!\n");
			break;
		}
		if (str[2][0]=='\0')
		{
			printf("No string to replace!\n");
			break;
		}
		pstr1=replace(str[1],str[2],pstr);
		break;
	case 'v'://显示整个编辑区
		print_str(pstr);
		pstr1=pstr;
		break;
	case 'c'://计算总行数
		printf("Total line number:%d\n",calculate_line(pstr));
		pstr1=pstr;
		break;
	default:
		printf("Wrong command!\n");
		pstr1=pstr;
		break;
	}
	return pstr1;
}

void main()
{
	PBigStr welcomestr,pstr=create_nullstr();
	char command[81]={'\0'};;
	welcomestr=input_file("welcome.txt");//显示欢迎界面
	print_str(welcomestr);
	while (1)//死循环执行命令
	{
		printf("Please enter a command(enter h for help):\n");
		gets(command);
		pstr=trans_command(command,pstr);
	}
}

⌨️ 快捷键说明

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