一开始这样写的,但是是错误的,只有20分
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<limits>
using namespace std;
//命令行选项
const int maxn = 280;//最多256个命令
int hashtable[256];
char str[maxn];//每一个命令行
string temp;
string nonop;//无参数选项
string op;//带参数选项
char a[maxn][maxn];
struct Output {
char op;
char out[maxn];
}output[maxn];
bool cmp(Output a, Output b) {
return a.op < a.op;
}
int main()
{
scanf("%s", str);
int i;
for (i = strlen(str); i >= 0; i--)
{
if (str[i] == ':')
op.insert(op.end(), str[--i]);
else
{
nonop.insert(nonop.end(), str[i]);
}
}
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int num = 0;
int outnum = 0;
fill(hashtable, hashtable + maxn, 0);
getchar();
gets(str);
//puts(str);
int j = 0;
//printf("len=%d", strlen(str));
for (; j < strlen(str); j++)
{
if (str[j] == ' ')
{
break;
}
}
//printf("j=%d\n", j);
for (; j < strlen(str);)
{
if (str[j] == ' ' && str[j + 1] == '-')//如果遇到了选项
{
j++;//每一个空格之后的位置;
if (nonop.find(str[j + 1])!=string::npos)//如果是无参数选项
{
//printf("找到了无参数选项");
if (hashtable[str[j + 1]] == 0)
{
hashtable[str[j + 1]] = 1;
output[num].out[0] = str[j];
output[num].out[1] = str[j + 1];
output[num].op = str[j + 1];
num++;
}
j = j + 2;
}
else if (op.find(str[j + 1]) != string::npos)//如果是有参数选项
{
//printf("有参数!\n");
int k;
if (hashtable[str[j + 1]] == 0)
{
hashtable[str[j + 1]] = 1;
output[num].out[0] = str[j];
output[num].out[1] = str[j + 1];
output[num].op = str[j + 1];
k = num;
}
else
{
for (k = 0; k < num; k++)
{
if (output[k].op == str[j + 1])//如果op等于j+1
break;
}
}
j = j + 2;
outnum = 0;
do {
a[output[k].op][outnum++] = str[j];
j++;
} while (str[j] != ' ');
num++;
}
else
{
break;
}
}
else//如果遇到了非法的字符串
{
break;
}
}
sort(output, output + num, cmp);
printf("Case %d: ", i);
for (int j = 0; j < num; j++)
{
printf("%s", output[j].out);
if (op.find(output[j].out[1]) != string::npos)
printf("%s",a[output[j].out[1]]);
if (j != num - 1)
printf(" ");
}
printf("\n");
}
return 0;
}
启发:
getline来输入,需要表明头文件#include
另外将每一行根据空格分开后单独存在vector v中
for (int t = line.find(" "); t != -1; t = line.find(" ")) //将每一行根据空格分开以后存储在vector中
{
v.push_back(line.substr(0, t));
line = line.substr(t + 1);
}
v.push_back(line);
完整代码:
不得不说stl真的好,可是我用不熟练ww!总是会出错。
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include<string>
using namespace std;
/*
检查选项是否合法 ,并且是否是带参数的
*/
void isIn(string s, char a, bool flag[])
{
for (int i = 0; i < s.length(); i++)
{
if (a == s[i])
{
flag[0] = true;
if (s[i + 1] == ':') flag[1] = true;
break;
}
}
}
int main()
{
string s, s2;
cin >> s;
int n;
cin >> n;
map <char, string> m; //记得清零
getline(cin, s2);
vector <string> v;
bool flag[2]; //初始化为false
for (int i = 0; i < n; i++)
{
memset(flag, false, sizeof(flag));
m.clear();
v.clear();
string s1;
string line;
getline(cin, s1);
line = s1;
for (int t = line.find(" "); t != -1; t = line.find(" ")) //将每一行根据空格分开以后存储在vector中
{
v.push_back(line.substr(0, t));
line = line.substr(t + 1);
}
v.push_back(line);
/*
对vector中的每一个数据进行检查,不合法直接break
若为合法数据,则将他与“!”装进map中
若为带参数, 则更新“!”为参数放进map中
*/
vector<string> ::iterator it;
for (it = v.begin() + 1; it != v.end(); it++)//是begin+1是因为不能计算命令
{
memset(flag, false, sizeof(flag));
string ss = *it;
if (ss[0] != '-' || ss.length() != 2) break;//如果不是命令,直接break
isIn(s, ss[1], flag);
if (flag[0])
{
m.insert(pair <char, string>(ss[1], "!"));
// cout<<"insert :"<<ss[1]<<endl;
if (flag[1] && it + 1 != v.end()) //不加这个只有90分
{
it += 1;
string paramter = *it;
m[ss[1]] = paramter;
// cout<<m[ss[1]]<<endl;
}
}
else break;
}
cout << "Case " << i + 1 << ":";
map <char, string> ::iterator iter;
for (iter = m.begin(); iter != m.end(); iter++)
{
if (iter->second == "!") cout << " -" << iter->first;
else cout << " -" << iter->first << " " << iter->second;
}
cout << endl;
}
}