代码编辑器

C/C++代码编辑器1.3

资源位置:https://download.csdn.net/download/2301_80029762/88657834?spm=1001.2014.3001.5503

 代码仅供参考

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <map>
#define UP						72
#define DOWN					80
#define LEFT					75
#define RIGHT					77
#define	helps					" Ctrl+Enter:\tcommand\n ?:\t\thelp\n s:\t\tsave\n q:\t\tquit and save\n !:\t\tquit\n o:\t\tcompile\n ^:\t\ttop\n @ num:\t\tto line num\n $ \"Enter\" str:\tsystem \"str\"\n =:\t\tcopy\n"
#define color(tc)				SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), tc)
#define gotoxy(xx, yy)			do{COORD position = {xx, yy};SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), position);}while(false)
#define min(a, b)				((a) < (b) ? (a) : (b))
#define max(a, b)				((a) > (b) ? (a) : (b))
typedef signed char int8;typedef signed short int16;typedef signed long int32;typedef signed long long int64;typedef unsigned char uint8;typedef unsigned short uint16;typedef unsigned long uint32;typedef unsigned long long uint64;typedef unsigned char byte_t;typedef unsigned short word_t;typedef unsigned long iterator;typedef void* pointer_t;
template <typename type> class vector {public:type* p;size_t Len;size_t Size;vector () {p = NULL;Len = 0;Size = 0;}~vector () {delete[] p;}size_t size () {return Size;}size_t len () {return Len;}void exp () {Len = (Len << 1) + 1;if (p == NULL) {p = new type [ Len ];} else {type *tp = new type [ Len ];for (size_t it = 0; it < Size; it++) tp[ it ] = p[ it ];delete[] p;p = tp;}return;}void exp (size_t ts) {while (ts >= Len) exp ();return;}type& operator [] (size_t it) {if (it >= Len) exp (it);return p[ it ];}type at (size_t it) {if (it >= Len) exp (it);return p[ it ];}void push_back (type x) {if (Size == Len) exp ();p[ Size++ ] = x;return;}void push_back (vector <type> vc) {for (size_t i = 0; i < vc.size(); i++) push_back (vc[i]);return;}void pop_back () {if (Size > 0) Size--;return;}void pop_back (size_t ts) {if (Size >= ts) Size -= ts;else Size = 0;return;}void insert (size_t it, type x) {if (Size == Len) exp ();for (size_t it_t = Size; it_t > it; it_t--) p[ it_t ] = p[it_t - 1];p[ it ] = x;Size++;return;}void erase (size_t it) {Size--;for (size_t it_t = it; it_t < Size; it_t++) p[ it_t ] = p[it_t + 1];return;}void reverse () {for (size_t it = 0; it < (Size >> 1); it++) {type tt = p[ it ];p[ it ] = p[Size - it - 1];p[Size - it - 1] = tt;}return;}void operator ++ () {if (Size == Len) exp ();Size++;return;}void operator ++ (int) {if (Size == Len) exp ();Size++;return;}void operator += (type x) {if (Size == Len) exp ();p[ Size++ ] = x;return;}void operator += (vector <type> vc) {for (size_t i = 0; i < vc.size(); i++) push_back (vc[i]);return;}void operator -- () {if (Size > 0) Size--;return;}void operator -- (int) {if (Size > 0) Size--;return;}void operator -= (size_t ts) {if (Size >= ts) Size -= ts;else Size = 0;return;}void operator = (vector <type> vc) {for (size_t i = 0; i < vc.size(); i++) push_back (vc[i]);return;}};
template <typename type> struct node {type v;node <type>* pre;node <type>* next;node () {pre = next = NULL;}};
template <typename type> class list {public:node <type>* head;node <type>* tail;list () {head = new node <type>;tail = new node <type>;head -> next = tail;tail -> pre = head;}~list () {node <type>* fn = head;node <type>* sn = head;while (fn !=NULL) {sn = sn -> next;delete fn;fn = sn;}}node <type>* begin () {return head;}node <type>* end () {return tail;}void push_back (type x) {node <type>* tn = new node <type>;tn -> v = x;tn -> pre = tail -> pre;tn -> next = tail;tail -> pre -> next = tn;tail -> pre = tn;return;}void push_back (list <type> tl) {node <type>* tn = tl.begin();tn = tn -> next;while (tn != tl.end()) {push_back(tn -> v);tn = tn -> next;}return;}void pop_back () {node <type>* tn = tail -> pre;if (tn != head) {tail -> pre = tn -> pre;tn -> pre -> next = tail;delete tn;}return;}void insert (node <type>* in, type x) {node <type>* tn = new node <type>;tn -> v = x;tn -> pre = in;tn -> next = in -> next;in -> next -> pre = tn;in -> next = tn;return;}void erase (node <type>* en) {node <type>* tn = en -> next;if (tn != tail) {en -> next = tn -> next;tn -> next -> pre = en;delete tn;}return;}void operator += (type x) {node <type>* tn = new node <type>;tn -> v = x;tn -> pre = tail -> pre;tn -> next = tail;tail -> pre -> next = tn;tail -> pre = tn;return;}void operator += (list <type> tl) {node <type>* tn = tl.begin();tn = tn -> next;while (tn != tl.end()) {push_back(tn -> v);tn = tn -> next;}return;}void operator -- () {node <type>* tn = tail -> pre;if (tn != head) {tail -> pre = tn -> pre;tn -> pre -> next = tail;delete tn;}return;}void operator = (list <type> tl) {node <type>* tn = tl.begin();tn = tn -> next;while (tn != tl.end()) {push_back(tn -> v);tn = tn -> next;}return;}};
list <vector <char> >			doc;
node <vector <char> >*			ty;
iterator						tx;
node <vector <char> >*			py;
iterator						px;
short							cnt;
size_t							lines;
char							name[64];
char							path[64];
bool							isat[256];
std::map <std::string, bool>	keyword;
void whelp () {FILE* fp = fopen ("FCDOC\\help.txt", "w");fprintf (fp, helps);fclose (fp);return;}
void rpath () {FILE* fp = fopen ("FCDOC\\path.txt", "r");if (fp == NULL) {printf ("Input path:");gets (path);FILE* sf = fopen ("path.txt", "w");fprintf (sf, "%s", path);fclose (sf);} else {fscanf (fp, "%s", path);}fclose (fp);}
void init (int argc, char** argv) {system ("title FastCode 1.3");system ("mode con lines=32 cols=128");
	if (argc != 1) {int ch;vector <char> tv;FILE* fp = fopen (argv[1], "r");printf ("Loadind...\n");iterator nt = strlen (argv[1]) - 1;for (; nt > 0; nt--) if (argv[1][nt] == '\\') break; for (iterator it = 0; argv[1][nt] != '\0'; it++) name[it] = argv[1][++nt];
	ch = fgetc (fp);while (ch != EOF) {if (ch == 10) {node <vector <char> >* tn = new node <vector <char> >;for (iterator it = 0; it < tv.size(); it++) tn -> v.push_back (tv[it]);tn -> pre = doc.tail -> pre;tn -> next = doc.tail;doc.tail -> pre -> next = tn;doc.tail -> pre = tn;while (tv.size () != 0) tv.pop_back ();}else if (ch == 9) {tv += ' ';tv += ' ';tv += ' ';tv += ' ';}else {tv += ch;}printf ("%c", ch);ch = fgetc(fp);}
	if (tv.size () != 0) {node <vector <char> >* tn = new node <vector <char> >;for (iterator it = 0; it < tv.size(); it++) tn -> v.push_back (tv[it]);tn -> pre = doc.tail -> pre;tn -> next = doc.tail;doc.tail -> pre -> next = tn;doc.tail -> pre = tn;}fclose(fp);}
	else {printf ("Input name:");vector <char> tv;doc += tv;gets (name);}char ts[64];sprintf (ts, "title %s", name);system (ts);
	ty = doc.begin () -> next;tx = 0;py = doc.begin () -> next;px = 0;CONSOLE_CURSOR_INFO cursor_info = {1, 0};SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
	isat['~'] = true;isat['!'] = true;isat['%'] = true;isat['^'] = true;isat['&'] = true;isat['*'] = true;isat['('] = true;isat[')'] = true;isat['-'] = true;isat['+'] = true;isat['='] = true;isat['{'] = true;isat['}'] = true;isat['['] = true;isat[']'] = true;isat['|'] = true;isat[':'] = true;isat[';'] = true;isat['<'] = true;isat['>'] = true;isat[','] = true;isat['.'] = true;isat['?'] = true;isat['/'] = true;
	keyword["auto"] = true;keyword["static"] = true;keyword["extern"] = true;keyword["signed"] = true;keyword["unsigned"] = true;keyword["void"] = true;keyword["bool"] = true;keyword["char"] = true;keyword["short"] = true;keyword["int"] = true;keyword["long"] = true;keyword["float"] = true;keyword["double"] = true;keyword["struct"] = true;keyword["class"] = true;keyword["operator"] = true;keyword["template"] = true;keyword["typename"] = true;keyword["inline"] = true;keyword["typedef"] = true;keyword["typeof"] = true;keyword["sizeof"] = true;keyword["new"] = true;keyword["delete"] = true;keyword["true"] = true;keyword["false"] = true;keyword["using"] = true;keyword["namespace"] = true;keyword["public"] = true;keyword["protected"] = true;keyword["private"] = true;keyword["if"] = true;keyword["else"] = true;keyword["for"] = true;keyword["while"] = true;keyword["do"] = true;keyword["return"] = true;keyword["switch"] = true;keyword["case"] = true;keyword["default"] = true;keyword["break"] = true;
	system ("cls");return;}
void save () {FILE* fp = fopen(name, "w");node <vector <char> >* sp = doc.begin () -> next;while (sp != doc.end ()) {for (iterator it = 0; it < sp -> v.size(); it++)fputc (sp -> v[it], fp);fputc ('\n', fp);sp = sp -> next;}fclose(fp);return;}
void output () {gotoxy (0, 0);node <vector <char> >* pt = py;char prts[128];
	for (iterator yy = 0; yy < 31 && pt != doc.end (); yy++) {iterator xx;bool isinc = false;bool isstr = false;bool isch = false;bool isnum = false;std::string ks;color(112);printf ("%4d ", lines + yy);
		for (xx = 0; xx < 121 && xx + px < pt -> v.size (); xx++) {char putch = pt -> v.at(xx + px);
			if ((!isinc) && (!isstr) && (!isch)) {
				if ((putch >= 'a' && putch <= 'z') || (putch >= '0' && putch <= '9') || putch == '_') ks += putch;
				else {if (keyword [ks] == true) {memset (prts, '\b', sizeof(prts));prts [ks.size ()] = '\0';printf (prts);for (iterator it = 0; it < ks.size(); it++) {if (pt == ty && xx + px - ks.size() + it == tx) color (240);else color(11);printf ("%c", ks[it]);}}ks = "";}
			} else ks = "";
			if (putch == ' ' || isat[putch] || xx + px == 0) isnum = true;else if(putch < '0' || putch > '9') isnum = false;
			if		((!isstr) && (!isch)  && (isinc || putch == '#'))  {color (10);isinc = true;}
			else if	((!isinc) && (!isch)  && (isstr || putch == '\"')) {color (11);if (pt -> v[xx + px] == '\"')isstr = !isstr;}
			else if	((!isinc) && (!isstr) && (isch  || putch == '\'')) {color (14);if (pt -> v[xx + px] == '\'')isch  = !isch;}
			else if	((!isinc) && (!isstr) && (!isch) && (isnum) && (putch >= '0' && putch <= '9')) color (11);
			else if	(isat[putch]) color(9);
			else color (15);
			if (pt == ty && xx + px == tx) color (240);
			printf ("%c", putch);}
		if (pt == ty && tx == pt -> v.size ())color (240);else color (15); printf (" ");color (15);memset(prts, ' ', sizeof (prts));prts[121 - xx] = '\0';printf ("%s\n", prts);pt = pt -> next;
	}printf ("%126c", ' ');return;}
void input () {int ch;
	while (true) {output ();ch = getch ();
		if (ch == 8) {
			if (tx == 0) {if (ty -> pre != doc.begin ()) {ty = ty -> pre;tx = ty -> v.size();for (iterator it = 0; it < ty -> next -> v.size (); it++)ty -> v += ty -> next -> v[it]; doc.erase (ty);cnt--;}}
			else {ty -> v.erase (tx - 1);tx--;}
		} else if (ch == 9) {iterator it = 4;while (it--) {ty -> v.insert (tx, ' ');tx++;}
		} else if (ch == 10) {char prt[128];memset(prt, '\b', sizeof(prt));prt[127] = '\0';printf("%s:", prt);ch = getchar ();
			switch (ch) {
				case '?': {printf (helps);system ("pause");break;}
				case 's': {save ();break;}
				case 'q': {save ();return;}
				case '!': {return;}
				case 'o': {save ();char calls[128];sprintf (calls, "%s -o3 %s", path, name);printf("%s\n", calls);system (calls);system ("start 3.exe");system ("pause");break;}
				case '^': {tx = 0;lines = 0;cnt = 0;ty = doc.begin () -> next;py = doc.begin () -> next;break;}
				case '@': {int linenum;scanf ("%d", &linenum);tx = 0;lines = 0;cnt = 0;ty = doc.begin () -> next;py = doc.begin () -> next;for (iterator it = 0; it < linenum; it++) {if (ty -> next != doc.end ()) {ty = ty -> next;py = py -> next;lines++;}}break;}
				case '$': {char calls[128];getchar ();gets (calls);system (calls);system ("pause");break;}
				case '=': {std::string TempBin;HGLOBAL hMemBin = NULL;PCHAR LockBin = NULL;node <vector <char> >* tn = doc.begin () -> next;OpenClipboard(NULL);EmptyClipboard();while (tn != doc.end ()) {for (iterator it = 0; it < tn -> v.size (); it++) TempBin += tn -> v[it];TempBin += '\n';tn = tn -> next;}hMemBin = GlobalAlloc(GMEM_MOVEABLE, TempBin.size() + 1);LockBin = (PCHAR)GlobalLock(hMemBin);RtlMoveMemory(LockBin, TempBin.c_str(), TempBin.size() + 1);GlobalUnlock(hMemBin);LockBin = NULL;SetClipboardData(CF_TEXT, hMemBin);CloseClipboard();break;}
				default:break;
			}CONSOLE_CURSOR_INFO cursor_info = {1, 0};SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);system ("cls");
		} else if (ch == 13) {node <vector <char> >* tn = new node <vector <char> >;
			for (iterator it = tx; it < ty -> v.size (); it++) tn -> v += ty -> v[it];
			ty -> v -= (ty -> v.size() - tx);
			tn -> pre = ty;tn -> next = ty -> next;ty -> next -> pre = tn;ty -> next = tn;
			ty = ty -> next;tx = 0;cnt++;
		} else if (ch == '(')  {ty -> v.insert (tx, (char)ch);tx++;ty -> v.insert (tx, ')');
		} else if (ch == '[')  {ty -> v.insert (tx, (char)ch);tx++;ty -> v.insert (tx, ']');
		} else if (ch == '{')  {ty -> v.insert (tx, (char)ch);tx++;ty -> v.insert (tx, '}');
		} else if (ch == '\"') {ty -> v.insert (tx, (char)ch);tx++;ty -> v.insert (tx, '\"');
		} else if (ch == '\'') {ty -> v.insert (tx, (char)ch);tx++;ty -> v.insert (tx, '\'');
		} else if (ch == 224) {ch = getch ();
			switch (ch) {
				case UP: {if (ty -> pre != doc.begin ()) {ty = ty -> pre;tx = min (tx, ty -> v.size ());cnt--;}break;}
				case DOWN: {if (ty -> next != doc.end ()) {ty = ty -> next;tx = min (tx, ty -> v.size ());cnt++;}break;}
				case LEFT: {if (tx > 0) tx--;break;}case RIGHT: {if (tx < ty -> v.size ()) tx++;break;}
			}
		} else {ty -> v.insert (tx, (char)ch);tx++;}
		while (cnt > 30) {lines++;cnt--;py = py -> next;}while (cnt < 0) {lines--;cnt++;py = py -> pre;}
	}return;}
int main (int argc, char** argv) {whelp ();rpath ();init (argc, argv);input ();return 0;}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值