命令行选项(50分....)

本文分享了一个C++编程挑战,通过解析特定格式的字符串来处理参数,包括负数和冒号分隔的键值对。文章详细介绍了如何使用C++标准库中的map和stringstream来实现这一功能,并展示了三个不同版本的代码实现,每个版本都有其独特之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

emmmmm,只有50分,,不知道哪儿没考虑到.......改了好多次... 后来发现参数不一定是数字....但是也只有70

//70

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <sstream>
using namespace std;

int main() {
	string s;
	cin >> s;

	int n;

	map<char, int> a;

	for(int i=0; s[i]; i++) {
		if(s[i] == ':')
			continue;
		
		if(s[i+1] && s[i+1] ==':'){
			if(a[s[i]] == -2){
				a[s[i]] == -1;
			} else	a[s[i]] = -3;
		}else a[s[i]] = -2;
	}
	cin >> n;
	cin.get();

	for(int i=0; i<n; i++) {
		string temp;
		map<char, string> b;

		getline(cin, temp);
		
		stringstream ss(temp);
		
		string t;
		char pre;
		
		ss >> t;
		
		while(ss >> t) {
			if(a[pre] == -3 || a[pre] == -1){
				b[pre] = t;
			} else if (t[0] == '-') {
			
				if(t.length() > 2)break;
				if(a[t[1]] < 0)
					b[t[1]] = "";
				else break;

			}else break;
			pre = t[1];
//			cout << t << endl;
		}

		cout << "Case " << i+1 << ": ";
		/*    for(map<char, int>::iterator it = a.begin(); it != a.end(); it++){
		            cout << "-" << it->first << " " << it->second << endl;
		        }
		*/
		for(map<char, string>::iterator it = b.begin(); it != b.end(); it++) {
			if((it -> second).length() > 0) {
				cout << "-" << it->first << " " << it->second << " ";
			}else cout << "-" << it->first << " ";
		}
		cout << endl;
	}

	return 0;
}

 

//50

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <sstream>
using namespace std;

int main() {
	string s;
	cin >> s;

	int n;

	map<char, int> a;

	for(int i=0; s[i]; i++) {
		if(s[i] == ':')
			continue;
	
		if(s[i+1] ==':'){
			if(a[s[i]] == -2){
				a[s[i]] == -1;
			} else	a[s[i]] = -3;
		}else a[s[i]] = -2;
	}
	cin >> n;
	cin.get();

	for(int i=0; i<n; i++) {
		string temp;
		map<char, int> b;

		getline(cin, temp);
		
		stringstream ss(temp);
		
		string t;
		char pre;
		
		ss >> t;

		while(ss >> t) {
			if (t[0] == '-') {
				if(t.length() > 2)break;
				if(a[t[1]] <0)
					b[t[1]] = -1;
				else break;

			} else if (isdigit(t[0])) {
				if(a[pre] != -3 && a[pre] != -1)break;
				int j = 0, tmp = 0;
				while (t[j] && isdigit(t[j])) {
					tmp *= 10;
					tmp += t[j] - '0';
					j++;
				}
				if(j != t.length()) {
					break;
				}
				b[pre] = tmp;
			} else break;
			pre = t[1];
//			cout << t << endl;
		}

		cout << "Case " << i+1 << ": ";
		/*    for(map<char, int>::iterator it = a.begin(); it != a.end(); it++){
		            cout << "-" << it->first << " " << it->second << endl;
		        }
		*/
		for(map<char, int>::iterator it = b.begin(); it != b.end(); it++) {
			if(it -> second > 0) {
				cout << "-" << it->first << " " << it->second << " ";
			}else cout << "-" << it->first << " ";
		}
		cout << endl;
	}

	return 0;
}
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <sstream>
using namespace std;

int main() {
	string s;
	cin >> s;

	int n;

	map<char, int> a;

	for(int i=0; s[i]; i++) {
		if(s[i] == ':')
			continue;

		a[s[i]] = -2;
		if(s[i+1] ==':') a[toupper(s[i])] = -3;
	}
	cin >> n;
	cin.get();

	for(int i=0; i<n; i++) {
		string temp;
		map<char, int> b;

		getline(cin, temp);
		stringstream ss(temp);
		string t;
		char pre;
		while(ss >> t) {
			if (t[2] == '\0') {
				if (t[0] == '-') {
					if(a[t[1]] < 0)
						b[t[1]] = -1;
					else if(a[t[1]] == 0)
						break;

				} else if (isdigit(t[0])) {
					int j = 0, tmp = 0;
					while (t[j]) {
						tmp *= 10;
						tmp += t[j] - '0';
						j++;
					}
					if(a[toupper(pre)] == -3 || b[toupper(pre)] > 0 )
						b[pre] = tmp;
					else break;
				}
				pre = t[1];
			} else break;
//			cout << t << endl;
		}

		cout << "Case " << i+1 << ": ";
		/*    for(map<char, int>::iterator it = a.begin(); it != a.end(); it++){
		            cout << "-" << it->first << " " << it->second << endl;
		        }
		*/
		for(map<char, int>::iterator it = b.begin(); it != b.end(); it++) {
			if(it->second == -1) {
				cout << "-" << it->first << " ";
				it->second = -2;
			}
			if(it -> second > 0) {
				cout << "-" << it->first << " " << it->second << " ";
				it ->second = -2;
			}
		}
		cout << endl;
	}

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值