140303 command line options ccf

reference

https://blog.csdn.net/wjh2622075127/article/details/81639534

Thinking

Container for storing a valid command parameter map table and a valid command input

Effective container for storing the input command set (automatic sorting)

Plus a valid command table storage '-', to avoid the trouble of retrieving a valid command

achieve

. 1 #include <bits / STDC ++ H.>
 2  
. 3  
. 4  the using  namespace STD;
 . 5  
. 6 Map < String , int > Table; // valid command table 
. 7 Vector < String > Order; // input command 
. 8 Map < String , String > CAN; // parameters 
. 9  SET < String > ANS; // enter the valid command 
10  
. 11  int main () {
 12 is      String S;
 13 is      CIN >> S;
14     //有效命令表 
15     for(size_t i=0;i<s.size();i++){
16         string no;
17         stringstream t;
18         t<<'-'<<s[i];
19         t>>no;
20         if(i<s.size()-1&&s[i+1]==':'){//有参数 
21             table[no]=1;
22             i++;
23         }
24         else{//无参数 
25             table[no]=0;
26         }
27     }
28      
29     int n;
30     cin>>n;
31     cin.get();
32     
33     for(int i=1;i<=n;i++){
34         
35         order.clear();
36         can.clear();
37         ans.clear();
38         
39         getline(cin,s);
40         stringstream ss;
41         ss<<S;
 42 is          the while (SS >> S) {
 43 is              order.push_back (S);
 44 is          }
 45          // analyze the input command 
46 is          for (J = size_t . 1 ; J <order.size (); J ++ ) {
 47              // Command legality 
48              iF (table.count (Order [J]) == 0 ) {
 49                  BREAK ;
 50              }
 51 is              // no parameters 
52 is              iF (Table [Order [J]] == 0 ) {
 53 is                  ans.insert ( Order [J]);
 54 is              } 
 55             //带参 
56             else if(j<order.size()-1){
57                 ans.insert(order[j]);
58                 can[order[j]]=order[j+1];
59                 j++;
60             }
61             else{
62                 break;
63             } 
64         }
65         //输出
66         cout<<"Case "<<i<<":";
67         set<string>::iterator is=ans.begin();
68         while(is!=ans.end()){
69             if(can.count(*is)>0){
70                 cout<<' '<<*is<<' '<<can[*is];
71             }
72             else{
73                 cout<<' '<<*is;
74             }
75             is++;
76         }
77         cout<<endl;
78     }
79     
80     return 0;
81 }
View Code

topic

Problem Description
 
  You are to write a command-line analysis program to analyze a given command line options which include. Each command consists of several strings, separated by a space exactly between them. The first of these strings for command-line tool name, lowercase letters, your program it not be processed. After the tool name may contain several options, and may include some parameters are not options.
  There are two types of options: options and options with arguments without arguments. A legitimate form of non-parameter option is a minus sign followed by a single lowercase letters, such as "-a" or "-b". The band parameter options by two character strings separated by spaces, the former requires the same format with no parameter options, the latter is the option parameter is non-empty string lowercase letters, numbers, and minus composition .
  The author command-line tools available to you to specify a format string his command-line tool which options need to accept. This string consists of several lower case letters and a colon, where each of the small letters indicate a program options accepted. If the back of the lowercase letter followed by a colon, it indicates a choice of parameters, the option was otherwise no arguments. For example, "ab: m:" indicates that the program accepts three options, i.e. "-a" (without parameters), "- b" (with parameters), and "-m" (parameters).
  On the command-line tool to prepare a number of command line to test your program. For each command line, you should always analyze the tools back. When you encounter a string of tools is neither legitimate option, but not a legitimate option parameters, the analysis stops. Command line options for the remaining unanalyzed part of the order does not constitute, so your program should ignore them.
 
Input Format
 
  The first line of the input format is a string that contains at least one character, and a length of not more than 52. The format string contains only lowercase letters and colon, to ensure that each lowercase letters at most once, no two adjacent colons appear, it will not begin with a colon.
  A second input line is a positive integer N (1 ≤ N ≤ 20) , you need to process the command indicates the number of rows.
  Then there are N rows, each row is a command line to be processed, which comprises no more than 256 characters. This command must be separated by a number of single space character strings, each string contains only lowercase letters, numbers, and minus.
 
Output Format
 
  There are N output lines. Wherein the i-th row in "Case i:" Start, then there should be exactly one space, then the output should be the names of all of the command line options used in ascending alphabetical order according to, for options with arguments, after the output is also its name to its output parameters. If there was an option on the command line several times, output only once. If the option with arguments appeared several times on the command line, output only the last parameter carried occur.
 
Sample input
 
albw:x
4
ls -a -l -a documents -b
ls
ls -w 10 -x -w 15
ls -a -b -c -d -e -l
 
Sample Output
 
Case 1: -a -l
Case 2:
Case 3: -w 15 -x
Case 4: -a -b

Guess you like

Origin www.cnblogs.com/Gru-blog/p/11348659.html