| | |
stack/queue program for long math operation - need help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 17
Reputation:
Solved Threads: 0
Hi all,
The program I'm making needs to basically interpret long math operations (see the attached text file) and compute them. Simple right?
I have to use stacks and queues to reorder the operation into the desired output (the output will compile properly if you run the program as it is now). I'm obviously trying to compute a final answer but the final answer is not correct, I'm thinking I'm having issues converting my char variables into float variables. For better convenience my problem I'm sure lies in my EVALUATE_POSTFIX function.
A side note: I have to read in multiple lines of math operations from the text file ultimately, but for now I'm only having the program interpret one line, just for better troubleshooting.
The purpose of all the reordering is for faster computation - as instructed from my college class.
Thank you for your time!
The program I'm making needs to basically interpret long math operations (see the attached text file) and compute them. Simple right?
I have to use stacks and queues to reorder the operation into the desired output (the output will compile properly if you run the program as it is now). I'm obviously trying to compute a final answer but the final answer is not correct, I'm thinking I'm having issues converting my char variables into float variables. For better convenience my problem I'm sure lies in my EVALUATE_POSTFIX function.
A side note: I have to read in multiple lines of math operations from the text file ultimately, but for now I'm only having the program interpret one line, just for better troubleshooting.
The purpose of all the reordering is for faster computation - as instructed from my college class.
Thank you for your time!
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> #include <stack> #include <string> #include <queue> #include <stdio.h> #include <ctype.h> #include <cstring> using namespace std; queue<char> INFIX_TO_POSTFIX(ifstream &); void EVALUATE_POSTFIX(queue<char> &); int precedence(char); int main() { ifstream inFile; inFile.open("a2.txt"); //attempts to open read file, and tests for existance if(inFile.is_open()) cout << "File Exists." << endl << endl; else cout << "File does not exist." << endl << endl; queue<char> bigqueue; //while(!inFile.eof()) // { bigqueue = INFIX_TO_POSTFIX(inFile); //Function call // } EVALUATE_POSTFIX(bigqueue); // float tool = 7 + (4 - 6) * (8 + 6) / 3; // cout << endl << tool << endl; system("PAUSE"); return EXIT_SUCCESS; } queue<char> INFIX_TO_POSTFIX(ifstream &Filein) //function, Filein is just the name of my text file read stream { stack<char> thestack; queue<char> thequeue; string data; //string created to store the input read line getline(Filein, data); //program reads file, stores read line as the string, data cout << "The original infix expression is: " << data << endl; //output read string char dataarray[25] = {0}; //char array created int arrayLength = data.length(); //set a counter char temp; //temporary variable for(int i = 0; i < arrayLength; i++) //for loop set up to step through the character array, one character at a time { dataarray[i] = data[i]; //Here's the key line, I equate each position in the string to equate to the character array if(isdigit(dataarray[i])) //Here on out is where the processing of the character array begins.... thequeue.push (dataarray[i]);//output content(Next) to Queue; else if(dataarray[i] == '(') thestack.push (dataarray[i]); else if(dataarray[i] == ')') { if(thestack.empty() == false) { while(thestack.top() != '(') { temp = thestack.top(); thequeue.push(temp); thestack.pop(); if(thestack.empty() == true) { break; } //POP the content in Stack to Queue until “(“ is reached, } } thestack.pop(); //POP it but not to Queue; break; } else if(dataarray[i] == '+' || '-' || '*' || '/' || '^') { if(thestack.empty() == false) { while(precedence(thestack.top()) >= precedence(dataarray[i])) //while(PRE(TOP) >= PRE(Next)) { temp = thestack.top(); thequeue.push(temp); thestack.pop(); if(thestack.empty() == true) { break; } } } thestack.push(dataarray[i]); } else cout << "There was an error in the operation, please restart." << endl; } while(!thestack.empty()) //while(PRE(TOP) >= PRE(Next)) { temp = thestack.top(); thequeue.push(temp); thestack.pop(); } return(thequeue); } int precedence(char thing) { int compare; switch(thing) { case '^': compare = 3; break; case '/': case '*': compare = 2; break; case '+': case '-': compare = 1; break; case '(': case ')': compare = 0; break; } return (compare); } void EVALUATE_POSTFIX(queue<char> &postqueue) { stack<float> poststack; int queueLength; float final; char var, temp, temp1, temp2; queueLength = postqueue.size(); for(int j = 0; j < queueLength; j++) { cout << postqueue.front(); if(isdigit(postqueue.front())) { var = postqueue.front(); poststack.push(var); } else if(postqueue.front() == '+' || '-' || '*' || '/' || '^') { temp2 = poststack.top(); poststack.pop(); temp1 = poststack.top(); poststack.pop(); switch(postqueue.front()) { case '+': temp = temp1 + temp2; break; case '-': temp = temp1 - temp2; break; case '*': temp = temp1 * temp2; break; case '/': temp = temp1 / temp2; break; case '^': temp = temp1 ^ temp2; break; default: cout << "There was a glitch in the operation, press any key to terminate."; } poststack.push(temp); } postqueue.pop(); } final = poststack.top(); cout << endl << "The postfix operation answer is: " << poststack.top() << endl; return; }
•
•
Join Date: Jan 2009
Posts: 17
Reputation:
Solved Threads: 0
Ok, I've figured out that my variables are equating to ASCII equivalents, I'm having trouble converting them, I don't understand why they're not converting when I convert them...
C++ Syntax (Toggle Plain Text)
void EVALUATE_POSTFIX(queue<char> &postqueue) { stack<float> poststack; int queueLength; char var; float var1, temp, temp1, temp2, final; queueLength = postqueue.size(); for(int j = 0; j < queueLength; j++) { cout << postqueue.front(); if(isdigit(postqueue.front())) { var = postqueue.front(); var1 = static_cast<float>(var); poststack.push(var1); } else if(postqueue.front() == '+' || '-' || '*' || '/' || '^') { temp2 = poststack.top(); poststack.pop(); temp1 = poststack.top(); poststack.pop(); switch(postqueue.front()) { case '+': temp = temp1 + temp2; break; case '-': temp = temp1 - temp2; break; case '*': temp = temp1 * temp2; break; case '/': temp = temp1 / temp2; break; case '^': temp = pow(temp1, temp2); break; default: cout << "There was a glitch in the operation, press any key to terminate."; } poststack.push(temp); } postqueue.pop(); } final = poststack.top(); cout << endl << "The postfix operation answer is: " << final << endl; return; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: problem with strtok
- Next Thread: Pointer Syntax
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





