⭐CSP 201403-3命令行选项

2024-02-27 12:58
文章标签 csp 命令行 选项 201403

本文主要是介绍⭐CSP 201403-3命令行选项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一开始这样写的,但是是错误的,只有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+1break;}}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;//如果不是命令,直接breakisIn(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;}
}

这篇关于⭐CSP 201403-3命令行选项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/752463

相关文章

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

Python的pip在命令行无法使用问题的解决方法

《Python的pip在命令行无法使用问题的解决方法》PIP是通用的Python包管理工具,提供了对Python包的查找、下载、安装、卸载、更新等功能,安装诸如Pygame、Pymysql等Pyt... 目录前言一. pip是什么?二. 为什么无法使用?1. 当我们在命令行输入指令并回车时,一般主要是出现以

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Windows server服务器使用blat命令行发送邮件

《Windowsserver服务器使用blat命令行发送邮件》在linux平台的命令行下可以使用mail命令来发送邮件,windows平台没有内置的命令,但可以使用开源的blat,其官方主页为ht... 目录下载blatBAT命令行示例备注总结在linux平台的命令行下可以使用mail命令来发送邮件,Win

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

Golang的CSP模型简介(最新推荐)

《Golang的CSP模型简介(最新推荐)》Golang采用了CSP(CommunicatingSequentialProcesses,通信顺序进程)并发模型,通过goroutine和channe... 目录前言一、介绍1. 什么是 CSP 模型2. Goroutine3. Channel4. Channe

redis-cli命令行工具的使用小结

《redis-cli命令行工具的使用小结》redis-cli是Redis的命令行客户端,支持多种参数用于连接、操作和管理Redis数据库,本文给大家介绍redis-cli命令行工具的使用小结,感兴趣的... 目录基本连接参数基本连接方式连接远程服务器带密码连接操作与格式参数-r参数重复执行命令-i参数指定命