-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdef.cpp
45 lines (41 loc) · 1.3 KB
/
def.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <sstream>
#include <unordered_map>
std::unordered_map<std::string, std::string> str_variables;
std::unordered_map<std::string, int> int_variables;
void process_line(const std::string& line) {
std::istringstream iss(line);
std::string command;
iss >> command;
if (command == "str:") {
std::string var_name, value;
iss >> var_name >> std::ws;
std::getline(iss, value);
str_variables[var_name] = value;
} else if (command == "int:") {
std::string var_name;
int value;
iss >> var_name >> value;
int_variables[var_name] = value;
} else if (command == "print") {
std::string var_name;
iss >> var_name;
if (str_variables.find(var_name) != str_variables.end()) {
std::cout << str_variables[var_name] << std::endl;
} else if (int_variables.find(var_name) != int_variables.end()) {
std::cout << int_variables[var_name] << std::endl;
} else {
std::cerr << "Undefined variable: " << var_name << std::endl;
}
} else {
std::cerr << "Unknown command: " << command << std::endl;
}
}
int main() {
std::string line;
while (std::getline(std::cin, line)) {
process_line(line);
}
return 0;
}