Command Operating System by cdsidi (ComSys)首次发布(版本0.1.2)

#include<iostream>
#include<string>
#include<cstdlib> 
#include<vector>
#include<windows.h>
#include<map>
#include<cstdio>
using std::cin;
using std::cout;
using std::string;
using std::vector;
using std::endl;
using std::map;
/*未知的问题:
System_com::comsolve_com::solve1 switch:default会触发:如果只有一个回车,错误id会是99 
*/
bool is_empty_char(char c){
    return c==' '|| c=='\n';
}
class System_com{
    public:
        class out_com{//字符串输出 
            public:
                void print(string s);//输出 
                void print(string s,int from);//从第from开始输出,一直到最后 
                void println(string s);//输出并换行 
                void println(string s,int from);//从from开始输出,并换行 
        }out;
        class err_com{//错误处理 
            public:
                void throwln(string s);//输出错误信息 
                void throwshut(string s,int timeout);//输出错误信息并且在timeout毫秒后关闭程序 
        }err;
        class in_com{//字符串输入 
            public:
                void read(string &s);//输入(空白作结尾符号) 
                void readln(string &s); //输入(换行作结尾符号) 
        }in;
        class systemio_com{ //依赖于Windows io的功能 
            public:                 
                void windowscmd(string s){//直接在cmd里面执行指令 
                    system(s.c_str());
                }
                void progexit(int code){//退出程序并且错误码为code 
                    exit(code);
                }
                void cleanscr();//清屏 
                void sleep(int k){//暂停k毫秒 
                    Sleep(k);
                } 
        }io,sysio;
        class fileio_com{//文件输入输出(还没写) 
        }fileio;
        class welcome_com{//系统欢迎与用户界面(并不是系统核心) 
            public:
                void sysinfo();//系统信息 
                void startsys();//启动和初始化 
                void startscr();//开启屏幕 
                void closescr();//关闭屏幕 
                void closesys();//关闭系统 
                void closetimeout(int m_secs);//延迟关闭(毫秒) 
                void sysmain();//系统运行的重要内容 
                void help();//输出帮助信息
                void advertisements();//输出广告 
        }welcome,wel;
        class storage_com{//存储管理 
            public:
                static const int code_void=0,//代号:没有类型 
                                code_integer=1,//整数 
                                code_floating=2,//浮点数 
                                code_string=3;//字符串 
                struct varinfo{//保存变量信息的数据结构 
                    string name;
                    int type;
                    int address;
                    varinfo(int a=0,int b=0):type(a),address(b){}
                    varinfo(string a,int b,int c):name(a),type(b),address(c){}
                };
                typedef long long Integer;//Integer:long long
                typedef double Floating;//Floating: double
                typedef string String;//String: string
                vector<Integer> Ints;//保存Integer 的数组 
                vector<Floating> Floats;//保存Floating的数组 
                vector<String> Strings;//保存String的数组 
                map<string,varinfo> varmap;//string->varinfo的映射 
            public://work for int only
                storage(){
                    //init();
                }
            //    void init();
                int gettype(string name);//获得变量名name的类型 
                void putvalue(string name,int k);//将变量名name的值改为k 
                int getInteger(string name);//获得name的int值 
                void solve0(string command);
        }var,storage;
        class comsolve_com{//指令处理 
            private://static const int code_
                static const int code_unknown=0;//错误指令 
                static const int code_print=1; 
                static const int code_windowscmd=2;
                static const int code_exit=3;
                static const int code_sysinfo=4;
                static const int code_help=5;
                static const int code_var=6,
                            code_empty=7;
                static const int Max_basic_command=100;//最大命令数目(现在只有8个指令,还早着呢) 
                string basic_command[Max_basic_command];//指令代号->指令名的映射 
                map<string,int> commap;//指令名->指令代号的映射 
            public:
                comsolve_com(){
                    init();
                }
                void init();//初始化 
                void solve0(string s);//第一步处理 
                void solve1(string s,int left,int code);//第二步命令处理 
        }comsolve,comwork,coms,comw;
}System;
//下面是这个类的成员函数 
//void System_com::_com::
void System_com::out_com::print(string s){
    print(s,0);
}
void System_com::out_com::println(string s){
    print(s);cout<<endl;
}
void System_com::out_com::print(string s,int from){
    int len=s.length();
    string s1=s.substr(from,len-from);
    cout<<s1;
}
void System_com::out_com::println(string s,int from){
    print(s,from);cout<<endl;
}
void System_com::in_com::read(string &s){
    cin>>s;
}
void System_com::in_com::readln(string &s){
    getline(cin,s);
}
void System_com::err_com::throwln(string s){
    System.sysio.windowscmd("color 1f"); //1 3 9
    System.wel.sysinfo();
    System.out.println("System Error:There is a problem:");
    System.out.println(s);
}
void System_com::err_com::throwshut(string s,int timeout){
    System.err.throwln(s);
    System.wel.closetimeout(timeout);
} 
void System_com::welcome_com::sysinfo(){
    System.out.println("[Command Operatoring System]");
    System.out.println("[This system is designed by cdsidi.]");
    System.out.println("[Version:0.1.2]");
    /************更新日志*************************** 
    0.0.1:    基本的框架和命令基础 
    0.0.2: 优化了GUI,添加了启动和关闭画面,为fileio做下早期准备
    0.1.0: 在底层支持了Integer类型 
    0.1.1: 优化了用户交互、完善了代码注释 
    0.1.2: 添加了广告 
    ***********************************************/ 
}
void System_com::welcome_com::advertisements(){
    System.out.println("****************以下是广告*************");
    System.out.println("如果喜欢这个系统,请捐助我 ");    
    System.out.println("如果想投放广告,请联系我 ");
    System.out.println("https://www.cnblogs.com/cdsidi");
    System.out.println("51 CSB 派对!");
    System.out.println("****************广告已结束*************");
}
void System_com::comsolve_com::solve1(string s,int left,int code){
    int len=s.length(),tmplen=len-left;
    string tmp=s.substr(left,len-left);//从left一直截取到len(下一步处理的tmp) 
    switch(code){
        case code_unknown:
            System.out.println("Bad command. \n Please enter 'help' for help of commands.");break;
        case code_print:
            System.out.println(tmp);break;
        case code_windowscmd:
            System.sysio.windowscmd(tmp);break;
        case code_exit:
            System.wel.closesys();break;
        case code_sysinfo:
            System.welcome.sysinfo();break;
        case code_help:
            System.welcome.help();break;
        case code_var:
            System.storage.solve0(tmp);break;
        case code_empty:
            System.out.println("EMPTY.");break;
        default://这里有问题! 
            printf("Bad command.\n");
            //printf("Bad command. id:%d\n",code);break;
    }
}
void System_com::comsolve_com::solve0(string s){
    int len=s.length();
    for(int i=0;i<len;++i){//枚举结尾
        if(is_empty_char(s[i])){//空白[i]
            string tmp=s.substr(0,i);//不算空白([0,i-1]是否达到 )
            if(commap[tmp]!=0){
                solve1(s,i+1,commap[tmp]);//转交下层处理,[i+1,end]跳过空白 
                return;//该命令运行完毕 
            }
        }
    }
    //中间没有遇到任何命令, 
    solve1(s,len,commap[s]);
    //left=len:传递一个空串
    //若是正常命令,则执行,否则BADCOMMAND     
}
void System_com::comsolve_com::init(){
    basic_command[code_print]=string("print"); 
    basic_command[code_windowscmd]=string("windows");
    basic_command[code_exit]=string("exit");
    basic_command[code_sysinfo]=string("sysinfo");
    basic_command[code_help]="help";
    basic_command[code_var]="var";
    basic_command[code_empty]=""; 
    for(int i=0;i<Max_basic_command;++i){
        commap[basic_command[i]]=i;
    }
}
void System_com::systemio_com::cleanscr(){
    system("cls");
}
void System_com::welcome_com::startscr(){//启动屏幕 
    int cnt_stars=80,cnt_emptyl=10,waitms=3000;
    System.sysio.cleanscr();
    for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("\n");
    System.welcome.sysinfo();
    System.welcome.advertisements();
    for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("\n");
    for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    System.out.print("\n");
    System.out.print("Starting...");
    System.sysio.sleep(waitms);
    System.sysio.cleanscr();
    System.out.println("System has got ready.");
}
void System_com::welcome_com::closescr(){
    System.sysio.cleanscr();
    int cnt_stars=80,cnt_emptyl=10,waitms=3000;
    for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("\n");
    System.welcome.sysinfo();
    System.welcome.advertisements();
    for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("\n");
    for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    System.out.print("\n");
    System.out.println("System is shutting down.");
    System.sysio.sleep(waitms);
    //System.sysio.cleanscr();
}
void System_com::welcome_com::help(){
    System.out.println("************Start of help*****************");
    System.out.println("1.    command 'print': ");
    System.out.println("print {string} System will say {string}.");
    System.out.println("eg. print Hello!");
    System.out.println("2.    command 'exit': ");
    System.out.println("System will close.");
    System.out.println("eg. exit");
    System.out.println("3.    command 'sysinfo': ");
    System.out.println("System will show the information of the System.");
    System.out.println("eg. sysinfo");
    System.out.println("4.    command 'help': ");
    System.out.println("System will show the commands of the System.");
    System.out.println("eg. help");
    System.out.println("5.    command 'windows': ");
    System.out.println("windows {string} system will do it in Windows cmd.exe");
    System.out.println("eg. windows echo hello");    
    System.out.println("************End of help*****************");
}
void System_com::welcome_com::startsys(){
    System.sysio.windowscmd("color 0f");
    System.wel.startscr(); 
    System_com System;
    System.wel.sysmain();
} 
void System_com::welcome_com::closesys(){
    System.wel.closescr();
    System.out.println("Press any key to exit.");
    System.sysio.windowscmd("pause >nul");
    System.sysio.progexit(0);
}
void System_com::welcome_com::closetimeout(int m_secs){
    printf("System will shutdown in %d seconds. Please get ready.",m_secs/1000);
    Sleep(m_secs);
    System.wel.closesys();
}
void System_com::welcome_com::sysmain(){
    string s;
    while(1){
        System.out.print(">");
        System.in.readln(s);
        System.comsolve.solve0(s);
    }
}
int System_com::storage_com::getInteger(string name){
    varinfo var=varmap[name];
    if(var.type==code_integer){
        return Ints[var.address];
    }else{
    //    System.err.throwshut("Error Type.");
        return 0;
    }
}
void System_com::storage_com::putvalue(string name,int val){
    varinfo var=varmap[name];
    if(var.type==code_void){
        Ints.push_back(val);
        var=varinfo(name,code_integer,Ints.size()-1);
        varmap[name]=var;
    }else if(var.type==code_integer){//此变量存在 
        Ints[var.address]=val;
    }else{//注意,以下会出现内存泄露 
        Ints.push_back(val);
        var=varinfo(name,code_integer,Ints.size()-1);
        varmap[name]=var;//将之前的存储信息覆盖 
    }    
}
int System_com::storage_com::gettype(string name){
    varinfo ret=varmap[name];
    return ret.type;    
}/*下面这里我没看明白 ,先暂时不用了 */
void System_com::storage_com::solve0(string s){//s是storage命令处理 
    int len=s.length();
    for(int i=0;i<len;++i){//枚举i,一直到结尾 
        if(!is_empty_char(s[i])){//空白[i]
            string tmp=s.substr(0,i);//不算空白[0,i-1]是否找到变量名
            varinfo tmpinfo=varmap[tmp]; 
            if(tmpinfo.type!=0){//如果这个变量存在 
                //solve1(s,i+1,tmpinfo);//转交下层处理,[i+1,end]跳过空白 
                return;//该命令运行完毕 
            }
        }
    }
    System.out.println("Bad type.");
}
int main(){
    System_com::welcome_com tmp;tmp.startsys();
    return 0;
}
View Code

ComSys 0.1.2 代码首次发布

请各位大神不吝赐教。

目前可用的命令

print {string}

windows

exit

sysinfo

help

程序中间有很多奇怪的bug,暂时无法解决

程序介绍:

这个程序的所有功能包含在了class System_com里面,通过class System_com System变量实现操作

class System_com 包含的目前有用的类:

class out_com 输出

class err_com 错误信息

class in_com 输入

class systemio_com

class welcome_com

class storage_com (目前仅支持Integer 类型)

class comsolve_com 处理用户输入的命令

下一次更新预告

1.可能实现多文件

2.添加支持隐藏的命令

3.可能会实现Stoarge

猜你喜欢

转载自www.cnblogs.com/cdsidi/p/12530036.html