Image

Imagebabygirlbeth13 wrote in Imagecpp

operands

While using stacks and queues, how would I go about using an if statement that is asking whether or not that first object in a line (in a queue) is an operand? What do I use to define an operand? In my pseudo code I have just written:

if operand
  move to q2
else
 *a bunch of other rules*

So I'm not sure how to translate the operand into syntex.



Edit (Jan. 30th, 11:02pm):
I finally got it compiling and running; It is "sort of" doing what it is suppose to, just not correctly. It's not producing the correctly ordered postfix, but it does recieve and print out something, so yay for now:



#include <iostream> #include <fstream> #include <string> #include <cmath> #include <iomanip> #include <stack> #include <queue> #include <cctype> using namespace std; int operatorPrecedence(char ch); int main() { ifstream ifp; string fileRead; stack<char> aStack; queue<char> q1; queue<char> q2; cout << "Name of file to be read." << endl; cin >> fileRead; ifp.open(fileRead.c_str()); if (!ifp.fail()) { while(!ifp.eof()) { char ch; ifp.get(ch); while(ch != '\n') { cout << "ch: " << ch << endl; q1.push(ch); ifp.get(ch); } while(!q1.empty()) { if (isalpha(q1.front())) { q2.push(q1.front()); q1.pop(); } else if (aStack.empty()) { aStack.push(q1.front()); q1.pop(); } else if (operatorPrecedence(ch) == 1) { q2.push(q1.front()); q1.pop(); } else if (operatorPrecedence(ch) == 2) { aStack.push(q1.front()); q1.pop(); } else { q2.push(q1.front()); q1.pop(); } } while(!aStack.empty()) { q2.push(aStack.top()); aStack.pop(); } cout << "The post fix answer is: "; while(!q2.empty()) { cout << q2.front(); q2.pop(); } cout << endl; } ifp.close(); } else cout << "Error opening file." << endl; return 0; } int operatorPrecedence(char ch) { switch (ch) { case '*': return 2; case '/': return 2; case '+': return 1; case '-': return 1; } return 0; }