Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <cmath>
- #include <fstream>
- #include <sstream>
- #include <algorithm>
- #include <stack>
- #include "ArgumentManager.h"
- using namespace std;
- //Abeer Malik 1514719
- struct node
- {
- char data;
- //string data2;
- struct node* next;
- };
- //struct node* top;
- //struct node* top2;
- class Stack {
- private:
- node* top;
- public:
- Stack();
- void push(char x);
- /*void push2(string y);*/
- void pop();
- void Display();
- bool Balanced(string value);
- int prec(char c);
- //void infixToPostfix(string s);
- string infixToPostfix(string s);
- /*int is_operator(char symbol);
- int precedence(char symbol);*/
- int evaluatePostfix(string exp);
- bool isEmpty();
- };
- Stack::Stack()
- {
- top = NULL;
- }
- void Stack::push(char x)
- {
- struct node* temp;
- temp = (struct node*)malloc(sizeof(struct node));
- if (temp == NULL)
- cout << "stack is full" << endl;
- else
- {
- temp->data = x;
- temp->next = top;
- top = temp;
- }
- }
- //void Stack::push2(string y)
- //{
- // struct node* temp2;
- // temp2 = (struct node*)malloc(sizeof(struct node));
- //
- // temp2->data2 = y;
- // temp2->next = top2;
- // top2 = temp2;
- //
- //}
- void Stack::pop()
- {
- struct node* temp;
- if (top == NULL)
- {
- cout << "stack is empty" << endl;
- }
- else
- {
- temp = top;
- top = top->next;
- temp->next = NULL;
- free(temp);
- }
- }
- void Stack::Display()
- {
- struct node* temp;
- if (top == NULL)
- {
- cout << "stack is empty" << endl;
- }
- else
- {
- temp = top;
- while (temp != NULL)
- {
- cout << temp->data << endl;
- temp = temp->next;
- }
- }
- }
- bool Stack::Balanced(string value)
- {
- int i = 0;
- while(value[i])
- {
- if (value[i] == '(')
- {
- push(value[i]);
- }
- else if (value[i] == '[')
- {
- push(value[i]);
- }
- else if (value[i] == '{')
- {
- push(value[i]);
- }
- else if (value[i] == ')')
- {
- if (top == NULL || top->data != '(')
- return false;
- pop();
- }
- else if (value[i] == ']')
- {
- if (top == NULL || top->data != '[')
- return false;
- pop();
- }
- else if (value[i] == '}')
- {
- if (top == NULL || top->data != '{')
- return false;
- pop();
- }
- i++;
- }
- if (top == NULL)
- return true;
- else
- return false;
- }
- bool Stack::isEmpty()
- {
- if (top == NULL)
- return true;
- return false;
- }
- int Stack::prec(char c)
- {
- if (c == '^')
- return 3;
- else if (c == '*' || c == '/')
- return 2;
- else if (c == '+' || c == '-')
- return 1;
- else
- return -1;
- }
- string Stack::infixToPostfix(string s)
- {
- stack<char> st;
- st.push('N');
- int l = s.length();
- string ns;
- for (int i = 0; i < l; i++)
- {
- // If the scanned character is an operand, add it to output string.
- if (isalpha(s[i]) || isdigit(s[i]))
- ns += s[i];
- // If the scanned character is an ‘(‘, push it to the stack.
- else if (s[i] == '(')
- st.push('(');
- else if (s[i] == '[')
- st.push('[');
- else if (s[i] == '{')
- st.push('{');
- // If the scanned character is an ‘)’, pop and to output string from the stack
- // until an ‘(‘ is encountered.
- else if (s[i] == ')')
- {
- while (st.top() != 'N' && st.top() != '(')
- {
- char c = st.top();
- st.pop();
- ns += c;
- }
- if (st.top() == '(')
- {
- char c = st.top();
- st.pop();
- }
- }
- else if (s[i] == ']')
- {
- while (st.top() != 'N' && st.top() != '[')
- {
- char c = st.top();
- st.pop();
- ns += c;
- }
- if (st.top() == '[')
- {
- char c = st.top();
- st.pop();
- }
- }
- else if (s[i] == '}')
- {
- while (st.top() != 'N' && st.top() != '{')
- {
- char c = st.top();
- st.pop();
- ns += c;
- }
- if (st.top() == '{')
- {
- char c = st.top();
- st.pop();
- }
- }
- //If an operator is scanned
- else {
- while (st.top() != 'N' && prec(s[i]) <= prec(st.top()))
- {
- char c = st.top();
- st.pop();
- ns += c;
- }
- st.push(s[i]);
- }
- }
- //Pop all the remaining elements from the stack
- while (st.top() != 'N')
- {
- char c = st.top();
- st.pop();
- ns += c;
- }
- st.pop();
- return ns;
- }
- int Stack::evaluatePostfix(string exp)
- {
- stack<int> st;
- int temp;
- // Create a stack of capacity equal to expression size
- int i;
- // See if stack was created successfully
- // Scan all characters one by one
- for (i = 0; exp[i]; ++i)
- {
- //if the character is blank space then continue
- if (exp[i] == ' ')continue;
- // If the scanned character is an
- // operand (number here),extract the full number
- // Push it to the stack.
- else if (isdigit(exp[i]))
- {
- int num = 0;
- //extract full number
- while (isdigit(exp[i]))
- {
- num = num * 10 + (int)(exp[i] - '0');
- i++;
- }
- i--;
- //push the element in the stack
- st.push(num);
- }
- // If the scanned character is an operator, pop two
- // elements from stack apply the operator
- else
- {
- int val1 = st.top();
- st.pop();
- int val2 = st.top();
- st.pop();
- switch (exp[i])
- {
- case '+': st.push(val2 + val1); break;
- case '-': st.push(val2 - val1); break;
- case '*': st.push(val2 * val1); break;
- case '/': st.push(val2 / val1); break;
- }
- }
- }
- temp = st.top();
- st.pop();
- return temp;
- }
- int main(int argc, char*argv[]) {
- if (argc < 2) {
- cout << "Usage: evalexpr input=input1.txt output=output1.txt" << std::endl;
- return -1;
- }
- ArgumentManager am(argc, argv);
- const string input = am.get("input");
- const string output = am.get("output");
- ifstream inputfile(input);
- string line = "";
- ofstream outputfile(output);
- Stack Num;
- /*Stack Num2;*/
- bool balanced = true;
- int count = 1;
- string* lines = new string[1000];
- while (getline(inputfile, line))
- {
- if (!line.empty())
- {
- /*Num2.push2(line);*/
- lines[count - 1] = line;
- if (Num.Balanced(line) == false)
- {
- cout << "Error at: " << count << endl;
- outputfile << "Error at: " << count << endl;
- balanced = false;
- }
- count++;
- while (!Num.isEmpty())
- {
- Num.pop();
- }
- }
- }
- if (balanced == true)
- {
- for (int i = 0; i < count-1; i++)
- {
- int j = 0;
- string value = lines[i];
- while (j < value.length())
- {
- if (value[j] == '-')
- {
- 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] == '^')
- {
- value.replace(j, 1, "0-");
- }
- }
- else if (value[j] == '+')
- {
- 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] == '^')
- {
- value.replace(j, 1, "0+");
- //value.erase(j, 1);
- }
- }
- lines[i] = value;
- j++;
- }
- cout << lines[i] << endl;
- cout << Num.infixToPostfix(lines[i]) << endl;
- string post = Num.infixToPostfix(lines[i]);
- for (int i = 1; i < post.length(); i += 2)
- {
- post.insert(i, " ", 1);
- }
- cout << post << endl;
- for (int i = 0; i < post.length(); i++)
- {
- if (isalpha(post[i]))
- {
- if (post[i] == 'a')
- post.replace(i, 1, "9");
- else if (post[i] == 'b')
- post.replace(i, 1, "25");
- else if (post[i] == 'c')
- post.replace(i, 1, "49");
- else if (post[i] == 'd')
- post.replace(i, 1, "121");
- else if (post[i] == 'e')
- post.replace(i, 1, "169");
- else if (post[i] == 'f')
- post.replace(i, 1, "289");
- else if (post[i] == 'g')
- post.replace(i, 1, "361");
- else if (post[i] == 'h')
- post.replace(i, 1, "529");
- else if (post[i] == 'i')
- post.replace(i, 1, "841");
- else if (post[i] == 'j')
- post.replace(i, 1, "961");
- else if (post[i] == 'k')
- post.replace(i, 1, "1369");
- else if (post[i] == 'l')
- post.replace(i, 1, "1681");
- else if (post[i] == 'm')
- post.replace(i, 1, "1849");
- else if (post[i] == 'n')
- post.replace(i, 1, "2209");
- else if (post[i] == 'o')
- post.replace(i, 1, "2809");
- else if (post[i] == 'p')
- post.replace(i, 1, "3481");
- else if (post[i] == 'q')
- post.replace(i, 1, "3721");
- else if (post[i] == 'r')
- post.replace(i, 1, "4489");
- else if (post[i] == 's')
- post.replace(i, 1, "5041");
- else if (post[i] == 't')
- post.replace(i, 1, "5329");
- else if (post[i] == 'u')
- post.replace(i, 1, "6241");
- else if (post[i] == 'v')
- post.replace(i, 1, "6889");
- else if (post[i] == 'w')
- post.replace(i, 1, "7921");
- else if (post[i] == 'x')
- post.replace(i, 1, "9409");
- else if (post[i] == 'y')
- post.replace(i, 1, "10201");
- else if (post[i] == 'z')
- post.replace(i, 1, "10609");
- else if (post[i] == 'A')
- post.replace(i, 1, "11449");
- else if (post[i] == 'B')
- post.replace(i, 1, "11881");
- else if (post[i] == 'C')
- post.replace(i, 1, "12769");
- else if (post[i] == 'D')
- post.replace(i, 1, "16129");
- else if (post[i] == 'E')
- post.replace(i, 1, "17161");
- else if (post[i] == 'F')
- post.replace(i, 1, "18769");
- else if (post[i] == 'G')
- post.replace(i, 1, "19321");
- else if (post[i] == 'H')
- post.replace(i, 1, "22201");
- else if (post[i] == 'I')
- post.replace(i, 1, "22801");
- else if (post[i] == 'J')
- post.replace(i, 1, "24649");
- else if (post[i] == 'K')
- post.replace(i, 1, "26569");
- else if (post[i] == 'L')
- post.replace(i, 1, "27889");
- else if (post[i] == 'M')
- post.replace(i, 1, "29929");
- else if (post[i] == 'N')
- post.replace(i, 1, "32041");
- else if (post[i] == 'O')
- post.replace(i, 1, "32761");
- else if (post[i] == 'P')
- post.replace(i, 1, "36481");
- else if (post[i] == 'Q')
- post.replace(i, 1, "37249");
- else if (post[i] == 'R')
- post.replace(i, 1, "38809");
- else if (post[i] == 'S')
- post.replace(i, 1, "39601");
- else if (post[i] == 'T')
- post.replace(i, 1, "44521");
- else if (post[i] == 'U')
- post.replace(i, 1, "49729");
- else if (post[i] == 'V')
- post.replace(i, 1, "51529");
- else if (post[i] == 'W')
- post.replace(i, 1, "52441");
- else if (post[i] == 'X')
- post.replace(i, 1, "54289");
- else if (post[i] == 'Y')
- post.replace(i, 1, "57121");
- else if (post[i] == 'Z')
- post.replace(i, 1, "58081");
- /*int ascii = post[i];
- auto s = std::to_string(ascii);
- post.replace(i, s.length(), (s + " "));*/
- }
- }
- lines[i] = post;
- cout << lines[i] << endl;
- cout << Num.evaluatePostfix(post) << endl;
- }
- bool isEqual = true;
- for (int i = 0; i < count - 2; i++)
- {
- if (Num.evaluatePostfix(lines[i]) != Num.evaluatePostfix(lines[i + 1]))
- isEqual = false;
- }
- if (isEqual == true)
- {
- cout << "Yes" << endl;
- outputfile << "Yes" << endl;
- }
- else if (isEqual == false)
- {
- cout << "No" << endl;
- outputfile << "No" << endl;
- }
- }
- /*string expr = "{a-[+(b-c-d)]-e}";
- if(Num.Balanced(expr) == true)
- cout << "is balanced" << endl;*/
- //while (top2 != NULL)
- //{
- // cout << top2->data2 << endl;
- // top2 = top2->next;
- //}
- //
- //string exp = "a+b*(c^d-e)^(f+g*h)-i";
- //Num.infixToPostfix(exp);
- //if (Num.Balanced(exp) == true)
- // cout << "balanced" << endl;
- //else if(Num.Balanced(exp) == false)
- // cout << "not balanced" << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement