zoukankan      html  css  js  c++  java
  • 201403-3 命令行选项

    思路

    字符串处理

    实现

    
    
    #include <iostream>
    #include <string>
    #include <map>
    #include <cstdio>
    #include <cctype>
    
    using namespace std;
    
    typedef pair<string, string> PAIR;
    
    bool no_arg_options[30];
    bool with_arg_options[30];
    
    struct opts {
    	map<string, string> opts_map;
    	
    	void insert(string opt, string arg) {
    		if (opts_map.find(opt) == opts_map.end()) {
    			opts_map.insert(std::pair<string, string>(opt,arg));
    		} else {
    			opts_map[opt] = arg;
    		}
    	}
    	
    	void out() {
    		string res;
    		for (map<string, string>::iterator ite = opts_map.begin();
    			 ite != opts_map.end();
    			 ++ite) {
    			if(ite->second == "") {
    				res = res + "-" + ite->first + " ";
    			} else {
    				res = res + "-" + ite->first + " " + ite->second + " ";
    			}
    		}
    		cout << res;
    	}
    
    };
    
    int main() {
    	
    	string consensus_ops;
    	getline(cin,consensus_ops);
    	
    	for (int i = 0;consensus_ops[i];++i) {
    		char &c = consensus_ops[i];
    		if (c==':') {
    			with_arg_options[consensus_ops[i-1] - 'a'] = true;
    			no_arg_options[consensus_ops[i-1] - 'a'] = false;
    		} else if (c>='a' && c<='z'){
    			no_arg_options[c - 'a'] = true;
    		}
    	}
    
    	int num;
    	scanf("%d
    ",&num);
    	
    	for (int i = 0;i < num;++i) {
    
    		string command;
    		opts opts_temp;
    
    		getline(cin, command);
    		string::iterator cmd_ite = command.begin();
    
    		/*cmd name*/
    		while(cmd_ite!=command.end() && !isspace(*cmd_ite)) {
    			++cmd_ite;
    		}
    		/*skip space*/
    		++cmd_ite;
    		while(cmd_ite!=command.end()) {
    			/* begin with - */
    			if (*cmd_ite++ == '-') {
    				char option_ch = *cmd_ite;
    				++cmd_ite;
    				
    				// illegal option
    				if (cmd_ite == command.end() 
    					|| !isspace(*cmd_ite) 
    					|| option_ch < 'a' 
    					|| option_ch > 'z') {
    					break;
    				}
    
    				if (no_arg_options[option_ch - 'a']) {
    					string opt(1,option_ch);
    					opts_temp.insert(opt,"");
    					++cmd_ite;
    				} else if (with_arg_options[option_ch - 'a']) {
    					string opt(1,option_ch);
    					++cmd_ite;
    
    					// legal arg
    					string arg;
    					arg.clear();
    					while (cmd_ite!=command.end() && !isspace(*cmd_ite)) {
    						if ((*cmd_ite>='a'&&*cmd_ite<='z')
    							||(*cmd_ite>='0'&&*cmd_ite<='9')
    							||(*cmd_ite=='-')) {
    							arg = arg + string(1,*cmd_ite);	
    						} else {
    							goto end; 
    						}
    						++cmd_ite;
    					}
    					if (arg.empty()) {
    						goto end;
    					}
    					opts_temp.insert(opt,arg);
    					
    					if(!isspace(*cmd_ite)) {
    						break;
    					}
    					++cmd_ite;
    				} else {
    					break;
    				}
    			} else { 
    				break;
    			}
    		}
    		end:;
    
    		cout << "Case " << i + 1 << ": ";
    		opts_temp.out();
    		printf("
    "); 	
    	}
    	
    }
    
    /*
    
    		while (cmd_ite!=command.end() && *cmd_ite != ' ' && *cmd_ite != '	') {
    			++cmd_ite;
    		}
    		++cmd_ite;
    	
    		while (cmd_ite !=command.end() ) {
    			if (*cmd_ite++ == '-') {
    				if (no_arg_options[*cmd_ite-'a']) {
    					if (cmd_ite!=command.end() && *(cmd_ite + 1) != ' ' && *(cmd_ite + 1) != '	') {
    						break;
    					}
    					string opt(1,*cmd_ite);
    					opts_temp.insert(opt,"");
    				} else if (with_arg_options[*cmd_ite-'a']) {
    					string opt(1,*cmd_ite);
    					if (cmd_ite!=command.end() && *(cmd_ite + 1) != ' ' && *(cmd_ite + 1) != '	') {
    						break;
    					}
    					++cmd_ite;
    					while (cmd_ite!=command.end() && (*cmd_ite == ' ' || *cmd_ite == '	')) {
    						++cmd_ite;
    					};
    					string arg;
    					while (cmd_ite!=command.end() && *cmd_ite != ' ' && *cmd_ite != '	') {
    						if ((*cmd_ite>='a'&&*cmd_ite<='z')
    							||(*cmd_ite>='0'&&*cmd_ite<='9')
    							||(*cmd_ite=='-')) {
    							arg = arg + string(1,*cmd_ite);	
    						} else {
    							goto end; 
    						}
    						++cmd_ite;
    					}
    					if (arg.empty()) {
    						goto end;
    					}
    					opts_temp.insert(opt,arg);
    				} else {
    					break;
    				}
    			} else {
    				break;
    			}
    			++cmd_ite;
    			while (cmd_ite!=command.end() && (*cmd_ite == ' '||*cmd_ite == '	') ) {
    				++cmd_ite;
    			}
    			if (cmd_ite==command.end()) {
    				break;
    			}
    		}
    		end:;
    		
    
    */
    
    
    
  • 相关阅读:
    二叉排序树
    #define使用方法
    typedef函数指针使用方法
    ORACLE触发器具体解释
    C++第11周(春)项目2
    建立人际信任的方法
    Error creating bean with name &#39;menuController&#39;: Injection of autowired dependency……
    strtok和strtok_r
    session销毁
    嵌入式相关5
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13543852.html
Copyright © 2011-2022 走看看