Infix to Postfix reading from file stuck in infinite loop c++ -
this program reads input file, , input file changes each incoming infix line postfix notation. seems working except nested while loop in main() deals operation symbols (* / + -). when commented out output lot better. unsure of why entering infinite loop.
i left of pseudo-code on main reader can see line of thought, there better way execute program.
tests.txt contains:
4 5+7 7*5 (5-3) 5/5 8*5+3 8*(5+3) 8+3*5-7 (8+3)*(5-6) ((8+3)*(2-7)) ((8+3)*2)-7 (8*5)+((3-2)-7*3) ((8*5+3)-7)-(5*3) 7*9+7-5*6+3-4
node.h
#pragma once template <class t> struct node { // data members t data; node<t>* next; // constructor node(t d); };// end node struct template<class t> node<t>::node(t d) { data = d; next = nullptr; }
stack.h
#pragma once #include "node.h" template <class t> class stack { private: node<t>* head; public: // constructor stack(); // destructor ~stack(); // push void push(t); // pop void pop(); // top t top(); // empty bool empty(); };// end stack template template<class t> stack<t>::stack() { head = nullptr; } template<class t> stack<t>::~stack() { // while stack not empty if (empty() != true) { // create temporary node node<t>* temp; // set temp next node temp = head->next; // delete first node delete head; // set first node next node head = temp; } } template<class t> void stack<t>::push(t value) { // make new node value node<t>* incomingtop = new node<t>(value); // set pointer current top incomingtop->next = head; // set top new value head = incomingtop; } template <class t> void stack<t>::pop() { // if stack not empty if (empty() != true) { // set new pointer top node<t>* oldtop = head; // set top next head = head->next; // delete oldtop delete oldtop; } } template <class t> t stack<t>::top() { // if stack not empty if (empty() != true) { // return data in top return head->data; } } template<class t> bool stack<t>::empty() { // if top of stack null if (head == nullptr) { // stack empty // return true return true; } else // otherwise { // stack has data // return false return false; } }
calc.cpp
// include necessary files #include "stack.h" #include <iostream> #include <fstream> #include <string> #include <assert.h> using namespace std; typedef char valuetype; /* precedence function purpose: determines if passed in char of higher precedence */ int precedence(char); // main int main() { cout << "postfix expressions translated ''tests.txt''"; stack<valuetype> storage; ifstream input; input.open("tests.txt"); if (input.fail()) { cout << "could not open input file." << endl; } // while not @ end of file while (input && input.peek() != eof) { // read , store charachter input file char testvalue = input.get(); // if value newline charachter if (testvalue == '\n') { // create newline on console cout << endl; // empty stack while (storage.empty() == false) { // display top of stack cout << storage.top(); // pop storage.pop(); } }// end if newline charachter // if value left paren else if (testvalue == '(') { // store on stack storage.push(testvalue); } // if value operation symbol else if (testvalue == '+' || testvalue == '-' || testvalue == '*' || testvalue == '/') { /* while: stack not empty or next symbol on stack not left paren or next symbol not of higher precedence */ while (storage.empty() != true || storage.top() != '(' || precedence(testvalue) >= precedence(storage.top())) { // display test value cout << testvalue; // make test value top of stack testvalue = storage.top(); // pop top of stack storage.pop(); }// end nested while loop /* if above while loop exited push test value onto stack */ storage.push(testvalue); }// end else if operation symbols // if value right paren else if (testvalue == ')') { // while top not left paren while (storage.top() != '(') { // if no left paren encountered halt program assert("there unblanced parentheses. halting program.", storage.empty == true); // print top operation cout << storage.top(); // pop top value storage.pop(); }// end nested while loop // pop left paren storage.pop(); }// end else if right paren else // otherwise if value operand (number or variable) { // write out console cout << testvalue; } }// end outer while loop input.close(); cout << endl; system("pause"); return 0; }// end main int precedence(char c) { if (c == '*' || c == '/') { return 2; } if (c == '+' || c == '-') { return 1; } else { return 0; } }
Comments
Post a Comment