Advertisement
Guest User

100

a guest
Apr 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <vector>
  5. #include <sstream>
  6. using namespace std;
  7. map<string, string> Var;
  8.  
  9. bool isnum(const string& str) {
  10.     for (int i = 0; i < (int)str.size(); i++) {
  11.         if (str[i]<'0' || str[i]>'9')
  12.             return false;
  13.     }
  14.     return true;
  15. }
  16.  
  17. string calc(const vector<string>& line) {
  18.     vector<string> tmp;
  19.     for (int i = 0; i < (int)line.size();) {
  20.         //      cout << "entering " << i << endl;
  21.         if (line[i] != ">"&&line[i] != "<"&&line[i] != ">="&&line[i] != "<="&&line[i] != "=="&&line[i] != "!=") {
  22.             tmp.push_back(line[i]);
  23.             //          cout << i << endl;
  24.             i++;
  25.         }
  26.  
  27.         else {
  28.             //          cout << "find a comparisonoperator" << endl;
  29.             int left, right;
  30.             //提取左操作数
  31.             if (isnum(line[i - 1]))
  32.                 left = stoi(line[i - 1]);
  33.             else if (line[i - 1] == "True")
  34.                 left = 1;
  35.             else if (line[i - 1] == "False")
  36.                 left = 0;
  37.             else {
  38.                 string num = (*Var.find(line[i - 1])).second;   //必定找得到
  39.                 if (num == "True")
  40.                     left = 1;
  41.                 else if (num == "False")
  42.                     left = 0;
  43.                 else
  44.                     left = stoi(num);
  45.             }
  46.             //          cout << "left" << endl;
  47.                         //提取右操作数           
  48.             if (isnum(line[i + 1]))
  49.                 right = stoi(line[i + 1]);
  50.             else if (line[i + 1] == "True")
  51.                 right = 1;
  52.             else if (line[i + 1] == "False")
  53.                 right = 0;
  54.             else {
  55.                 string num = (*Var.find(line[i + 1])).second;   //必定找得到
  56.                 if (num == "True")
  57.                     right = 1;
  58.                 else if (num == "False")
  59.                     right = 0;
  60.                 else
  61.                     right = stoi(num);
  62.             }
  63.             //          cout << "right" << endl;
  64.                         //计算   
  65.             int id = tmp.size() - 1;
  66.             if (line[i] == ">") {
  67.                 //              cout << 1 << endl;
  68.                 if (left > right)
  69.                     tmp[id] = "True";
  70.                 else
  71.                     tmp[id] = "False";
  72.             }
  73.             else if (line[i] == "<") {
  74.                 //              cout << 2 << endl;
  75.                 if (left < right)
  76.                     tmp[id] = "True";
  77.                 else
  78.                     tmp[id] = "False";
  79.             }
  80.             else if (line[i] == ">=") {
  81.                 //              cout << 3 << endl;
  82.                 if (left >= right)
  83.                     tmp[id] = "True";
  84.                 else
  85.                     tmp[id] = "False";
  86.             }
  87.             else if (line[i] == "<=") {
  88.                 //              cout << 4 << endl;
  89.                 if (left <= right)
  90.                     tmp[id] = "True";   //??????????,????????????1?,??????????
  91.                 else
  92.                     tmp[id] = "False";
  93.             }
  94.             else if (line[i] == "!=") {
  95.                 //              cout << 5 << endl;
  96.                 if (left != right)
  97.                     tmp[id] = "True";
  98.                 else
  99.                     tmp[id] = "False";
  100.             }
  101.             else if (line[i] == "==") {
  102.                 //              cout << 6 << endl;
  103.                 if (left == right)
  104.                     tmp[id] = "True";
  105.                 else
  106.                     tmp[id] = "False";
  107.             }
  108.             //          cout << i << endl;
  109.             i += 2;//跳过右操作数
  110.  
  111.         }
  112.     }
  113.     vector<string> tmp1;
  114.     for (int i = 0; i < tmp.size();) {
  115.         //      cout << "not" << endl;
  116.         if (tmp[i] != "not")
  117.             tmp1.push_back(tmp[i++]);
  118.         else {
  119.             //处理右操作数
  120.             int right;
  121.             if (isnum(tmp[i + 1]))
  122.                 right = stoi(tmp[i + 1]);
  123.             else if (tmp[i + 1] == "True")
  124.                 right = 1;
  125.             else if (tmp[i + 1] == "False")
  126.                 right = 0;
  127.             else {
  128.                 string num = (*Var.find(tmp[i + 1])).second;    //必定找得到
  129.                 if (num == "True")
  130.                     right = 1;
  131.                 else if (num == "False")
  132.                     right = 0;
  133.                 else
  134.                     right = stoi(num);
  135.             }
  136.             if (right)
  137.                 tmp1.push_back("False");
  138.             else if (!right)
  139.                 tmp1.push_back("True");
  140.             i += 2;
  141.         }
  142.     }
  143.     vector<string> tmp2;
  144.     for (int i = 0; i < tmp1.size();) {
  145.         //      cout << "and" << endl;
  146.         if (tmp1[i] != "and") {
  147.             tmp2.push_back(tmp1[i++]);
  148.         }
  149.         else {
  150.             int left, right;
  151.             if (isnum(tmp1[i - 1]))
  152.                 left = stoi(tmp1[i - 1]);
  153.             else if (tmp1[i - 1] == "True")
  154.                 left = 1;
  155.             else if (tmp1[i - 1] == "False")
  156.                 left = 0;
  157.             else {
  158.                 string num = (*Var.find(tmp1[i - 1])).second;   //必定找得到
  159.                 if (num == "True")
  160.                     left = 1;
  161.                 else if (num == "False")
  162.                     left = 0;
  163.                 else
  164.                     left = stoi(num);
  165.             }
  166.  
  167.             if (isnum(tmp1[i + 1]))
  168.                 right = stoi(line[i + 1]);
  169.             else if (tmp1[i + 1] == "True")
  170.                 right = 1;
  171.             else if (tmp1[i + 1] == "False")
  172.                 right = 0;
  173.             else {
  174.                 string num = (*Var.find(tmp1[i + 1])).second;   //必定找得到
  175.                 if (num == "True")
  176.                     right = 1;
  177.                 else if (num == "False")
  178.                     right = 0;
  179.                 else
  180.                     right = stoi(num);
  181.             }
  182.             int id = tmp2.size() - 1;
  183.             if (left == 0 || right == 0)
  184.                 tmp2[id] = "False";
  185.             else
  186.                 tmp2[id] = "True";
  187.             i += 2;
  188.         }
  189.     }
  190.     vector<string> tmp3;
  191.     for (int i = 0; i < tmp2.size();) {
  192.         //      cout << "or" << endl;
  193.         if (tmp2[i] != "or") {
  194.             tmp3.push_back(tmp2[i++]);
  195.         }
  196.         else {
  197.             int left, right;
  198. //          cout << tmp3[tmp3.size() - 1] << endl;
  199.             if (i - 2 < 0 || (i - 2 >= 0 && tmp2[i - 2] != "or")) {
  200. //              cout << "single or" << endl;
  201.                 if (isnum(tmp2[i - 1]))
  202.                     left = stoi(tmp1[i - 1]);
  203.                 else if (tmp2[i - 1] == "True")
  204.                     left = 1;
  205.                 else if (tmp2[i - 1] == "False")
  206.                     left = 0;
  207.                 else {
  208.                     string num = (*Var.find(tmp2[i - 1])).second;   //必定找得到
  209.                     if (num == "True")
  210.                         left = 1;
  211.                     else if (num == "False")
  212.                         left = 0;
  213.                     else
  214.                         left = stoi(num);
  215.                 }
  216.             }
  217.             else if (i - 2 >= 0 && tmp2[i - 2] == "or") {
  218. //              cout << "double or" << endl;
  219.                 string bef = tmp3[tmp3.size() - 1];
  220. //              cout << "Before: " << bef << endl;
  221.                 if (isnum(bef))
  222.                     left = stoi(bef);
  223.                 else if (bef == "True")
  224.                     left = 1;
  225.                 else if (bef == "False")
  226.                     left = 0;
  227.                 else {
  228.                     string num = (*Var.find(bef)).second;   //必定找得到
  229.                     if (num == "True")
  230.                         left = 1;
  231.                     else if (num == "False")
  232.                         left = 0;
  233.                     else
  234.                         left = stoi(num);
  235.                 }
  236.             }
  237.  
  238.             if (isnum(tmp2[i + 1]))
  239.                 right = stoi(line[i + 1]);
  240.             else if (tmp2[i + 1] == "True")
  241.                 right = 1;
  242.             else if (tmp2[i + 1] == "False")
  243.                 right = 0;
  244.             else {
  245.                 string num = (*Var.find(tmp2[i + 1])).second;   //必定找得到
  246.                 if (num == "True")
  247.                     right = 1;
  248.                 else if (num == "False")
  249.                     right = 0;
  250.                 else
  251.                     right = stoi(num);
  252.             }
  253.             int id = tmp3.size() - 1;
  254.             if (left == 0 && right == 0)
  255.                 tmp3[id] = "False";
  256.             else
  257.                 tmp3[id] = "True";
  258.             i += 2;
  259.         }
  260.     }
  261.     //  cout << "size " << tmp3.size() << " num " << tmp3[0] << endl;   //测试语句,看tmp3是不是只是一个数或者一个字母或者true/false
  262.     if (isnum(tmp3[0]) || tmp3[0] == "True" || tmp3[0] == "False")
  263.         return tmp3[0];
  264.     else {
  265.         return (*Var.find(tmp3[0])).second;
  266.     }
  267. }
  268.  
  269. int main() {
  270.     string s;
  271.     string t;
  272.     //这两句可以有,也可以没有
  273.     //  Var.insert(make_pair("True",1));
  274.     //  Var.insert(make_pair("False",0));
  275.     while (getline(cin, s)) {
  276.         //将字符串按照空格区分存在vector中
  277.         stringstream ss(s);
  278.         string tmp;
  279.         vector<string> line;
  280.         while (ss >> tmp) {
  281.             line.push_back(tmp);
  282.             //          cout << tmp << endl;
  283.         }
  284.         //寻找=位置
  285.         if (line[1] == "=") {   //tf >= 0说明是赋值运算
  286. //          cout << "entering 1" << endl;
  287.             vector<string> ln(line.begin() + 2, line.end());
  288.             t = calc(ln);
  289.             Var[line[0]] = t;
  290.         }
  291.         else if (line[0] == "print") {
  292.             vector<string> ln(line.begin() + 1, line.end());
  293.             t = calc(ln);
  294.             cout << t << endl;
  295.         }
  296.     }
  297.     return 0;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement