Advertisement
Guest User

oopt2

a guest
Apr 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 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++]);   //?????,?????????not,???tmp1??vector,??????????
  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.             if (isnum(tmp2[i - 1]))
  199.                 left = stoi(tmp1[i - 1]);
  200.             else if (tmp2[i - 1] == "True")
  201.                 left = 1;
  202.             else if (tmp2[i - 1] == "False")
  203.                 left = 0;
  204.             else {
  205.                 string num = (*Var.find(tmp2[i - 1])).second;   //必定找得到
  206.                 if (num == "True")
  207.                     left = 1;
  208.                 else if (num == "False")
  209.                     left = 0;
  210.                 else
  211.                     left = stoi(num);
  212.             }
  213.  
  214.             if (isnum(tmp2[i + 1]))
  215.                 right = stoi(line[i + 1]);
  216.             else if (tmp2[i + 1] == "True")
  217.                 right = 1;
  218.             else if (tmp2[i + 1] == "False")
  219.                 right = 0;
  220.             else {
  221.                 string num = (*Var.find(tmp2[i + 1])).second;   //必定找得到
  222.                 if (num == "True")
  223.                     right = 1;
  224.                 else if (num == "False")
  225.                     right = 0;
  226.                 else
  227.                     right = stoi(num);
  228.             }
  229.             int id = tmp3.size() - 1;
  230.             if (left == 0 && right == 0)
  231.                 tmp3[id] = "False";
  232.             else
  233.                 tmp3[id] = "True";
  234.             i += 2;
  235.         }
  236.     }
  237. //  cout << "size " << tmp3.size() << " num " << tmp3[0] << endl;   //测试语句,看tmp3是不是只是一个数或者一个字母或者true/false
  238.     if (isnum(tmp3[0]) || tmp3[0] == "True" || tmp3[0] == "False")
  239.         return tmp3[0];
  240.     else {
  241.         return (*Var.find(tmp3[0])).second;
  242.     }
  243. }
  244.  
  245. int main() {
  246.     string s;
  247.     string t;
  248.     //这两句可以有,也可以没有
  249.     //  Var.insert(make_pair("True",1));
  250.     //  Var.insert(make_pair("False",0));
  251.     while (getline(cin, s)) {
  252.         //将字符串按照空格区分存在vector中
  253.         stringstream ss(s);
  254.         string tmp;
  255.         vector<string> line;
  256.         while (ss >> tmp) {
  257.             line.push_back(tmp);
  258. //          cout << tmp << endl;
  259.         }
  260.         //寻找=位置
  261.         if (line[1] == "=") {  
  262. //          cout << "entering 1" << endl;
  263.             vector<string> ln(line.begin() + 2, line.end());
  264.             t = calc(ln);
  265.             Var[line[0]] = t;
  266.         }
  267.         else if(line[0] == "print"){
  268.             vector<string> ln(line.begin() + 1, line.end());
  269.             t = calc(ln);
  270.             cout << t << endl;
  271.         }
  272.     }
  273.     return 0;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement