Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <map>
- #include <vector>
- #include <sstream>
- using namespace std;
- map<string, string> Var;
- bool isnum(const string& str) {
- for (int i = 0; i < (int)str.size(); i++) {
- if (str[i]<'0' || str[i]>'9')
- return false;
- }
- return true;
- }
- string calc(const vector<string>& line) {
- vector<string> tmp;
- for (int i = 0; i < (int)line.size();) {
- // cout << "entering " << i << endl;
- if (line[i] != ">"&&line[i] != "<"&&line[i] != ">="&&line[i] != "<="&&line[i] != "=="&&line[i] != "!=") {
- tmp.push_back(line[i]);
- // cout << i << endl;
- i++;
- }
- else {
- // cout << "find a comparisonoperator" << endl;
- int left, right;
- //提取左操作数
- if (isnum(line[i - 1]))
- left = stoi(line[i - 1]);
- else if (line[i - 1] == "True")
- left = 1;
- else if (line[i - 1] == "False")
- left = 0;
- else {
- string num = (*Var.find(line[i - 1])).second; //必定找得到
- if (num == "True")
- left = 1;
- else if (num == "False")
- left = 0;
- else
- left = stoi(num);
- }
- // cout << "left" << endl;
- //提取右操作数
- if (isnum(line[i + 1]))
- right = stoi(line[i + 1]);
- else if (line[i + 1] == "True")
- right = 1;
- else if (line[i + 1] == "False")
- right = 0;
- else {
- string num = (*Var.find(line[i + 1])).second; //必定找得到
- if (num == "True")
- right = 1;
- else if (num == "False")
- right = 0;
- else
- right = stoi(num);
- }
- // cout << "right" << endl;
- //计算
- int id = tmp.size()-1;
- if (line[i] == ">") {
- // cout << 1 << endl;
- if (left > right)
- tmp[id] = "True";
- else
- tmp[id] = "False";
- }
- else if (line[i] == "<") {
- // cout << 2 << endl;
- if (left < right)
- tmp[id] = "True";
- else
- tmp[id] = "False";
- }
- else if (line[i] == ">=") {
- // cout << 3 << endl;
- if (left >= right)
- tmp[id] = "True";
- else
- tmp[id] = "False";
- }
- else if (line[i] == "<=") {
- // cout << 4 << endl;
- if (left <= right)
- tmp[id] = "True"; //??????????,????????????1?,??????????
- else
- tmp[id] = "False";
- }
- else if (line[i] == "!=") {
- // cout << 5 << endl;
- if (left != right)
- tmp[id] = "True";
- else
- tmp[id] = "False";
- }
- else if (line[i] == "==") {
- // cout << 6 << endl;
- if (left == right)
- tmp[id] = "True";
- else
- tmp[id] = "False";
- }
- // cout << i << endl;
- i += 2;//跳过右操作数
- }
- }
- vector<string> tmp1;
- for (int i = 0; i < tmp.size();) {
- // cout << "not" << endl;
- if (tmp[i] != "not")
- tmp1.push_back(tmp[i++]); //?????,?????????not,???tmp1??vector,??????????
- else {
- //处理右操作数
- int right;
- if (isnum(tmp[i + 1]))
- right = stoi(tmp[i + 1]);
- else if (tmp[i + 1] == "True")
- right = 1;
- else if (tmp[i + 1] == "False")
- right = 0;
- else {
- string num = (*Var.find(tmp[i + 1])).second; //必定找得到
- if (num == "True")
- right = 1;
- else if (num == "False")
- right = 0;
- else
- right = stoi(num);
- }
- if (right)
- tmp1.push_back("False");
- else if (!right)
- tmp1.push_back("True");
- i += 2;
- }
- }
- vector<string> tmp2;
- for (int i = 0; i < tmp1.size();) {
- // cout << "and" << endl;
- if (tmp1[i] != "and") {
- tmp2.push_back(tmp1[i++]);
- }
- else {
- int left, right;
- if (isnum(tmp1[i - 1]))
- left = stoi(tmp1[i - 1]);
- else if (tmp1[i - 1] == "True")
- left = 1;
- else if (tmp1[i - 1] == "False")
- left = 0;
- else {
- string num = (*Var.find(tmp1[i - 1])).second; //必定找得到
- if (num == "True")
- left = 1;
- else if (num == "False")
- left = 0;
- else
- left = stoi(num);
- }
- if (isnum(tmp1[i + 1]))
- right = stoi(line[i + 1]);
- else if (tmp1[i + 1] == "True")
- right = 1;
- else if (tmp1[i + 1] == "False")
- right = 0;
- else {
- string num = (*Var.find(tmp1[i + 1])).second; //必定找得到
- if (num == "True")
- right = 1;
- else if (num == "False")
- right = 0;
- else
- right = stoi(num);
- }
- int id = tmp2.size() - 1;
- if (left == 0 || right == 0)
- tmp2[id] = "False";
- else
- tmp2[id] = "True";
- i += 2;
- }
- }
- vector<string> tmp3;
- for (int i = 0; i < tmp2.size();) {
- // cout << "or" << endl;
- if (tmp2[i] != "or") {
- tmp3.push_back(tmp2[i++]);
- }
- else {
- int left, right;
- if (isnum(tmp2[i - 1]))
- left = stoi(tmp1[i - 1]);
- else if (tmp2[i - 1] == "True")
- left = 1;
- else if (tmp2[i - 1] == "False")
- left = 0;
- else {
- string num = (*Var.find(tmp2[i - 1])).second; //必定找得到
- if (num == "True")
- left = 1;
- else if (num == "False")
- left = 0;
- else
- left = stoi(num);
- }
- if (isnum(tmp2[i + 1]))
- right = stoi(line[i + 1]);
- else if (tmp2[i + 1] == "True")
- right = 1;
- else if (tmp2[i + 1] == "False")
- right = 0;
- else {
- string num = (*Var.find(tmp2[i + 1])).second; //必定找得到
- if (num == "True")
- right = 1;
- else if (num == "False")
- right = 0;
- else
- right = stoi(num);
- }
- int id = tmp3.size() - 1;
- if (left == 0 && right == 0)
- tmp3[id] = "False";
- else
- tmp3[id] = "True";
- i += 2;
- }
- }
- // cout << "size " << tmp3.size() << " num " << tmp3[0] << endl; //测试语句,看tmp3是不是只是一个数或者一个字母或者true/false
- if (isnum(tmp3[0]) || tmp3[0] == "True" || tmp3[0] == "False")
- return tmp3[0];
- else {
- return (*Var.find(tmp3[0])).second;
- }
- }
- int main() {
- string s;
- string t;
- //这两句可以有,也可以没有
- // Var.insert(make_pair("True",1));
- // Var.insert(make_pair("False",0));
- while (getline(cin, s)) {
- //将字符串按照空格区分存在vector中
- stringstream ss(s);
- string tmp;
- vector<string> line;
- while (ss >> tmp) {
- line.push_back(tmp);
- // cout << tmp << endl;
- }
- //寻找=位置
- if (line[1] == "=") {
- // cout << "entering 1" << endl;
- vector<string> ln(line.begin() + 2, line.end());
- t = calc(ln);
- Var[line[0]] = t;
- }
- else if(line[0] == "print"){
- vector<string> ln(line.begin() + 1, line.end());
- t = calc(ln);
- cout << t << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement