输入两个C语言语句,第一句为正常的C语言变量定义语句,符合C语言语法要求,变量间可以有多个空格,包含数组,指针定义等。第二句为变量运算语句,要求输出第二个C语言语句中未定义的变量。
思路:
1、使用stringstream类,流输入,使用set
这个方法不完善,遇到数组的处理不了,但可以作为一种方法学习。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<sstream>
#include<set>
using namespace std;
int main() {
string str;
getline(cin, str);
string pick;
getline(cin, pick);
for (int i = 0; i < str.length(); i++) {
if (str[i] == ',' || str[i] == ';' || str[i] == '=') {
str[i] = ' ';
}
}
set<string> words;
stringstream ss(str);
string word;
while (ss >> word) {
words.insert(word);
}
for (set<string>::iterator it = words.begin(); it != words.end(); it++) {
cout << *it << " ";
}
for (int i = 0; i < pick.length(); i++) {
if (pick[i] == '+' || pick[i] == '-' || pick[i] == '=' || pick[i] == '*' || pick[i] == '/' || pick[i] == '%' || pick[i] == '(' || pick[i] == ')'||pick[i]==';') {
pick[i] = ' ';
}
}
stringstream ss2(pick);
string word2;
while (ss2 >> word2) {
if (words.find(word2) == words.end()) {
cout << word2 << " ";
}
}
return 0;
}