Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <stack>
  9. #include "ArgumentManager.h"
  10.  
  11. using namespace std;
  12. //Abeer Malik 1514719
  13. struct node
  14.     {
  15.         char data;
  16.         //string data2;
  17.         struct node* next;
  18.     };
  19. //struct node* top;
  20. //struct node* top2;
  21.  
  22. class Stack {
  23. private:
  24.     node* top;
  25. public:
  26.     Stack();
  27.     void push(char x);
  28.     /*void push2(string y);*/
  29.     void pop();
  30.     void Display();
  31.     bool Balanced(string value);
  32.     int prec(char c);
  33.     //void infixToPostfix(string s);
  34.     string infixToPostfix(string s);
  35.     /*int is_operator(char symbol);
  36.     int precedence(char symbol);*/
  37.     int evaluatePostfix(string exp);
  38.     bool isEmpty();
  39. };
  40.  
  41. Stack::Stack()
  42. {
  43.     top = NULL;
  44. }
  45. void Stack::push(char x)
  46. {
  47.     struct node* temp;
  48.     temp = (struct node*)malloc(sizeof(struct node));
  49.     if (temp == NULL)
  50.         cout << "stack is full" << endl;
  51.     else
  52.     {
  53.         temp->data = x;
  54.         temp->next = top;
  55.         top = temp;
  56.     }
  57.  
  58. }
  59. //void Stack::push2(string y)
  60. //{
  61. //  struct node* temp2;
  62. //  temp2 = (struct node*)malloc(sizeof(struct node));
  63. // 
  64. //      temp2->data2 = y;
  65. //      temp2->next = top2;
  66. //      top2 = temp2;
  67. // 
  68. //}
  69. void Stack::pop()
  70. {
  71.     struct node* temp;
  72.     if (top == NULL)
  73.     {
  74.         cout << "stack is empty" << endl;
  75.     }
  76.     else
  77.     {
  78.         temp = top;
  79.         top = top->next;
  80.         temp->next = NULL;
  81.         free(temp);
  82.     }
  83.  
  84. }
  85. void Stack::Display()
  86. {
  87.     struct node* temp;
  88.     if (top == NULL)
  89.     {
  90.         cout << "stack is empty" << endl;
  91.     }
  92.     else
  93.     {
  94.         temp = top;
  95.         while (temp != NULL)
  96.         {
  97.             cout << temp->data << endl;
  98.             temp = temp->next;
  99.         }
  100.     }
  101. }
  102. bool Stack::Balanced(string value)
  103. {
  104.     int i = 0;
  105.         while(value[i])
  106.         {
  107.             if (value[i] == '(')
  108.             {
  109.                 push(value[i]);
  110.             }
  111.             else if (value[i] == '[')
  112.             {
  113.                 push(value[i]);
  114.             }
  115.             else if (value[i] == '{')
  116.             {
  117.                 push(value[i]);
  118.             }
  119.             else if (value[i] == ')')
  120.             {
  121.                 if (top == NULL || top->data != '(')
  122.                     return false;
  123.                 pop();
  124.             }
  125.             else if (value[i] == ']')
  126.             {
  127.                 if (top == NULL || top->data != '[')
  128.                     return false;
  129.                 pop();
  130.             }
  131.             else if (value[i] == '}')
  132.             {
  133.                 if (top == NULL || top->data != '{')
  134.                     return false;
  135.                 pop();
  136.             }
  137.             i++;
  138.         }
  139.         if (top == NULL)
  140.             return true;
  141.         else
  142.             return false;
  143. }
  144. bool Stack::isEmpty()
  145. {
  146.     if (top == NULL)
  147.         return true;
  148.     return false;
  149. }
  150.  
  151. int Stack::prec(char c)
  152. {
  153.     if (c == '^')
  154.         return 3;
  155.     else if (c == '*' || c == '/')
  156.         return 2;
  157.     else if (c == '+' || c == '-')
  158.         return 1;
  159.     else
  160.         return -1;
  161. }
  162.  
  163. string Stack::infixToPostfix(string s)
  164. {
  165.     stack<char> st;
  166.     st.push('N');
  167.     int l = s.length();
  168.     string ns;
  169.     for (int i = 0; i < l; i++)
  170.     {
  171.         // If the scanned character is an operand, add it to output string.
  172.         if (isalpha(s[i]) || isdigit(s[i]))
  173.             ns += s[i];
  174.  
  175.         // If the scanned character is an ‘(‘, push it to the stack.
  176.         else if (s[i] == '(')
  177.             st.push('(');
  178.         else if (s[i] == '[')
  179.             st.push('[');
  180.         else if (s[i] == '{')
  181.             st.push('{');
  182.         // If the scanned character is an ‘)’, pop and to output string from the stack
  183.         // until an ‘(‘ is encountered.
  184.         else if (s[i] == ')')
  185.         {
  186.             while (st.top() != 'N' && st.top() != '(')
  187.             {
  188.                 char c = st.top();
  189.                 st.pop();
  190.                 ns += c;
  191.             }
  192.             if (st.top() == '(')
  193.             {
  194.                 char c = st.top();
  195.                 st.pop();
  196.             }
  197.         }
  198.         else if (s[i] == ']')
  199.         {
  200.             while (st.top() != 'N' && st.top() != '[')
  201.             {
  202.                 char c = st.top();
  203.                 st.pop();
  204.                 ns += c;
  205.             }
  206.             if (st.top() == '[')
  207.             {
  208.                 char c = st.top();
  209.                 st.pop();
  210.             }
  211.         }
  212.         else if (s[i] == '}')
  213.         {
  214.             while (st.top() != 'N' && st.top() != '{')
  215.             {
  216.                 char c = st.top();
  217.                 st.pop();
  218.                 ns += c;
  219.             }
  220.             if (st.top() == '{')
  221.             {
  222.                 char c = st.top();
  223.                 st.pop();
  224.             }
  225.         }
  226.  
  227.         //If an operator is scanned
  228.         else {
  229.             while (st.top() != 'N' && prec(s[i]) <= prec(st.top()))
  230.             {
  231.                 char c = st.top();
  232.                 st.pop();
  233.                 ns += c;
  234.             }
  235.             st.push(s[i]);
  236.         }
  237.  
  238.     }
  239.     //Pop all the remaining elements from the stack
  240.     while (st.top() != 'N')
  241.     {
  242.         char c = st.top();
  243.         st.pop();
  244.         ns += c;
  245.     }
  246.     st.pop();
  247.     return ns;
  248.  
  249. }
  250.  
  251. int Stack::evaluatePostfix(string exp)
  252. {
  253.     stack<int> st;
  254.    
  255.     int temp;
  256.     // Create a stack of capacity equal to expression size  
  257.     int i;
  258.  
  259.     // See if stack was created successfully  
  260.  
  261.     // Scan all characters one by one  
  262.     for (i = 0; exp[i]; ++i)
  263.     {
  264.         //if the character is blank space then continue  
  265.         if (exp[i] == ' ')continue;
  266.  
  267.         // If the scanned character is an  
  268.         // operand (number here),extract the full number  
  269.         // Push it to the stack.  
  270.         else if (isdigit(exp[i]))
  271.         {
  272.             int num = 0;
  273.  
  274.             //extract full number  
  275.             while (isdigit(exp[i]))
  276.             {
  277.                 num = num * 10 + (int)(exp[i] - '0');
  278.                 i++;
  279.             }
  280.             i--;
  281.  
  282.             //push the element in the stack  
  283.             st.push(num);
  284.         }
  285.  
  286.         // If the scanned character is an operator, pop two  
  287.         // elements from stack apply the operator  
  288.         else
  289.         {
  290.             int val1 = st.top();
  291.             st.pop();
  292.             int val2 = st.top();
  293.             st.pop();
  294.             switch (exp[i])
  295.             {
  296.             case '+': st.push(val2 + val1); break;
  297.             case '-': st.push(val2 - val1); break;
  298.             case '*': st.push(val2 * val1); break;
  299.             case '/': st.push(val2 / val1); break;
  300.  
  301.             }
  302.         }
  303.     }
  304.     temp = st.top();
  305.     st.pop();
  306.     return temp;
  307.    
  308. }
  309.  
  310. int main(int argc, char*argv[]) {
  311.  
  312.     if (argc < 2) {
  313.         cout << "Usage: evalexpr input=input1.txt output=output1.txt" << std::endl;
  314.         return -1;
  315.     }
  316.  
  317.     ArgumentManager am(argc, argv);
  318.     const string input = am.get("input");
  319.     const string output = am.get("output");
  320.  
  321.     ifstream inputfile(input);
  322.     string line = "";
  323.     ofstream outputfile(output);
  324.  
  325.     Stack Num;
  326.     /*Stack Num2;*/
  327.     bool balanced = true;
  328.     int count = 1;
  329.     string* lines = new string[1000];
  330.  
  331.     while (getline(inputfile, line))
  332.     {
  333.         if (!line.empty())
  334.         {
  335.             /*Num2.push2(line);*/
  336.             lines[count - 1] = line;
  337.             if (Num.Balanced(line) == false)
  338.             {
  339.                 cout << "Error at: " << count << endl;
  340.                 outputfile << "Error at: " << count << endl;
  341.                 balanced = false;
  342.             }
  343.             count++;
  344.  
  345.             while (!Num.isEmpty())
  346.             {
  347.                 Num.pop();
  348.             }
  349.         }
  350.     }
  351.  
  352.    
  353.     if (balanced == true)
  354.     {
  355.         for (int i = 0; i < count-1; i++)
  356.         {
  357.             int j = 0;
  358.             string value = lines[i];
  359.             while (j < value.length())
  360.             {
  361.                 if (value[j] == '-')
  362.                 {
  363.                     if (j == 0 || value[j - 1] == '(' || value[j - 1] == '[' || value[j - 1] == '{' || value[j - 1] == '+' || value[j - 1] == '-' || value[j - 1] == '*' || value[j - 1] == '/' || value[j - 1] == '^')
  364.                     {
  365.                         value.replace(j, 1, "0-");
  366.                     }
  367.                 }
  368.                 else if (value[j] == '+')
  369.                 {
  370.                     if (j == 0 || value[j - 1] == '(' || value[j - 1] == '[' || value[j - 1] == '{' || value[j - 1] == '+' || value[j - 1] == '-' || value[j - 1] == '*' || value[j - 1] == '/' || value[j - 1] == '^')
  371.                     {
  372.                         value.replace(j, 1, "0+");
  373.                         //value.erase(j, 1);
  374.                     }
  375.                 }
  376.                 lines[i] = value;
  377.                 j++;
  378.             }
  379.             cout << lines[i] << endl;
  380.             cout << Num.infixToPostfix(lines[i]) << endl;
  381.  
  382.             string post = Num.infixToPostfix(lines[i]);
  383.             for (int i = 1; i < post.length(); i += 2)
  384.             {
  385.                 post.insert(i, " ", 1);
  386.             }
  387.             cout << post << endl;
  388.  
  389.             for (int i = 0; i < post.length(); i++)
  390.             {
  391.  
  392.                 if (isalpha(post[i]))
  393.                 {
  394.                     if (post[i] == 'a')
  395.                         post.replace(i, 1, "9");
  396.                     else if (post[i] == 'b')
  397.                         post.replace(i, 1, "25");
  398.                     else if (post[i] == 'c')
  399.                         post.replace(i, 1, "49");
  400.                     else if (post[i] == 'd')
  401.                         post.replace(i, 1, "121");
  402.                     else if (post[i] == 'e')
  403.                         post.replace(i, 1, "169");
  404.                     else if (post[i] == 'f')
  405.                         post.replace(i, 1, "289");
  406.                     else if (post[i] == 'g')
  407.                         post.replace(i, 1, "361");
  408.                     else if (post[i] == 'h')
  409.                         post.replace(i, 1, "529");
  410.                     else if (post[i] == 'i')
  411.                         post.replace(i, 1, "841");
  412.                     else if (post[i] == 'j')
  413.                         post.replace(i, 1, "961");
  414.                     else if (post[i] == 'k')
  415.                         post.replace(i, 1, "1369");
  416.                     else if (post[i] == 'l')
  417.                         post.replace(i, 1, "1681");
  418.                     else if (post[i] == 'm')
  419.                         post.replace(i, 1, "1849");
  420.                     else if (post[i] == 'n')
  421.                         post.replace(i, 1, "2209");
  422.                     else if (post[i] == 'o')
  423.                         post.replace(i, 1, "2809");
  424.                     else if (post[i] == 'p')
  425.                         post.replace(i, 1, "3481");
  426.                     else if (post[i] == 'q')
  427.                         post.replace(i, 1, "3721");
  428.                     else if (post[i] == 'r')
  429.                         post.replace(i, 1, "4489");
  430.                     else if (post[i] == 's')
  431.                         post.replace(i, 1, "5041");
  432.                     else if (post[i] == 't')
  433.                         post.replace(i, 1, "5329");
  434.                     else if (post[i] == 'u')
  435.                         post.replace(i, 1, "6241");
  436.                     else if (post[i] == 'v')
  437.                         post.replace(i, 1, "6889");
  438.                     else if (post[i] == 'w')
  439.                         post.replace(i, 1, "7921");
  440.                     else if (post[i] == 'x')
  441.                         post.replace(i, 1, "9409");
  442.                     else if (post[i] == 'y')
  443.                         post.replace(i, 1, "10201");
  444.                     else if (post[i] == 'z')
  445.                         post.replace(i, 1, "10609");
  446.                     else if (post[i] == 'A')
  447.                         post.replace(i, 1, "11449");
  448.                     else if (post[i] == 'B')
  449.                         post.replace(i, 1, "11881");
  450.                     else if (post[i] == 'C')
  451.                         post.replace(i, 1, "12769");
  452.                     else if (post[i] == 'D')
  453.                         post.replace(i, 1, "16129");
  454.                     else if (post[i] == 'E')
  455.                         post.replace(i, 1, "17161");
  456.                     else if (post[i] == 'F')
  457.                         post.replace(i, 1, "18769");
  458.                     else if (post[i] == 'G')
  459.                         post.replace(i, 1, "19321");
  460.                     else if (post[i] == 'H')
  461.                         post.replace(i, 1, "22201");
  462.                     else if (post[i] == 'I')
  463.                         post.replace(i, 1, "22801");
  464.                     else if (post[i] == 'J')
  465.                         post.replace(i, 1, "24649");
  466.                     else if (post[i] == 'K')
  467.                         post.replace(i, 1, "26569");
  468.                     else if (post[i] == 'L')
  469.                         post.replace(i, 1, "27889");
  470.                     else if (post[i] == 'M')
  471.                         post.replace(i, 1, "29929");
  472.                     else if (post[i] == 'N')
  473.                         post.replace(i, 1, "32041");
  474.                     else if (post[i] == 'O')
  475.                         post.replace(i, 1, "32761");
  476.                     else if (post[i] == 'P')
  477.                         post.replace(i, 1, "36481");
  478.                     else if (post[i] == 'Q')
  479.                         post.replace(i, 1, "37249");
  480.                     else if (post[i] == 'R')
  481.                         post.replace(i, 1, "38809");
  482.                     else if (post[i] == 'S')
  483.                         post.replace(i, 1, "39601");
  484.                     else if (post[i] == 'T')
  485.                         post.replace(i, 1, "44521");
  486.                     else if (post[i] == 'U')
  487.                         post.replace(i, 1, "49729");
  488.                     else if (post[i] == 'V')
  489.                         post.replace(i, 1, "51529");
  490.                     else if (post[i] == 'W')
  491.                         post.replace(i, 1, "52441");
  492.                     else if (post[i] == 'X')
  493.                         post.replace(i, 1, "54289");
  494.                     else if (post[i] == 'Y')
  495.                         post.replace(i, 1, "57121");
  496.                     else if (post[i] == 'Z')
  497.                         post.replace(i, 1, "58081");
  498.                     /*int ascii = post[i];
  499.                     auto s = std::to_string(ascii);
  500.                     post.replace(i, s.length(), (s + " "));*/
  501.                 }
  502.             }
  503.             lines[i] = post;
  504.             cout << lines[i] << endl;
  505.             cout << Num.evaluatePostfix(post) << endl;
  506.         }
  507.         bool isEqual = true;
  508.         for (int i = 0; i < count - 2; i++)
  509.         {
  510.             if (Num.evaluatePostfix(lines[i]) != Num.evaluatePostfix(lines[i + 1]))
  511.                 isEqual = false;
  512.         }
  513.         if (isEqual == true)
  514.         {
  515.             cout << "Yes" << endl;
  516.             outputfile << "Yes" << endl;
  517.         }
  518.         else if (isEqual == false)
  519.         {
  520.             cout << "No" << endl;
  521.             outputfile << "No" << endl;
  522.         }
  523.     }
  524.     /*string expr = "{a-[+(b-c-d)]-e}";
  525.     if(Num.Balanced(expr) == true)
  526.     cout << "is balanced" << endl;*/
  527.     //while (top2 != NULL)
  528.     //{
  529.     //  cout << top2->data2 << endl;
  530.     //  top2 = top2->next;
  531.     //}
  532.  
  533.     //
  534.     //string exp = "a+b*(c^d-e)^(f+g*h)-i";
  535.     //Num.infixToPostfix(exp);
  536.     //if (Num.Balanced(exp) == true)
  537.     //  cout << "balanced" << endl;
  538.     //else if(Num.Balanced(exp) == false)
  539.     //  cout << "not balanced" << endl;
  540.  
  541.     system("pause");
  542.     return 0;
  543.  
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement