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!
#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;
}