zoukankan      html  css  js  c++  java
  • Command Operating System by cdsidi(ComSys) 0.1.x版本陆续更新

    0.1.3版本更新:
    1.架构调整:1.将System_com 类拆分,不再从属于System_com 类

          2.将System_com 类的功能拆分为 System_com (基本功能,无附加存储)和Systemext_com(基本拓展,有附加存储)

          3.所有的Systemext 需要初始化的功能都放在自己的class 的void init() 在System.wel.sysint()系统开启之前调用

    2.bug 修复:解决comsolve_com 类输入空行会报错误id99的问题

    3.功能改善:

    1.现在系统init()完毕之后会提示"System has inited"(很短)

    2.按下回车(输入空命令)不会提示任何信息

    3.改善了系统启动进程

    4.去掉了comsolve_com::code_empty 因为已经在solve0中支持

    5.优化了注释和代码美观

      1 #include<iostream>
      2 #include<string>
      3 #include<cstdlib> 
      4 #include<vector>
      5 #include<windows.h>
      6 #include<map>
      7 #include<cstdio>
      8 using std::cin;
      9 using std::cout;
     10 using std::string;
     11 using std::vector;
     12 using std::endl;
     13 using std::map;
     14 bool is_empty_char(char c){
     15     return c==' '|| c=='
    ';
     16 }
     17 class out_com{//字符串输出
     18     public:
     19         void print(string s);//输出 
     20         void print(string s,int from);//从第from开始输出,一直到最后 
     21         void println(string s);//输出并换行 
     22         void println(string s,int from);//从from开始输出,并换行     
     23 };
     24 class err_com{//错误处理 
     25     public:
     26     void throwln(string s);//输出错误信息 
     27     void throwshut(string s,int timeout);//输出错误信息并且在timeout毫秒后关闭程序 
     28 };
     29 class fileio_com{    //文件输入输出(还没写) 
     30 };
     31 class in_com{//字符串输入 
     32     public:
     33         void read(string &s);//输入(空白作结尾符号) 
     34         void readln(string &s); //输入(换行作结尾符号) 
     35 };
     36 class systemio_com{ //依赖于Windows io的功能 
     37     public:                 
     38     void windowscmd(string s){//直接在cmd里面执行指令 
     39         system(s.c_str());
     40     }
     41     void progexit(int code){//退出程序并且错误码为code 
     42         exit(code);
     43     }
     44     void cleanscr();//清屏 
     45         void sleep(int k){//暂停k毫秒 
     46             Sleep(k);
     47     } 
     48 };
     49 class welcome_com{//系统欢迎与用户界面(并不是系统核心) 
     50     public:
     51         void sysinfo();//系统信息 
     52         void startsys();//启动和初始化 
     53         void startscr();//开启屏幕 
     54         void closescr();//关闭屏幕 
     55         void closesys();//关闭系统 
     56         void initsys();//数据结构初始化 
     57         void closetimeout(int m_secs);//延迟关闭(毫秒) 
     58         void sysmain();//系统运行的重要内容 
     59         void help();//输出帮助信息
     60         void advertisements();//输出广告 
     61 };
     62 class storage_com{//存储管理 
     63     public:
     64         static const int code_void=0,//代号:没有类型 
     65                         code_integer=1,//整数 
     66                         code_floating=2,//浮点数 
     67                         code_string=3;//字符串 
     68         struct varinfo{//保存变量信息的数据结构 
     69             string name;
     70             int type;
     71             int address;
     72             varinfo(int a=0,int b=0):type(a),address(b){}
     73             varinfo(string a,int b,int c):name(a),type(b),address(c){}
     74         };
     75         typedef long long Integer;//Integer:long long
     76         typedef double Floating;//Floating: double
     77         typedef string String;//String: string
     78         vector<Integer> Ints;//保存Integer 的数组 
     79         vector<Floating> Floats;//保存Floating的数组 
     80         vector<String> Strings;//保存String的数组 
     81         map<string,varinfo> varmap;//string->varinfo的映射 
     82     public://work for int only
     83         storage(){
     84         }
     85         void init();
     86         int gettype(string name);//获得变量名name的类型 
     87         void putvalue(string name,int k);//将变量名name的值改为k 
     88         int getInteger(string name);//获得name的int值 
     89         void solve0(string command);
     90 };
     91 class comsolve_com{//指令处理 
     92     private://static const int code_
     93         static const int code_unknown=0;//错误指令 
     94         static const int code_print=1; 
     95         static const int code_windowscmd=2;
     96         static const int code_exit=3;
     97         static const int code_sysinfo=4;
     98         static const int code_help=5;
     99         static const int code_var=6,
    100         static const int Max_basic_command=100;//最大命令数目(现在只有8个) 
    101         string basic_command[Max_basic_command];//指令代号->指令名的映射 
    102         map<string,int> commap;//指令名->指令代号的映射 
    103     public:
    104         comsolve_com(){
    105             ;
    106         }
    107         void init();//初始化 
    108         void solve0(string s);//第一步处理 
    109         void solve1(string s,int left,int code);//第二步命令处理 
    110 };
    111 class System_com{//基本功能(几乎无需初始化) 
    112     public:
    113         out_com out;
    114         err_com err;
    115         in_com in;
    116         systemio_com sysio;
    117         fileio_com fileio;
    118         welcome_com wel;
    119 }System;
    120 class Systeme_com{//基本拓展功能(可能需要初始化) 
    121     public:
    122         storage_com var;
    123         comsolve_com comsolve;
    124 }Systemext;
    125 void out_com::print(string s){
    126     print(s,0);
    127 }
    128 void out_com::println(string s){
    129     print(s);cout<<endl;
    130 }
    131 void out_com::print(string s,int from){
    132     int len=s.length();
    133     string s1=s.substr(from,len-from);
    134     cout<<s1;
    135 }
    136 void out_com::println(string s,int from){
    137     print(s,from);cout<<endl;
    138 }
    139 void in_com::read(string &s){
    140     cin>>s;
    141 }
    142 void in_com::readln(string &s){
    143     getline(cin,s);
    144 }
    145 void err_com::throwln(string s){
    146     System.sysio.windowscmd("color 1f"); //color 1 3 9是蓝色 
    147     System.wel.sysinfo();
    148     System.out.println("System Error:There is a problem:");
    149     System.out.println(s);
    150 }
    151 void err_com::throwshut(string s,int timeout){
    152     System.err.throwln(s);
    153     System.wel.closetimeout(timeout);
    154 } 
    155 void welcome_com::sysinfo(){
    156     System.out.println("[Command Operatoring System]");
    157     System.out.println("[This system is designed by cdsidi.]");
    158     System.out.println("[Version:0.1.3]");
    159     /************更新日志*************************** 
    160     0.0.1:    基本的框架和命令基础 
    161     0.0.2: 优化了GUI,添加了启动和关闭画面,为fileio做下早期准备
    162     0.1.0: 在底层支持了Integer类型 
    163     0.1.1: 优化了用户交互、完善了代码注释 
    164     0.1.2: 添加了广告
    165     0.1.3:重构代码,将System里面的类 拆分开来,为多文件编程打下基础 
    166     ***********************************************/ 
    167 }
    168 void welcome_com::advertisements(){
    169     System.out.println("****************以下是广告*************");
    170     System.out.println("如果喜欢这个系统,请捐助我 ");    
    171     System.out.println("如果想投放广告,请联系我 ");
    172     System.out.println("https://www.cnblogs.com/cdsidi");
    173     System.out.println("51C SB 派对!");
    174     System.out.println("****************广告已结束*************");
    175 }
    176 void comsolve_com::solve1(string s,int left,int code){
    177     int len=s.length(),tmplen=len-left;
    178     string tmp=s.substr(left,len-left);//从left一直截取到len(下一步处理的tmp) 
    179     switch(code){
    180         case code_unknown:
    181             System.out.println("Bad command. 
     Please type 'help' for help of commands.");break;
    182         case code_print:
    183             System.out.println(tmp);break;
    184         case code_windowscmd:
    185             System.sysio.windowscmd(tmp);break;
    186         case code_exit:
    187             System.wel.closesys();break;
    188         case code_sysinfo:
    189             System.wel.sysinfo();break;
    190         case code_help:
    191             System.wel.help();break;
    192         case code_var:
    193             Systemext.var.solve0(tmp);break;
    194         default://这里有问题! 
    195             printf("Bad command.
    ");
    196             //printf("Bad command. id:%d
    ",code);break;
    197     }
    198 }
    199 void comsolve_com::solve0(string s){
    200     int len=s.length();
    201     for(int i=0;i<len;++i){//枚举结尾
    202         if(is_empty_char(s[i])){//空白[i]
    203             string tmp=s.substr(0,i);//不算空白([0,i-1]是否达到 )
    204             if(commap[tmp]!=0){
    205                 solve1(s,i+1,commap[tmp]);//转交下层处理,[i+1,end]跳过空白 
    206                 return;//该命令运行完毕 
    207             }
    208         }
    209     }
    210     //中间没有遇到任何命令, 
    211     solve1(s,len,commap[s]);
    212     //left=len:传递一个空串
    213     //若是正常命令,则执行,否则BADCOMMAND     
    214 }
    215 void comsolve_com::init(){
    216     basic_command[code_print]=string("print"); 
    217     basic_command[code_windowscmd]=string("windows");
    218     basic_command[code_exit]=string("exit");
    219     basic_command[code_sysinfo]=string("sysinfo");
    220     basic_command[code_help]="help";
    221     basic_command[code_var]="var";
    222     basic_command[code_empty]=""; 
    223     for(int i=0;i<Max_basic_command;++i){
    224         commap[basic_command[i]]=i;
    225     }
    226 }
    227 void systemio_com::cleanscr(){
    228     system("cls");
    229 }
    230 void welcome_com::startscr(){//启动屏幕 
    231     int cnt_stars=80,cnt_emptyl=10,waitms=3000;
    232     System.sysio.cleanscr();
    233     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    234     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    235     System.wel.sysinfo();
    236     System.wel.advertisements();
    237     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    238     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    239     System.out.print("
    ");
    240     System.out.println("Starting...");
    241     System.sysio.sleep(waitms);
    242 }
    243 void welcome_com::closescr(){
    244     System.sysio.cleanscr();
    245     int cnt_stars=80,cnt_emptyl=10,waitms=3000;
    246     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    247     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    248     System.wel.sysinfo();
    249     System.wel.advertisements();
    250     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    251     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    252     System.out.print("
    ");
    253     System.out.println("System is shutting down.");
    254     System.sysio.sleep(waitms);
    255     //System.sysio.cleanscr();
    256 }
    257 void welcome_com::help(){
    258     System.out.println("************Start of help*****************");
    259     System.out.println("1.    command 'print': ");
    260     System.out.println("print {string} System will say {string}.");
    261     System.out.println("eg. print Hello!");
    262     System.out.println("2.    command 'exit': ");
    263     System.out.println("System will close.");
    264     System.out.println("eg. exit");
    265     System.out.println("3.    command 'sysinfo': ");
    266     System.out.println("System will show the information of the System.");
    267     System.out.println("eg. sysinfo");
    268     System.out.println("4.    command 'help': ");
    269     System.out.println("System will show the commands of the System.");
    270     System.out.println("eg. help");
    271     System.out.println("5.    command 'windows': ");
    272     System.out.println("windows {string} system will do it in Windows cmd.exe");
    273     System.out.println("eg. windows echo hello");    
    274     System.out.println("************End of help*****************");
    275 }
    276 void welcome_com::startsys(){
    277     System.sysio.windowscmd("color 0f");
    278     System.wel.startscr(); 
    279     System.wel.initsys();
    280     System.sysio.cleanscr();
    281     System.out.println("System has got ready.");
    282     System.wel.sysmain();
    283 } 
    284 void welcome_com::initsys(){
    285 
    286     Systemext.comsolve.init();
    287     Systemext.var.init();
    288     System.out.println("System has inited");
    289 }
    290 void welcome_com::closesys(){
    291     System.wel.closescr();
    292     System.out.println("Press any key to exit.");
    293     System.sysio.windowscmd("pause >nul");
    294     System.sysio.progexit(0);
    295 }
    296 void welcome_com::closetimeout(int m_secs){
    297     printf("System will shutdown in %d seconds. Please get ready.",m_secs/1000);
    298     Sleep(m_secs);
    299     System.wel.closesys();
    300 }
    301 void welcome_com::sysmain(){
    302     string s;
    303     while(1){
    304         System.out.print(">");
    305         System.in.readln(s);
    306         if(!s.empty())
    307             Systemext.comsolve.solve0(s);
    308     }
    309 }
    310 int storage_com::getInteger(string name){
    311     varinfo var=varmap[name];
    312     if(var.type==code_integer){
    313         return Ints[var.address];
    314     }else{
    315     //    System.err.throwshut("Error Type.");
    316         return 0;
    317     }
    318 }
    319 void storage_com::putvalue(string name,int val){
    320     varinfo var=varmap[name];
    321     if(var.type==code_void){
    322         Ints.push_back(val);
    323         var=varinfo(name,code_integer,Ints.size()-1);
    324         varmap[name]=var;
    325     }else if(var.type==code_integer){//此变量存在 
    326         Ints[var.address]=val;
    327     }else{//注意,以下会出现内存泄露 
    328         Ints.push_back(val);
    329         var=varinfo(name,code_integer,Ints.size()-1);
    330         varmap[name]=var;//将之前的存储信息覆盖 
    331     }    
    332 }
    333 int storage_com::gettype(string name){
    334     varinfo ret=varmap[name];
    335     return ret.type;    
    336 }/*下面这里我没看明白 ,先暂时不用了 */
    337 void storage_com::solve0(string s){//s是storage命令处理 
    338     int len=s.length();
    339     for(int i=0;i<len;++i){//枚举i,一直到结尾 
    340         if(!is_empty_char(s[i])){//空白[i]
    341             string tmp=s.substr(0,i);//不算空白[0,i-1]是否找到变量名
    342             varinfo tmpinfo=varmap[tmp]; 
    343             if(tmpinfo.type!=0){//如果这个变量存在 
    344                 //solve1(s,i+1,tmpinfo);//转交下层处理,[i+1,end]跳过空白 
    345                 return;//该命令运行完毕 
    346             }
    347         }
    348     }
    349     System.out.println("Bad type.");
    350 }
    351 void storage_com::init(){
    352     vector<Integer>().swap(Ints);
    353     vector<Floating>().swap(Floats);
    354     vector<String>().swap(Strings);
    355     varmap.clear();
    356 }
    357 int main(){
    358     System.wel.startsys();
    359     return 0;
    360 }
    0.1.3

    0.1.4版本更新:
    1.添加了"cls"指令 用于清屏
    2. storage 已经支持Integer/Floating/String全类型
    3. 现在命令首尾如果有空格会自动忽略(trim函数)
    4.支持通过varinfo 或者string name来输出变量的值的字符串

    5. 由于help指令未更新,所以发布0.1.4 patch1 来修复bug

      1 #include<iostream>
      2 #include<string>
      3 #include<cstdlib> 
      4 #include<vector>
      5 #include<windows.h>
      6 #include<map>
      7 #include<ctime>
      8 #include<cstdio>
      9 #include<sstream>
     10 using std::cin;
     11 using std::cout;
     12 using std::string;
     13 using std::vector;
     14 using std::endl;
     15 using std::map;
     16 bool is_empty_char(char c){
     17     return c==' '|| c=='
    ';
     18 }
     19 void trim(string &s){//删除字符串首尾空格 
     20     if(s.empty()) return;
     21     s.erase(0,s.find_first_not_of(" "));
     22     s.erase(s.find_last_not_of(" ") + 1);
     23 } 
     24 class out_com{//字符串输出
     25     public:
     26         void print(string s);//输出 
     27         void print(string s,int from);//从第from开始输出,一直到最后 
     28         void println(string s);//输出并换行 
     29         void println(string s,int from);//从from开始输出,并换行     
     30 };
     31 class err_com{//错误处理 
     32     public:
     33     void throwln(string s);//输出错误信息 
     34     void throwshut(string s,int timeout);//输出错误信息并且在timeout毫秒后关闭程序 
     35 };
     36 class fileio_com{//文件输入输出(还没写)
     37 };
     38 class in_com{//字符串输入 
     39     public:
     40         void read(string &s);//输入(空白作结尾符号) 
     41         void readln(string &s); //输入(换行作结尾符号) 
     42 };
     43 class systemio_com{ //依赖于Windows io的功能 
     44     public:                 
     45     void windowscmd(string s){//直接在cmd里面执行指令 
     46         system(s.c_str());
     47     }
     48     void progexit(int code){//退出程序并且错误码为code 
     49         exit(code);
     50     }
     51     void cleanscr();//清屏 
     52         void sleep(int k){//暂停k毫秒 
     53             Sleep(k);
     54     } 
     55 };
     56 class welcome_com{//系统欢迎与用户界面(并不是系统核心) 
     57     public:
     58         void sysinfo();//系统信息 
     59         void startsys();//启动和初始化 
     60         void startscr();//开启屏幕 
     61         void closescr();//关闭屏幕 
     62         void closesys();//关闭系统 
     63         void initsys();//数据结构初始化 
     64         void closetimeout(int m_secs);//延迟关闭(毫秒) 
     65         void sysmain();//系统运行的重要内容 
     66         void help();//输出帮助信息
     67         void advertisements();//输出广告 
     68 };
     69 class storage_com{//存储管理 
     70     public:
     71         static const int code_void=0,//空类型 
     72                         code_integer=1,//整数 
     73                         code_floating=2,//浮点数 
     74                         code_string=3;//字符串
     75         
     76         struct varinfo{//保存变量信息的数据结构 
     77             string name;
     78             int type;
     79             int address;
     80             varinfo(int a=0,int b=0):type(a),address(b){}
     81             varinfo(string a,int b,int c):name(a),type(b),address(c){}
     82         };
     83         typedef long long Integer;//Integer:long long
     84         typedef double Floating;//Floating: double
     85         typedef string String;//String: string
     86         vector<Integer> Ints;//保存Integer 的数组 
     87         vector<Floating> Floats;//保存Floating的数组 
     88         vector<String> Strings;//保存String的数组 
     89         map<string,varinfo> varmap;//string->varinfo的映射 
     90     public:
     91         storage(){}
     92         void init();
     93         int gettype(string name);//获得变量名name的类型 
     94         void putvalue(string name,long long val);//将变量名name的值设为val
     95         void putvalue(string name,string val);
     96         void putvalue(string name,double val);
     97         int getInteger(string name);//获得name的int值 
     98         String getString(string name);
     99         double getFloating(string name);
    100         void solve0(string command);
    101         string getvarstr(varinfo var);
    102         string getvarstr(string name);
    103         void printvarstr(string name);
    104         void printvarstrln(string name);
    105 };
    106 class comsolve_com{//指令处理 
    107     private:
    108         static const int 
    109             code_unknown=0,//错误指令 
    110             code_print=1,
    111             code_windowscmd=2,
    112             code_exit=3,
    113             code_sysinfo=4,
    114             code_help=5,
    115             code_var=6,
    116             code_cls=7;
    117         static const int 
    118             code_varflag0='%';//变量出现的标志 
    119         static const int Max_basic_command=100;//最大命令数目(现在只有8个) 
    120         string basic_command[Max_basic_command];//指令代号->指令名的映射 
    121         map<string,int> commap;//指令名->指令代号的映射 
    122     public:
    123         comsolve_com(){}
    124         void init();//初始化 
    125         void solve0(string s);//第一步处理
    126         void solve1(string s,int left,int code);//第二步命令处理 
    127         string varsolve(string s);//把字符串中的变量替换成所代表的的值
    128 };
    129 class System_com{//基本功能(几乎无需初始化) 
    130     public:
    131         out_com out;
    132         err_com err;
    133         in_com in;
    134         systemio_com sysio;
    135         fileio_com fileio;
    136         welcome_com wel;
    137     System_com(){}
    138 }System;
    139 class Systemext_com{//基本拓展功能(可能需要初始化) 
    140     public:
    141         storage_com var;
    142         comsolve_com comsolve;
    143     Systemext_com(){}
    144     init(){
    145         var.init();
    146         comsolve.init();
    147     }
    148 }Systemext;
    149 void out_com::print(string s){
    150     print(s,0);
    151 }
    152 void out_com::println(string s){
    153     print(s);cout<<endl;
    154 }
    155 void out_com::print(string s,int from){
    156     int len=s.length();
    157     string s1=s.substr(from,len-from);
    158     cout<<s1;
    159 }
    160 void out_com::println(string s,int from){
    161     print(s,from);cout<<endl;
    162 }
    163 void in_com::read(string &s){
    164     cin>>s;
    165 }
    166 void in_com::readln(string &s){
    167     getline(cin,s);
    168 }
    169 void err_com::throwln(string s){
    170     System.sysio.windowscmd("color 1f"); //color 1 3 9是蓝色 
    171     System.wel.sysinfo();
    172     System.out.println("System Error:There is a problem:");
    173     System.out.println(s);
    174 }
    175 void err_com::throwshut(string s,int timeout){
    176     System.err.throwln(s);
    177     System.wel.closetimeout(timeout);
    178 } 
    179 void welcome_com::sysinfo(){
    180     System.out.println("[Command Operatoring System]");
    181     System.out.println("[This system is designed by cdsidi.]");
    182     System.out.println("[Version:0.1.4 patch 1]");
    183     /************更新日志*************************** 
    184     0.0.1:    基本的框架和命令基础 
    185     0.0.2: 优化了GUI,添加了启动和关闭画面,为fileio做下早期准备
    186     0.1.0: 在底层支持了Integer类型 
    187     0.1.1: 优化了用户交互、完善了代码注释 
    188     0.1.2: 添加了广告
    189     0.1.3:重构代码,将System里面的类 拆分开来,为多文件编程打下基础 
    190     0.1.4:    1.添加了"cls"指令 用于清屏 
    191             2. storage 已经支持Integer/Floating/String全类型 
    192             3. 现在命令首尾如果有空格会自动忽略(trim函数) 
    193             4.支持通过varinfo string 来输出变量的内容 
    194     0.1.4 patch 1: 1.修复了bug;添加了 cls 指令在help内 
    195     ***********************************************/ 
    196 }
    197 void welcome_com::advertisements(){
    198     System.out.println("****************以下是广告*************");
    199     System.out.println("_______广告 1_________ ");    
    200     System.out.println("如果喜欢ComSys,请捐助我 ");    
    201     System.out.println("如果想投放广告,请联系我 ");
    202     System.out.println("https://www.cnblogs.com/cdsidi");
    203     System.out.println("_______广告 2_________ ");    
    204     System.out.println("现在加入 51C SB 派对!");
    205     System.out.println("****************广告已结束*************");
    206 }
    207 //这里有bug 先不用了 
    208 /* 
    209 string comsolve_com::varsolve(string s){
    210     string ret;
    211     int len=s.length(),
    212         lastplace=-1;//上一个没有配对的varflag位置 
    213     for(int i=0;i<len;++i){
    214     //    printf("lastplace:%d i:%d len:%d 
    ",lastplace,i,len);
    215         if(s.at(i)==code_varflag0){
    216             if(lastplace!=-1) {
    217                 lastplace=i;
    218             }else{
    219                 string varname=s.substr(lastplace+1,i-(lastplace+1));//恰好删除varflag中间的部分 
    220                 if(!varname.empty()){
    221                     string varstr=Systemext.var.getvarstr(varname);
    222                     ret.erase(lastplace,i-lastplace);//删除ret从lastplace开始,一直到i-1的部分 
    223                     continue;
    224                 }
    225                 lastplace=-1;
    226             }
    227         }
    228             ret.push_back(s.at(i));
    229     }
    230     return ret;
    231 }*/
    232 void comsolve_com::solve1(string s,int left,int code){
    233     int len=s.length(),tmplen=len-left;
    234     string tmp=s.substr(left,len-left);//从left一直截取到len(下一步处理的tmp) 
    235     switch(code){
    236         case code_unknown:
    237             System.out.println("Bad command.
    Please type 'help' for help of commands.");break;
    238         case code_print:
    239             System.out.println(tmp);break;
    240         case code_windowscmd:
    241             System.sysio.windowscmd(tmp);break;
    242         case code_exit:
    243             System.wel.closesys();break;
    244         case code_sysinfo:
    245             System.wel.sysinfo();break;
    246         case code_help:
    247             System.wel.help();break;
    248         case code_var:
    249             Systemext.var.solve0(tmp);break;
    250         case code_cls:
    251             System.sysio.cleanscr();break;
    252         default:
    253             printf("Bad command.
    ");
    254     }
    255 }
    256 void comsolve_com::solve0(string s){
    257     int len=s.length();
    258     for(int i=0;i<len;++i){//枚举第一级命令的结尾
    259         if(is_empty_char(s[i])){//如果[i]是空格 
    260             string tmp=s.substr(0,i);//不算空格[0,i-1]是否找到一个指令 
    261             if(commap[tmp]!=0){
    262                 solve1(s,i+1,commap[tmp]);//转交下层处理,[i+1,end]跳过空格 
    263                 return;
    264             }
    265         }
    266     }
    267     //s没有遇到任何空格,传递整个字符串作为第一级命令,第二级命令是空串 
    268     solve1(s,len,commap[s]);
    269 }
    270 void comsolve_com::init(){
    271     basic_command[code_print]="print";
    272     basic_command[code_windowscmd]="windows";
    273     basic_command[code_exit]="exit";
    274     basic_command[code_sysinfo]="sysinfo";
    275     basic_command[code_help]="help";
    276     basic_command[code_var]="var";
    277     basic_command[code_cls]="cls";
    278     for(int i=0;i<Max_basic_command;++i){
    279         commap[basic_command[i]]=i;
    280     }
    281 }
    282 void systemio_com::cleanscr(){
    283     system("cls");
    284 }
    285 void welcome_com::startscr(){//启动屏幕 
    286     int cnt_stars=80,cnt_emptyl=10,waitms=3000;
    287     System.sysio.cleanscr();
    288     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    289     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    290     System.wel.sysinfo();
    291     System.wel.advertisements();
    292     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    293     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    294     System.out.print("
    ");
    295     System.out.println("Starting...");
    296     System.sysio.sleep(waitms);
    297 }
    298 void welcome_com::closescr(){
    299     System.sysio.cleanscr();
    300     int cnt_stars=80,cnt_emptyl=10,waitms=3000;
    301     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    302     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    303     System.wel.sysinfo();
    304     System.wel.advertisements();
    305     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    306     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    307     System.out.print("
    ");
    308     System.out.println("System is shutting down.");
    309     System.sysio.sleep(waitms);
    310     //System.sysio.cleanscr();
    311 }
    312 void welcome_com::help(){
    313     System.out.println("************Start of help*****************");
    314     System.out.println("1.    command 'print': ");
    315     System.out.println("print {string} System will say {string}.");
    316     System.out.println("eg. print Hello!");
    317     System.out.println("2.    command 'exit': ");
    318     System.out.println("System will close.");
    319     System.out.println("eg. exit");
    320     System.out.println("3.    command 'sysinfo': ");
    321     System.out.println("System will show the information of the System.");
    322     System.out.println("eg. sysinfo");
    323     System.out.println("4.    command 'help': ");
    324     System.out.println("System will show the commands of the System.");
    325     System.out.println("eg. help");
    326     System.out.println("5.    command 'windows': ");
    327     System.out.println("windows {string} system will do it in Windows cmd.exe");
    328     System.out.println("eg. windows echo hello");    
    329     System.out.println("6.    command 'cls': ");
    330     System.out.println("System will clean the screen.");
    331     System.out.println("eg. cls");    
    332     System.out.println("************End of help*****************");
    333 }
    334 void welcome_com::startsys(){
    335     System.sysio.windowscmd("color 0f");
    336     System.wel.startscr(); 
    337     System.wel.initsys();
    338     System.sysio.cleanscr();
    339     System.out.println("System has got ready.");
    340     System.wel.sysmain();
    341 } 
    342 void welcome_com::initsys(){
    343 
    344     Systemext.comsolve.init();
    345     Systemext.var.init();
    346     System.out.println("System has inited");
    347 }
    348 void welcome_com::closesys(){
    349     System.wel.closescr();
    350     System.out.println("Press any key to exit.");
    351     System.sysio.windowscmd("pause >nul");
    352     System.sysio.progexit(0);
    353 }
    354 void welcome_com::closetimeout(int m_secs){
    355     printf("System will shutdown in %d seconds. Please get ready.",m_secs/1000);
    356     Sleep(m_secs);
    357     System.wel.closesys();
    358 }
    359 void welcome_com::sysmain(){
    360     string s;
    361     while(1){
    362         System.out.print(">");
    363         System.in.readln(s);
    364         trim(s);//去掉首尾空格 
    365         if(!s.empty())
    366             Systemext.comsolve.solve0(s);
    367     }
    368 }
    369 int storage_com::getInteger(string name){
    370     varinfo var=varmap[name];
    371     if(var.type==code_integer){
    372         return Ints[var.address];
    373     }else{
    374         System.err.throwln("Wrong Type.");
    375         return 0;
    376     }
    377 }
    378 double storage_com::getFloating(string name){
    379     varinfo var=varmap[name];
    380     if(var.type==code_floating){
    381         return Floats[var.address];
    382     }else{
    383         System.err.throwln("Wrong Type.");
    384         return 0;
    385     }
    386 }
    387 string storage_com::getString(string name){
    388     varinfo var=varmap[name];
    389     if(var.type==code_string){
    390         return Strings[var.address];
    391     }else{
    392         System.err.throwln("Wrong Type.");
    393         return "";
    394     }
    395 }
    396 void storage_com::putvalue(string name,long long val){
    397     varinfo var=varmap[name];
    398     if(var.type==code_void){
    399         Ints.push_back(val);
    400         var=varinfo(name,code_integer,Ints.size()-1);
    401         varmap[name]=var;
    402     }else if(var.type==code_integer){//此变量存在 
    403         Ints[var.address]=val;
    404     }else{//注意,以下会出现在内存池中变量的内存泄露 
    405         Ints.push_back(val);
    406         var=varinfo(name,code_integer,Ints.size()-1);
    407         varmap[name]=var;
    408         //将之前的存储信息覆盖(之前的内存将永远无法用到) 
    409     }    
    410 }
    411 void storage_com::putvalue(string name,string val){
    412     varinfo var=varmap[name];
    413     if(var.type==code_void){
    414         Strings.push_back(val);
    415         var=varinfo(name,code_string,Strings.size()-1);
    416         varmap[name]=var;
    417     }else if(var.type==code_string){
    418         Strings[var.address]=val;
    419     }else{ 
    420         Strings.push_back(val);
    421         var=varinfo(name,code_string,Strings.size()-1);
    422         varmap[name]=var;
    423     }
    424 }
    425 void storage_com::putvalue(string name,double val){
    426     varinfo var=varmap[name];
    427     const int type=var.type;
    428     if(type==code_void){
    429         Floats.push_back(val);
    430         var=varinfo(name,code_floating,Floats.size()-1);
    431         varmap[name]=var;
    432     }else if(type==code_floating){
    433         Floats[var.address]=val;
    434     }else{ 
    435         Floats.push_back(val);
    436         var=varinfo(name,code_floating,Floats.size()-1);
    437         varmap[name]=var;
    438     }
    439 }
    440 int storage_com::gettype(string name){
    441     varinfo ret=varmap[name];
    442     return ret.type;    
    443 }
    444 string storage_com::getvarstr(varinfo var){
    445     const int type=var.type,address=var.address;
    446     string ret;
    447     std::stringstream stream;
    448     if(type==code_integer){
    449         stream<<Ints.at(address);
    450     }else if (type==code_string){
    451         stream<<Strings.at(address);
    452     }else if (type==code_floating){
    453         stream<<Floats.at(address);
    454     }else{
    455         return "VOID";
    456     }
    457     stream>>ret;
    458     return ret;
    459 }
    460 string storage_com::getvarstr(string name){
    461     varinfo var=varmap[name];
    462     return getvarstr(var);
    463 }
    464 void storage_com::printvarstr(string name){
    465     System.out.print(getvarstr(name));
    466 }
    467 void storage_com::printvarstrln(string name){
    468     System.out.println(getvarstr(name));
    469 }
    470 /*下面这里我没看明白 ,先暂时不用了 */
    471 void storage_com::solve0(string s){//s是storage命令处理 
    472     int len=s.length();
    473     for(int i=0;i<len;++i){//枚举i,一直到结尾 
    474         if(!is_empty_char(s[i])){//空白[i]
    475             string tmp=s.substr(0,i);//不算空白[0,i-1]是否找到变量名
    476             varinfo tmpinfo=varmap[tmp]; 
    477             if(tmpinfo.type!=0){//如果这个变量存在 
    478                 //solve1(s,i+1,tmpinfo);//转交下层处理,[i+1,end]跳过空白 
    479                 return;//该命令运行完毕 
    480             }
    481         }
    482     }
    483     System.out.println("Bad type.");
    484 }
    485 void storage_com::init(){
    486     vector<Integer>().swap(Ints);
    487     vector<Floating>().swap(Floats);
    488     vector<String>().swap(Strings);
    489     map<string,varinfo>().swap(varmap);
    490 }
    491 int main(){
    492     System.wel.startsys();
    493     return 0;
    494 }
    0.1.4 patch1

     0.1.5版本更新:

    1.添加了syscore_com 类 拆分了welcome_com的一些功能
    2.整合了开始屏幕和关闭屏幕欢迎信息
    3.添加了"welcome::about()"包含系统信息和广告
    4.更改了版本号系统[x.x.x.x] 最后一位是补丁号

      1 #include<iostream>
      2 #include<string>
      3 #include<cstdlib> 
      4 #include<vector>
      5 #include<windows.h>
      6 #include<map>
      7 #include<ctime>
      8 #include<cstdio>
      9 #include<sstream>
     10 using std::cin;
     11 using std::cout;
     12 using std::string;
     13 using std::vector;
     14 using std::endl;
     15 using std::map;
     16 bool is_empty_char(char c){
     17     return c==' '|| c=='
    ';
     18 }
     19 void trim(string &s){//删除字符串首尾空格 
     20     if(s.empty()) return;
     21     s.erase(0,s.find_first_not_of(" "));
     22     s.erase(s.find_last_not_of(" ") + 1);
     23 } 
     24 class out_com{//字符串输出
     25     public:
     26         void print(string s);//输出 
     27         void print(string s,int from);//从第from开始输出,一直到最后 
     28         void println(string s);//输出并换行 
     29         void println(string s,int from);//从from开始输出,并换行     
     30 };
     31 class err_com{//错误处理 
     32     public:
     33     void throwln(string s);//输出错误信息 
     34     void throwshut(string s,int timeout);//输出错误信息并且在timeout毫秒后关闭程序 
     35 };
     36 class fileio_com{//文件输入输出(还没写)
     37 };
     38 class in_com{//字符串输入 
     39     public:
     40         void read(string &s);//输入(空白作结尾符号) 
     41         void readln(string &s); //输入(换行作结尾符号) 
     42 };
     43 class systemio_com{ //依赖于Windows io的功能 
     44     public:                 
     45     void windowscmd(string s){//直接在cmd里面执行指令 
     46         system(s.c_str());
     47     }
     48     void progexit(int code){//退出程序并且错误码为code 
     49         exit(code);
     50     }
     51     void cleanscr();//清屏 
     52     void sleep(int k){//暂停k毫秒 
     53             Sleep(k);
     54     } 
     55 };
     56 class welcome_com{//系统欢迎与用户界面(并不是系统核心) 
     57     public:
     58         void sysinfo();//系统信息 
     59         void startsys();//启动和初始化 
     60         void welcomescr(string message);//开始和关闭屏幕
     61         void help();//输出帮助信息
     62         void advertisements();//输出广告 
     63         void about();//输出关于信息 
     64 };
     65 class storage_com{//存储管理 
     66     public:
     67         static const int code_void=0,//空类型 
     68                         code_integer=1,//整数 
     69                         code_floating=2,//浮点数 
     70                         code_string=3;//字符串
     71         
     72         struct varinfo{//保存变量信息的数据结构 
     73             string name;
     74             int type;
     75             int address;
     76             varinfo(int a=0,int b=0):type(a),address(b){}
     77             varinfo(string a,int b,int c):name(a),type(b),address(c){}
     78         };
     79         typedef long long Integer;//Integer:long long
     80         typedef double Floating;//Floating: double
     81         typedef string String;//String: string
     82         vector<Integer> Ints;//保存Integer 的数组 
     83         vector<Floating> Floats;//保存Floating的数组 
     84         vector<String> Strings;//保存String的数组 
     85         map<string,varinfo> varmap;//string->varinfo的映射 
     86     public:
     87         storage(){}
     88         void init();
     89         int gettype(string name);//获得变量名name的类型 
     90         void putvalue(string name,long long val);//将变量名name的值设为val
     91         void putvalue(string name,string val);
     92         void putvalue(string name,double val);
     93         int getInteger(string name);//获得name的int值 
     94         String getString(string name);
     95         double getFloating(string name);
     96         void solve0(string command);
     97         string getvarstr(varinfo var);
     98         string getvarstr(string name);
     99         void printvarstr(string name);
    100         void printvarstrln(string name);
    101 };
    102 class syscore_com{//系统核心 
    103     public: 
    104         void closesys();//关闭系统 
    105         void initsys();//数据结构初始化 
    106         void closetimeout(int m_secs);//延迟关闭(毫秒) 
    107         void sysmain();//系统运行的重要内容 
    108 };
    109 class comsolve_com{//指令处理 
    110     private:
    111         static const int 
    112             code_unknown=0,//错误指令 
    113             code_print=1,
    114             code_windowscmd=2,
    115             code_exit=3,
    116             code_sysinfo=4,
    117             code_help=5,
    118             code_var=6,
    119             code_cls=7;
    120         static const int 
    121             code_varflag0='%';//变量出现的标志 
    122         static const int Max_basic_command=100;//最大命令数目(现在只有8个) 
    123         string basic_command[Max_basic_command];//指令代号->指令名的映射 
    124         map<string,int> commap;//指令名->指令代号的映射 
    125     public:
    126         comsolve_com(){}
    127         void init();//初始化 
    128         void solve0(string s);//第一步处理
    129         void solve1(string s,int left,int code);//第二步命令处理 
    130         string varsolve(string s);//把字符串中的变量替换成所代表的的值
    131 };
    132 class System_com{//基本功能(几乎无需初始化) 
    133     public:
    134         out_com out;
    135         err_com err;
    136         in_com in;
    137         systemio_com sysio;
    138         fileio_com fileio;
    139         welcome_com wel;
    140         syscore_com syscore;
    141     System_com(){}
    142 }System;
    143 class Systemext_com{//基本拓展功能(可能需要初始化) 
    144     public:
    145         storage_com var;
    146         comsolve_com comsolve;
    147     Systemext_com(){}
    148     init(){
    149         var.init();
    150         comsolve.init();
    151     }
    152 }Systemext;
    153 void out_com::print(string s){
    154     print(s,0);
    155 }
    156 void out_com::println(string s){
    157     print(s);cout<<endl;
    158 }
    159 void out_com::print(string s,int from){
    160     int len=s.length();
    161     string s1=s.substr(from,len-from);
    162     cout<<s1;
    163 }
    164 void out_com::println(string s,int from){
    165     print(s,from);cout<<endl;
    166 }
    167 void in_com::read(string &s){
    168     cin>>s;
    169 }
    170 void in_com::readln(string &s){
    171     getline(cin,s);
    172 }
    173 void err_com::throwln(string s){
    174     System.sysio.windowscmd("color 1f"); //color 1 3 9是蓝色 
    175     System.wel.sysinfo();
    176     System.out.println("System Error:There is a problem:");
    177     System.out.println(s);
    178 }
    179 void err_com::throwshut(string s,int timeout){
    180     System.err.throwln(s);
    181     System.syscore.closetimeout(timeout);
    182 } 
    183 void welcome_com::sysinfo(){
    184     int num1=0,num2=1,num3=5,patch=0;
    185     std::stringstream versionstream;
    186     versionstream<<"[Version:"<<num1<<"."<<num2<<"."<<num3<<"."<<patch<<"]";
    187     string versionstring;
    188     versionstream>>versionstring;
    189     System.out.println("[Command Operatoring System]");
    190     System.out.println("[This system is designed by cdsidi.]");
    191     System.out.println(versionstring);
    192     /************更新日志*************************** 
    193     0.0.1:    基本的框架和命令基础 
    194     0.0.2: 优化了GUI,添加了启动和关闭画面,为fileio做下早期准备
    195     0.1.0: 在底层支持了Integer类型 
    196     0.1.1: 优化了用户交互、完善了代码注释 
    197     0.1.2: 添加了广告
    198     0.1.3:重构代码,将System里面的类 拆分开来,为多文件编程打下基础 
    199     0.1.4:    1.添加了"cls"指令 用于清屏 
    200             2. storage 已经支持Integer/Floating/String全类型 
    201             3. 现在命令首尾如果有空格会自动忽略(trim函数) 
    202             4.支持通过varinfo string 来输出变量的内容 
    203     0.1.4 patch 1: 1.修复了bug;添加了 cls 指令在help内 
    204     0.1.5:    1.添加了syscore_com 类 拆分了welcome_com的一些功能
    205              2.整合了开始屏幕和关闭屏幕欢迎信息
    206             3.添加了"welcome::about()"包含系统信息和广告 
    207             4.更改了版本号系统[x.x.x.x] 最后一位是补丁号 
    208     ***********************************************/ 
    209 }
    210 void welcome_com::advertisements(){
    211     System.out.println("****************以下是广告*************");
    212     System.out.println("_______广告 1_________ ");    
    213     System.out.println("如果喜欢ComSys,请捐助我 ");    
    214     System.out.println("如果想投放广告,请联系我 ");
    215     System.out.println("https://www.cnblogs.com/cdsidi");
    216     System.out.println("_______广告 2_________ ");    
    217     System.out.println("现在加入 51C SB 派对!");
    218     System.out.println("****************广告已结束*************");
    219 }
    220 void welcome_com::about(){
    221     sysinfo();
    222     advertisements();
    223 }
    224 //这里有bug 先不用了 
    225 /* 
    226 string comsolve_com::varsolve(string s){
    227     string ret;
    228     int len=s.length(),
    229         lastplace=-1;//上一个没有配对的varflag位置 
    230     for(int i=0;i<len;++i){
    231     //    printf("lastplace:%d i:%d len:%d 
    ",lastplace,i,len);
    232         if(s.at(i)==code_varflag0){
    233             if(lastplace!=-1) {
    234                 lastplace=i;
    235             }else{
    236                 string varname=s.substr(lastplace+1,i-(lastplace+1));//恰好删除varflag中间的部分 
    237                 if(!varname.empty()){
    238                     string varstr=Systemext.var.getvarstr(varname);
    239                     ret.erase(lastplace,i-lastplace);//删除ret从lastplace开始,一直到i-1的部分 
    240                     continue;
    241                 }
    242                 lastplace=-1;
    243             }
    244         }
    245             ret.push_back(s.at(i));
    246     }
    247     return ret;
    248 }*/
    249 void comsolve_com::solve1(string s,int left,int code){
    250     int len=s.length(),tmplen=len-left;
    251     string tmp=s.substr(left,len-left);//从left一直截取到len(下一步处理的tmp) 
    252     switch(code){
    253         case code_unknown:
    254             System.out.println("Bad command.
    Please type 'help' for help of commands.");break;
    255         case code_print:
    256             System.out.println(tmp);break;
    257         case code_windowscmd:
    258             System.sysio.windowscmd(tmp);break;
    259         case code_exit:
    260             System.syscore.closesys();break;
    261         case code_sysinfo:
    262             System.wel.sysinfo();break;
    263         case code_help:
    264             System.wel.help();break;
    265         case code_var:
    266             Systemext.var.solve0(tmp);break;
    267         case code_cls:
    268             System.sysio.cleanscr();break;
    269         default:
    270             printf("Bad command.
    ");
    271     }
    272 }
    273 void comsolve_com::solve0(string s){
    274     int len=s.length();
    275     for(int i=0;i<len;++i){//枚举第一级命令的结尾
    276         if(is_empty_char(s[i])){//如果[i]是空格 
    277             string tmp=s.substr(0,i);//不算空格[0,i-1]是否找到一个指令 
    278             if(commap[tmp]!=0){
    279                 solve1(s,i+1,commap[tmp]);//转交下层处理,[i+1,end]跳过空格 
    280                 return;
    281             }
    282         }
    283     }
    284     //s没有遇到任何空格,传递整个字符串作为第一级命令,第二级命令是空串 
    285     solve1(s,len,commap[s]);
    286 }
    287 void comsolve_com::init(){
    288     basic_command[code_print]="print";
    289     basic_command[code_windowscmd]="windows";
    290     basic_command[code_exit]="exit";
    291     basic_command[code_sysinfo]="sysinfo";
    292     basic_command[code_help]="help";
    293     basic_command[code_var]="var";
    294     basic_command[code_cls]="cls";
    295     for(int i=0;i<Max_basic_command;++i){
    296         commap[basic_command[i]]=i;
    297     }
    298 }
    299 void systemio_com::cleanscr(){
    300     system("cls");
    301 }
    302 void welcome_com::welcomescr(string message){//启动和关闭屏幕 (通过message传递) 
    303     const int cnt_stars=80,cnt_emptyl=8,waitms=3000;
    304     System.sysio.cleanscr();
    305     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    306     for(int i=1;i<=cnt_emptyl/2;++i)System.out.print("
    ");
    307     System.wel.about();
    308     System.out.println(message);
    309     for(int i=1;i<=cnt_emptyl-cnt_emptyl/2;++i)System.out.print("
    ");
    310     for(int i=1;i<=cnt_stars;++i)System.out.print("*");
    311     System.sysio.sleep(waitms);
    312 }
    313 void welcome_com::help(){
    314     System.out.println("************Start of help*****************");
    315     System.out.println("1.    command 'print': ");
    316     System.out.println("print {string} System will say {string}.");
    317     System.out.println("eg. print Hello!");
    318     System.out.println("2.    command 'exit': ");
    319     System.out.println("System will close.");
    320     System.out.println("eg. exit");
    321     System.out.println("3.    command 'sysinfo': ");
    322     System.out.println("System will show the information of the System.");
    323     System.out.println("eg. sysinfo");
    324     System.out.println("4.    command 'help': ");
    325     System.out.println("System will show the commands of the System.");
    326     System.out.println("eg. help");
    327     System.out.println("5.    command 'windows': ");
    328     System.out.println("windows {string} system will do it in Windows cmd.exe");
    329     System.out.println("eg. windows echo hello");    
    330     System.out.println("6.    command 'cls': ");
    331     System.out.println("System will clean the screen.");
    332     System.out.println("eg. cls");    
    333     System.out.println("************End of help*****************");
    334 }
    335 void welcome_com::startsys(){
    336     System.sysio.windowscmd("color 0f");
    337     System.wel.welcomescr("Starting...");
    338     System.syscore.initsys();
    339     System.sysio.cleanscr();
    340     System.out.println("System has got ready.");
    341     System.syscore.sysmain();
    342 } 
    343 void syscore_com::initsys(){
    344     Systemext.init();
    345 }
    346 void syscore_com::closesys(){
    347     System.sysio.cleanscr();
    348     System.wel.welcomescr("System is shutting down...");
    349     System.out.println("Press any key to exit.");
    350     System.sysio.windowscmd("pause >nul");
    351     System.sysio.progexit(0);
    352 }
    353 void syscore_com::closetimeout(int m_secs){
    354     printf("System will shutdown in %d seconds. Please get ready.",m_secs/1000);
    355     Sleep(m_secs);
    356     System.syscore.closesys();
    357 }
    358 void syscore_com::sysmain(){
    359     string s;
    360     while(1){
    361         System.out.print(">");
    362         System.in.readln(s);
    363         trim(s);//去掉首尾空格 
    364         if(!s.empty())
    365             Systemext.comsolve.solve0(s);
    366     }
    367 }
    368 int storage_com::getInteger(string name){
    369     varinfo var=varmap[name];
    370     if(var.type==code_integer){
    371         return Ints[var.address];
    372     }else{
    373         System.err.throwln("Wrong Type.");
    374         return 0;
    375     }
    376 }
    377 double storage_com::getFloating(string name){
    378     varinfo var=varmap[name];
    379     if(var.type==code_floating){
    380         return Floats[var.address];
    381     }else{
    382         System.err.throwln("Wrong Type.");
    383         return 0;
    384     }
    385 }
    386 string storage_com::getString(string name){
    387     varinfo var=varmap[name];
    388     if(var.type==code_string){
    389         return Strings[var.address];
    390     }else{
    391         System.err.throwln("Wrong Type.");
    392         return "";
    393     }
    394 }
    395 void storage_com::putvalue(string name,long long val){
    396     varinfo var=varmap[name];
    397     if(var.type==code_void){
    398         Ints.push_back(val);
    399         var=varinfo(name,code_integer,Ints.size()-1);
    400         varmap[name]=var;
    401     }else if(var.type==code_integer){//此变量存在 
    402         Ints[var.address]=val;
    403     }else{//注意,以下会出现在内存池中变量的内存泄露 
    404         Ints.push_back(val);
    405         var=varinfo(name,code_integer,Ints.size()-1);
    406         varmap[name]=var;
    407         //将之前的存储信息覆盖(之前的内存将永远无法用到) 
    408     }    
    409 }
    410 void storage_com::putvalue(string name,string val){
    411     varinfo var=varmap[name];
    412     if(var.type==code_void){
    413         Strings.push_back(val);
    414         var=varinfo(name,code_string,Strings.size()-1);
    415         varmap[name]=var;
    416     }else if(var.type==code_string){
    417         Strings[var.address]=val;
    418     }else{ 
    419         Strings.push_back(val);
    420         var=varinfo(name,code_string,Strings.size()-1);
    421         varmap[name]=var;
    422     }
    423 }
    424 void storage_com::putvalue(string name,double val){
    425     varinfo var=varmap[name];
    426     const int type=var.type;
    427     if(type==code_void){
    428         Floats.push_back(val);
    429         var=varinfo(name,code_floating,Floats.size()-1);
    430         varmap[name]=var;
    431     }else if(type==code_floating){
    432         Floats[var.address]=val;
    433     }else{ 
    434         Floats.push_back(val);
    435         var=varinfo(name,code_floating,Floats.size()-1);
    436         varmap[name]=var;
    437     }
    438 }
    439 int storage_com::gettype(string name){
    440     varinfo ret=varmap[name];
    441     return ret.type;    
    442 }
    443 string storage_com::getvarstr(varinfo var){
    444     const int type=var.type,address=var.address;
    445     string ret;
    446     std::stringstream stream;
    447     if(type==code_integer){
    448         stream<<Ints.at(address);
    449     }else if (type==code_string){
    450         stream<<Strings.at(address);
    451     }else if (type==code_floating){
    452         stream<<Floats.at(address);
    453     }else{
    454         return "VOID";
    455     }
    456     stream>>ret;
    457     return ret;
    458 }
    459 string storage_com::getvarstr(string name){
    460     varinfo var=varmap[name];
    461     return getvarstr(var);
    462 }
    463 void storage_com::printvarstr(string name){
    464     System.out.print(getvarstr(name));
    465 }
    466 void storage_com::printvarstrln(string name){
    467     System.out.println(getvarstr(name));
    468 }
    469 /*下面这里我没看明白 ,先暂时不用了 */
    470 void storage_com::solve0(string s){//s是storage命令处理 
    471     int len=s.length();
    472     for(int i=0;i<len;++i){//枚举i,一直到结尾 
    473         if(!is_empty_char(s[i])){//空白[i]
    474             string tmp=s.substr(0,i);//不算空白[0,i-1]是否找到变量名
    475             varinfo tmpinfo=varmap[tmp]; 
    476             if(tmpinfo.type!=0){//如果这个变量存在 
    477                 //solve1(s,i+1,tmpinfo);//转交下层处理,[i+1,end]跳过空白 
    478                 return;//该命令运行完毕 
    479             }
    480         }
    481     }
    482     System.out.println("Bad type.");
    483 }
    484 void storage_com::init(){
    485     vector<Integer>().swap(Ints);
    486     vector<Floating>().swap(Floats);
    487     vector<String>().swap(Strings);
    488     map<string,varinfo>().swap(varmap);
    489 }
    490 int main(){
    491     System.wel.startsys();
    492     return 0;
    493 }
    0.1.5.0
  • 相关阅读:
    MDX Step by Step 读书笔记(六) Building Complex Sets (复杂集合的处理) Filtering Sets
    在 Visual Studio 2012 开发 SSIS,SSAS,SSRS BI 项目
    微软BI 之SSIS 系列 在 SSIS 中读取 SharePoint List
    MDX Step by Step 读书笔记(五) Working with Expressions (MDX 表达式) Infinite Recursion 和 SOLVE_ORDER 原理解析
    MDX Step by Step 读书笔记(五) Working with Expressions (MDX 表达式)
    使用 SQL Server 2012 Analysis Services Tabular Mode 表格建模 图文教程
    MDX Step by Step 读书笔记(四) Working with Sets (使用集合) Limiting Set and AutoExists
    SQL Server 2012 Analysis Services Tabular Model 读书笔记
    Microsoft SQL Server 2008 MDX Step by Step 学习笔记连载目录
    2011新的开始,介绍一下AgileEAS.NET平台在新的一年中的发展方向
  • 原文地址:https://www.cnblogs.com/cdsidi/p/12539718.html
Copyright © 2011-2022 走看看