ccf 命令行选项

CCF 命令行选项

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
struct state{
    char c;
    int tag;//0:不带参数,1:带参数
};
struct result1{
    int i;
    char * s;
};
int main()
{
    struct state cs[52];
    char c;
    scanf("%c",&c);
    int len=0;
    while(c!='\n'){
        if(c!=':'){cs[len].c=c;cs[len++].tag=0;}
        else {cs[len-1].tag=1;}
        scanf("%c",&c);
    }
    int n;
    scanf("%d",&n);getchar();
    char od[n][256];
    for(int i=0;i<n;i++)
        gets(od[i]);
    /**对cs排序*/
    for(int i=1;i<len;i++)
        for(int j=0;j<len-i;j++){
            if(cs[j].c>cs[j+1].c){
                int t1,t2;
                t1=cs[j].c;t2=cs[j].tag;
                cs[j].c=cs[j+1].c;cs[j].tag=cs[j+1].tag;
                cs[j+1].c=t1;cs[j+1].tag=t2;
            }
        }
    struct result1 r[len];
    for(int i=0;i<n;i++){
        for(int i=0;i<len;i++) r[i].i=0;
        int k,j=0,flag=0;char c1;
        while(od[i][j]!='\0'&&flag==0){
            while(od[i][j]!='-'&&od[i][j]!='\0') j++;
            if(od[i][j]=='-'){
                c1=od[i][++j];
                for(k=0;k<len;k++){
                    if(c1==cs[k].c){
                        if(cs[k].tag==1){/**该选项带参数*/
                            while(od[i][++j]==' ');
                            if(od[i][j]=='-'||od[i][j]=='\0'){flag=1;}
                            else {r[k].i=1;r[k].s=&od[i][j];while(od[i][++j]!=' ');od[i][j++]='\0';}
                        }
                        else{/**该选项不带参数*/
                            while(od[i][++j]==' ');
                            if(od[i][j]!='-'&&od[i][j]!='\0'){flag=1;}
                            else r[k].i=1;
                        }
                        break;
                    }
                }
                if(k==len)/**没有该选项*/
                    flag=1;
            }
        }
        /**输出*/
        printf("Case %d: ",i);
        for(int i=0;i<len;i++){
            if(r[i].i==1){
                if(cs[i].tag==0)
                    printf("-%c ",cs[i].c);
                else
                    printf("-%c %s ",cs[i].c,r[i].s);
            }
        }
        printf("\n");
    }
    return 0;
}
以下是使用 C++ 解决 CCF - CSP 201403 - 3 命令行选项问题的代码: ```cpp #include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string ask; cin >> ask; map<char, int> mp; for (int i = 0; i < ask.size(); i++) { if (i + 1 < ask.size() && ask[i + 1] == ':') { mp[ask[i]] = 2; i++; } else { mp[ask[i]] = 1; } } int n; cin >> n; cin.ignore(); string s, t; for (int i = 1; i <= n; i++) { getline(cin, s); stringstream ss(s); cout << "Case " << i << ": "; vector<string> v; map<char, string> res; while (ss >> t) { v.push_back(t); } for (int j = 1; j < v.size(); j++) { string tmp = v[j]; if (tmp.size() == 2 && tmp[0] == '-' && islower(tmp[1])) { char ch = tmp[1]; if (mp[ch] == 1) { res[ch] = "**"; } else if (mp[ch] == 2 && j + 1 < v.size()) { res[ch] = v[j + 1]; j++; } else { break; } } else { break; } } for (auto x : res) { cout << "-" << x.first << " "; if (x.second != "**") { cout << x.second << " "; } } cout << endl; } return 0; } ``` 这段代码的主要逻辑如下: 1. 读取第一行的格式字符串,确定每个选项是带参数还是不带参数,存储在 `map<char, int> mp` 中。 2. 读取测试用例的数量 `n`。 3. 对于每个测试用例,读取命令行输入,使用 `stringstream` 分割输入的字符串。 4. 遍历分割后的字符串,检查每个选项是否合法,并根据选项类型(带参数或不带参数)处理。 5. 将合法的选项及其参数存储在 `map<char, string> res` 中。 6. 按照升序输出合法的选项及其参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值