自制控制台编辑器
  • 板块灌水区
  • 楼主LZCH
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/11/22 16:35
  • 上次更新2023/11/3 23:45:16
查看原帖
自制控制台编辑器
473862
LZCH楼主2021/11/22 16:35

自制了一个控制台代码编辑器,可以编辑,链接编译器,谁能帮忙改一下,解决掉频闪的问题?谢谢

#include <iostream>
#include <windows.h>
#include <fstream>
#include <cstring>
#include <conio.h>
using namespace std;
///优先级:1."" 2.注释 3.# 4.关键字 5.数字(4,5并列)
class guanjianzi{       //这个类主要用于判断字符串是不是关键字。
public://关键字
    string guanjian[64];
    guanjianzi(){
        guanjian[1] = "asm";
        guanjian[2] = "auto";
        guanjian[3] = "bool";
        guanjian[4] = "break";
        guanjian[5] = "case";
        guanjian[6] = "catch";
        guanjian[7] = "char";
        guanjian[8] = "class";
        guanjian[9] = "const";
        guanjian[10] = "const_cast";
        guanjian[11] = "continue";
        guanjian[12] = "default";
        guanjian[13] = "delete";
        guanjian[14] = "do";
        guanjian[15] = "double";
        guanjian[16] = "dynamic_cast";
        guanjian[17] = "else";
        guanjian[18] = "enum";
        guanjian[19] = "explicit";
        guanjian[20] = "export";
        guanjian[21] = "extern";
        guanjian[22] = "false";
        guanjian[23] = "float";
        guanjian[24] = "for";
        guanjian[25] = "friend";
        guanjian[26] = "goto";
        guanjian[27] = "if";
        guanjian[28] = "inline";
        guanjian[29] = "int";
        guanjian[30] = "long";
        guanjian[31] = "mutable";
        guanjian[32] = "namespace";
        guanjian[33] = "new";
        guanjian[34] = "operator";
        guanjian[35] = "private";
        guanjian[36] = "protected";
        guanjian[37] = "public";
        guanjian[38] = "register";
        guanjian[39] = "reinterpret_cast";
        guanjian[40] = "return";
        guanjian[41] = "short";
        guanjian[42] = "signed";
        guanjian[43] = "sizeof";
        guanjian[44] = "static";
        guanjian[45] = "static_cast";
        guanjian[46] = "struct";
        guanjian[47] = "switch";
        guanjian[48] = "template";
        guanjian[49] = "this";
        guanjian[50] = "throw";
        guanjian[51] = "true";
        guanjian[52] = "try";
        guanjian[53] = "typedef";
        guanjian[54] = "typeid";
        guanjian[55] = "typename";
        guanjian[56] = "union";
        guanjian[57] = "unsigned";
        guanjian[58] = "using";
        guanjian[59] = "virtual";
        guanjian[60] = "void";
        guanjian[61] = "volatile";
        guanjian[62] = "wchar_t";
        guanjian[63] = "while";
    }
    bool isguanjian(string word){
        for(int i=1;i<=63;i++){
            if(guanjian[i] == word){
                return true;
            }
        }
        return false;
    }/*
private:
    bool erfen(string word,int l, int r){
        if(l+1 <= r){
            if(word == guanjian[l]||word == guanjian[r]){
                return true;
            }
            else{
                return false;
            }
        }
        else if(l>r){
            cout<<"二分查找关键字错误"<<endl;
            return false;
        }
        else if(word<=((guanjian[l])+(guanjian[r])/2)){
            return erfen(word,l,r-((r-l)/2));
        }
        else if(word>((guanjian[l])+(guanjian[r])/2)){
            return erfen(word,l+((r-l)/2),r);
        }
    }*/
};







class point{
public:
    int x,y,number;
    point(){
        x = 0; y = 0;number = 0;
    }
};
class CODE{
public:
    guanjianzi g;
    point screen,mouseplace;
    int screenstart;
    bool zhushi,jinghao,yinhao,guan,shuzi,zhushi2;
    string code;
    CODE(){
        screen.x = 40;
        screen.y = 40;
        mouseplace.x = 1;
        mouseplace.y = 1;
        screenstart = 1;
        zhushi=false,jinghao=false,yinhao=false,guan=false,shuzi=false,zhushi2=false;
    }
    void show(){
        zhushi=false,jinghao=false,yinhao=false,guan=false,shuzi=false,zhushi2=false;
        int next;
        next = 0;
        system("cls");
        system("color");
        cout<<"x:"<<mouseplace.x<<" y:"<<mouseplace.y<<"  screen end:"<<screenstart+screen.y-1<<' '<<mouseplace.y-screen.y+1+screen.y<<endl;
        int x,y,i;
        int xx,yy;
        x = 1;y = 1;xx=1;yy=1;
        //bool zhushi=false,jinghao=false,yinhao=false,guan=false,shuzi=false,zhushi2=false;//对应显示中的五个优先级,zhushi2表示多行注释,另一个表示单行注释。
        if(mouseplace.y<1){
            mouseplace.y = 1;
        }
        if(mouseplace.y<screenstart+3){
            screenstart = mouseplace.y-3;
            if(screenstart<=0){
                screenstart = 1;
            }
        }
        else if(mouseplace.y+3>(screenstart+screen.y-1)){
            //     5           2         3           4
            screenstart = mouseplace.y-screen.y+3;
        }
        bool yinhao1 = false,yinhao2 = false;//双引号和单引号。




        if(mouseplace.x<1){
            mouseplace.x = 1;
        }
        for(i=0;i<strlen(code.c_str());i++){
            //cout<<code[i];
            bool yz = false;
            if(yy >= screenstart && yy <= screenstart+screen.y-1){//在屏幕范围内
                /*if(xx>screen.x&&code[i]!='\n'){
                    cout<<endl;
                }*/
                if(code[i] == '\n'){
                    if(mouseplace.x<0){
                        mouseplace.x = 0;
                    }
                    else if(mouseplace.x>x&&mouseplace.y == y){
                        mouseplace.x = x;
                    }
                    if(mouseplace.x == x && mouseplace.y == y){
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xf0);
                        mouseplace.number = i;
                    }
                    cout<<' ';
                    if(mouseplace.x == x && mouseplace.y == y){
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
                    }
                    cout<<endl;
                    jinghao = false;zhushi = false;yinhao = false;yinhao1 = false;yinhao2 = false;
                }
                else{
                    if(code[i] == '/'&&yinhao!=true){
                        if(code[i-1] != '*'){
                            if(code[i+1] == '/'&&zhushi2!=true){
                                zhushi = true;
                            }
                            else if(code[i+1] == '*'&&zhushi!=true){
                                zhushi2 = true;
                            }
                        }
                    }
                    else if(code[i] == '#'&&yinhao!=true&&zhushi!=true&&zhushi2!=true){
                        jinghao = true;
                    }
                    else if(code[i] == '"'&&zhushi!=true&&zhushi2!=true&&jinghao!=true){
                        if(yinhao!=true&&yinhao1!=true){
                            yinhao1 = true;
                            yinhao = true;
                            yz=true;
                        }
                    }
                    else if(code[i] == '\''&&zhushi!=true&&zhushi2!=true&&jinghao!=true){
                        if(yinhao!=true&&yinhao1!=true){
                            yinhao2 = true;
                            yinhao = true;
                            yz=true;
                        }
                    }//关键字和数字
                    else if(code[i]>='0'&&code[i]<='9'&&(!((code[i-1]>='a'&&code[i-1]<='z')||(code[i-1]>='A'&&code[i-1]<='Z')))&&(code[i-1]!='_')){
                        shuzi = true;
                    }
                    else{
                        if(((code[i]>='a'&&code[i]<='z')||(code[i]>='A'&&code[i]<='Z'))&&(!((code[i-1]>='a'&&code[i-1]<='z')||(code[i-1]>='A'&&code[i-1]<='Z')))&&(code[i-1]!='_')){
                            string gtp;///加入gtp然后判断关键。
                            int j = i;
                            while((code[j]>='a'&&code[j]<='z')||(code[j]>='A'&&code[j]<='Z')){
                                gtp+=code[j];
                                j++;
                            }
                            if(isguanjian(gtp)){
                                guan = true;/*****************************************************/
                                next = strlen(gtp.c_str());
                            }
                        }
                    }
                    setRightColor();
                    if(mouseplace.x == x && mouseplace.y == y){
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xf0);
                        mouseplace.number = i;
                    }
                    //cout<<yinhao1;
                    if(code[i]!='\t'){
                        cout<<code[i];
                    }
                    else if(code[i] == '\t'){
                        cout<<" ";
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
                        cout<<"   ";
                    }
                    next--;
                    if(mouseplace.x == x && mouseplace.y == y){
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
                    }

                    if(!((code[i]>='a'&&code[i]<='z')||(code[i]>='A'&&code[i]<='Z'))){
                        guan = false;
                    }
                    if(!(code[i]>='0'&&code[i]<='9')){
                        shuzi = false;
                    }
                    if(code[i] == '/'){
                        if(code[i-1] == '*'&&code[i-2]!='/'){
                            zhushi2 = false;
                        }
                    }
                    if(code[i] == '"'){
                        if(yinhao1 == true && yinhao == true && yz!=true){
                            if(code[i-1]!='\\'){
                                yinhao1=false;yinhao=false;
                            }
                        }
                    }
                    if(code[i] == '\''){
                        if(yinhao2 == true && yinhao == true && yz!=true){
                            if(code[i-1]!='\\'){
                                yinhao2=false;yinhao=false;
                            }
                        }
                    }
                    //cout<<yinhao;

                    if(next <= 0){
                        guan = false;
                    }
                    shuzi = false;
                }
            }


            if(xx>screen.x&&code[i]!='\n'){
                xx = 1;yy++;
            }
            if(code[i] == '\n'){
                x = 1;xx=1;
                y++;yy++;
            }
            else{
                x++;xx++;
            }

            if((i == strlen(code.c_str())-1 )&& (mouseplace.y>y)){
                mouseplace.y = y;
                this->show();
            }
            if((i == strlen(code.c_str())-1) && (mouseplace.y == y) && (mouseplace.x>=x)){
                mouseplace.x = x-1;
                this->show();
            }
        }
    }
    bool read(string dizhi){
        fstream _file;
        _file.open(dizhi.c_str(), ios::in);
        if(_file)//if(_file)
        {
            ifstream r_in(dizhi.c_str());
            code = "";
            while(r_in){
                string stp;
                getline(r_in,stp);
                code += stp;
                code += "\n";
            }
            return true;
        }
        else{
            return false;
        }
    }
    bool write(string dizhi){
        /*
        fstream _file;
        _file.open(dizhi.c_str(), ios::in);
        if(_file){
            return false;
        }
        _file.close();
        */
        ofstream write_o(dizhi.c_str());
        write_o<<code;
        return true;
    }
    bool input(char c){
        /*
        if(mouseplace.number <= strlen(code.c_str())){
            for(int i=strlen(code.c_str());i>=mouseplace.number;i--){
                code[i+1] = code[i];
            }
            code[mouseplace.number] = c;
            mouseplace.x++;
            return true;
        }
        else{
            return false;
        }*/
        string stp;
        stp+=c;
        code.insert(mouseplace.number,stp);
        if(c!='\n'){
            mouseplace.x++;
        }
        else{
            mouseplace.x = 1;
            mouseplace.y++;
        }
    }
    bool backspace(){
        if(mouseplace.number > 0){
            for(int i=mouseplace.number-1;i<=strlen(code.c_str())-1;i++){
                code[i] = code[i+1];
            }
            mouseplace.x--;
        }
        return true;
    }
    bool del(){
        if(mouseplace.number < strlen(code.c_str())&&(code[mouseplace.number+1]!='\0')){
            for(int i=mouseplace.number;i<=strlen(code.c_str())-1;i++){
                code[i] = code[i+1];
            }
        }
        return true;
    }
    void setRightColor(){
        //bool zhushi=false,jinghao=false,yinhao=false,guan=false,shuzi=false,zhushi2=false;//对应显示中的五个优先级,zhushi2表示多行注释,另一个表示单行注释。
        if(yinhao == true){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0d);
        }
        else if(zhushi2 == true){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0a);///0x09
        }
        else if(zhushi == true){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0a);///
        }
        else if(jinghao == true){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0a);///
        }
        else if(guan == true){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0b);
        }
        else if(shuzi == true){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0a);///
        }
        else{
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
        }
    }
private:
    bool isguanjian(string word){
        return g.isguanjian(word);
    }
};
class COM{
public:
    string dizhi;
    COM(){
        fstream _file;
        _file.open("com.lpp", ios::in);
        if(_file){//if(_file){
            ifstream comin("com.lpp");
            comin>>dizhi;
        }
    }
    bool cpl(string ndz){
        dizhi = ndz;
        ofstream comout("com.lpp");
        comout<<ndz;
    }
    bool cplace(string ndz){
        cpl(ndz);
    }
    bool run(string wjdz){
        string mingling;
        mingling+="\"";
        mingling += dizhi;
        mingling += " ";
        mingling += wjdz;
        mingling += " -o ";
        for(int i=0;wjdz[i]!='.'&&i<strlen(wjdz.c_str());i++){
            mingling += wjdz[i];
        }
        mingling += ".exe";
        mingling += "\"";
        system(mingling.c_str());
        return 0;
    }
};









int main(int argc, char** argv){
    cout<<"这是Liu++1.0.7 编辑器,由刘梓宸于2021年11月10日16点48分完成。\n这个程序的源代码很快就会开放。\n按F8查看帮助。"<<endl;
    Sleep(1000);
    system("pause");
    system("cls");


    CODE code;
    //code.read("C:\\Users\\Administrator\\Desktop\\阻止关机.cpp");
    char c1,c2;
    code.code = " ";
    //code.show();
    COM com;
    string place = "";
    char *place2;
    if(argc==2){
        place = argv[1];
        code.read(place);
        //cout<<place;
    }
    //system("pause");
    code.show();
    while(1){
        while(!kbhit()){}
        c1 = getch();
        if(kbhit()){
            c2 = getch();
            //cout<<"kbhit";
            ///上下左右F1F2等判断(F键用于判断编译、保存类指令)
            if(c1 == -32 && c2 == 72){
                ///上
                code.mouseplace.y--;
                //cout<<"up";
            }
            else if(c1 == -32 && c2 == 80){
                ///下
                code.mouseplace.y++;
                //cout<<"down";
            }
            else if(c1 == -32 && c2 == 75){
                code.mouseplace.x--;
                //cout<<"left";
            }
            else if(c1 == -32 && c2 == 77){
                code.mouseplace.x++;
                //cout<<"right";
            }
            else if(c1 == -32 && c2 == 83){
                code.del();
            }//f键:编译、运行、保存、打开、更改编译器地址、退出
            else if(c1 == 0){
                if(c2 == 59){
                    system("cls");
                    if(place!="")com.run(place);
                    else{
                        cout<<"请输入文件保存的地址。";
                        cin>>place;
                        code.write(place);
                        com.run(place);
                    }
                    system("pause");
                    code.show();
                }
                else if(c2 == 60){
                    system("cls");
                    string mingling;
                    for(int i=0;place[i]!='.'&&i<strlen(place.c_str());i++){
                        mingling += place[i];
                    }
                    mingling += ".exe";
                    system(mingling.c_str());
                    cout<<endl;
                    system("pause");
                    code.show();
                }
                else if(c2 == 61){
                    system("cls");
                    if(place!="")code.write(place);
                    else{
                        cout<<"请输入文件保存的地址。";
                        cin>>place;
                        code.write(place);
                    }
                    code.show();
                }
                else if(c2 == 62){
                    system("cls");
                    cout<<"请输入文件的地址。";
                    cin>>place;
                    code.read(place);
                    code.show();
                }
                else if(c2 == 63){
                    system("cls");
                    cout<<"请输入编译器的地址";
                    string stp;
                    cin>>stp;
                    com.cplace(stp);
                    code.show();
                }
                else if(c2 == 64){
                    system("cls");
                    cout<<"请输入文件保存的地址。";
                    cin>>place;
                    code.write(place);
                    code.show();
                }
                else if(c2 == 65){
                    system("cls");
                    cout<<"请输入屏幕的长度(字符个数):";
                    int tpi;
                    cin>>tpi;
                    code.screen.y = tpi;
                }
                else if(c2 == 66){
                    system("cls");
                    cout<<"正常的写入,F1编译、F2运行、F3保存、F4打开、F5更改编译器地址、F6另存为、F7更改屏幕长度、F8帮助、F9新建\n";
                    system("pause");
                    code.show();
                }
                else if(c2 == 67){
                    code.code = " ";
                    code.screen.x = 40;
                    code.screen.y = 40;
                    code.mouseplace.x = 1;
                    code.mouseplace.y = 1;
                    code.screenstart = 1;
                    code.zhushi=false,code.jinghao=false,code.yinhao=false,code.guan=false,code.shuzi=false,code.zhushi2=false;
                    place = "";
                    code.show();
                }
            }
            else{
                ///其他输入c2
                if(c2 == 8){
                    code.backspace();
                }
                else{
                    if(c2 == 13){
                        code.input('\n');
                    }
                    else{
                        code.input(c2);
                    }
                }
                ///其他输入c1
                if(c1 == 8){
                    code.backspace();
                }
                else{
                    if(c1 == 13){
                        code.input('\n');
                    }
                    else{
                        code.input(c1);
                    }
                }
            }
        }
        else{
            ///其他输入
            if(c1 == 8){
                code.backspace();
            }
            else{
                if(c1 == 13){
                    code.input('\n');
                }
                else{
                    code.input(c1);
                }
            }
        }
        //cout<<(int)c1<<' '<<(int)c2;
        //cout<<code.mouseplace.x<<' '<<code.mouseplace.y;
        code.show();
        //cout<<code.mouseplace.x<<' '<<code.mouseplace.y;
    }
    /*CODE code;
    code.read("C:\\Users\\86137\\Desktop\\阻止关机.cpp");
    code.show();
    Sleep(500);
    code.mouseplace.x++;
    code.show();
    Sleep(500);
    code.del();
    code.show();
    Sleep(1000);
    code.mouseplace.y = 20;
    code.show();
    //return code.write("C:\\Users\\86137\\Desktop\\阻止关机.cpp");
    //cout<<code.write("C:\\Users\\86137\\Desktop\\阻止关机2.cpp");
    return 0;*/
}

2021/11/22 16:35
加载中...