CCF 命令行窗口

出处

参考自码迷


摘自码迷的代码

#include <iostream>
#include <vector>
#include <map>
#include <math.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include <cstdio>
#include <string.h>
#include <stdio.h>
using namespace std;

int main() {
    //freopen("in.txt","r",stdin);
    string s;
    cin>>s;
    char buffer[260];
    int n;
    cin>>n;
    cin.getline(buffer,260);     //读取无用字符到这行末
    for(int i=1;i<=n;i++){
        cin.getline(buffer,260);
        string a(buffer);     //把char数组转换为string
        vector<string> v;     //申明向量存放切割的string
        map<string,string> map1;     //声明map存放命令和参数,first为命令,second为参数,注意不要和map重名,小心使用不了迭代器
        for(int t = (int)a.find(" ");t != -1; t = (int)a.find(" ")){
             string temp = a.substr(0,t);
             v.push_back(temp);
             a = a.substr(t+1);
         }
         v.push_back(a);     //上面这部分切割了字符串并全部放入向量。
          for(int j=0;j<v.size();j++)     //对向量里面元素遍历
        {
             if(v[j].size()==2 && v[j][0] == '-')     //如果是命令
             {
                char c = v[j][1];     //找到命令
                int next = (int)s.find(c);
                if(next==-1)     //如果命令非法则跳出
                    break;
                if(map1.find(v[j]) == map1.end())     //如果命令不存在则添加,键值对第二个参数默认为空
                    map1.insert(pair<string,string>(v[j],""));
                if(next+1<s.size() && s[next+1]==':' && j+1<v.size()){
                     map<string,string>::iterator it = map1.find(v[j]);
                     it->second = v[j+1];
                     j++;
                 }     //上面是对第i+1个v元素检查,如果i个v元素需要参数则写入map键值对的second,此办法直接覆盖前一个参数
             } else if(j!=0)     //如果不是命令且不为第一个(程序名)则跳出
                 break;

         }
              //由于map本身就是按键的值升序排列,少去了自己排序麻烦,可直接迭代器输出
         cout<<"Case "<<i<<":";
         for(map<string,string>::iterator it = map1.begin();it != map1.end();it++)
         {
            cout<<" "<<it->first;
             if(it->second !="")     //这么写是为了输出格式考虑,以免程序不通过
                 cout<<" "<<it->second;
         }
         cout<<endl;
     }
     return 0;
 }

还得学c++,map都不会

这是我自己写的代码,40分。

新手一个。。


#include <iostream>
#include <math.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include <cstdio>
#include <string.h>
#include <stdio.h>
using namespace std;

struct node{
    char aa[2];
    char bb[256];
};
node p[257];
int main()
{
    //freopen("in.txt","r",stdin);
    char in[53],d[53];
    int n,num,i,N,nd=0;
    int nn;
    cin>>in;
    cin>>n;
    N=strlen(in);
    int w[53];
    memset(w,0,sizeof(w));
    for(i=N-1;i>=0;){
        if(in[i]==':'){
            i--;
            d[nd]=in[i];
            w[nd]=1;
            i--;
            nd++;
        }
        else {
            d[nd]=in[i];
            i--;
            nd++;
        }
    }
    char a[256],b[256];
    int seq[256];
    int j=0,t;
    memset(seq,0,sizeof(seq));
    memset(a,0,sizeof(a));
    cin>>a;
    bool feifa=false,daican=false;
    for(i=1;i<=n;){
        memset(b,0,sizeof(b));
        cin>>b;
        if(strcmp(a,b)==0||strncmp(b,"",1)==0){
            cout<<"Case "<<i<<":";
            for(int k=0;k<j;){
                for(int t=k+1;t<j;){
                    if(strncmp(p[k].aa,p[t].aa,2)==0){
                      seq[k]=3;
                    }
                    else if(strncmp(p[k].aa,p[t].aa,2)>0){
                        swap(p[k].aa,p[t].aa);
                        swap(p[k].bb,p[t].bb);
                        swap(seq[k],seq[t]);
                    }
                    t++;
                }
                k++;
            }
            for(int k=0;k<j;k++){
                if(seq[k]!=3){
                    cout<<" "<<p[k].aa[0]<<p[k].aa[1];
                    if(seq[k]==1){
                        cout<<" "<<p[k].bb;
                    }
                }
            }
            cout<<"\n";
            feifa=false;
            daican=false;
            i++;
            j=0;
            memset(seq,0,sizeof(seq));
        }
        else if(feifa){
            continue;
        }
        else {
            if(daican){
                strcpy(p[j].bb,b);
                j++;
                //p[j].bb=b;
                daican=false;
            }
            else {
                if(strlen(b)!=2||b[0]!='-'){
                    feifa=true;
                    continue;
                }
                char a1;
                a1=b[1];
                for(t=0;t<nd;t++){
                    if(d[t]==a1){
                        strcpy(p[j].aa,b);
                        //p[j].aa=b;
                        if(w[t]!=1){
                            j++;
                        }
                        else {
                            daican=true;
                            seq[j]=1;
                        }
                        break;
                    }
                }
            }
            if(t==nd){
                feifa=true;
            }
        }
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值