//Am I on the right track?
#include <iostream>
#include <fstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <queue>
using namespace std;
int GetOperatorWeight(char op)//detects precedence of operator.
{
int weight = -1;
switch(op)
{
case '(':
case '=':
weight = 0;
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
}
return weight;
}
int main ()
{
stack <int> num; //used to sort through infix expression.
queue <int> nums2; //going to store all final values in queue to be displayed.
string number;
int x = 0;
int y = 0;
ifstream myfile;
myfile.open ("input.txt");
getline (myfile,number);
for(unsigned int i = 0; i<number.length(); i++) {//detects if item is digit.
if (isdigit(number[i]) )//if item is digit, push onto queue.
{
nums2.push(number[i]);
}
else if (isalnum (number[i]) == 0)//detects if item is operand.
{
x = GetOperatorWeight(number[i]);//gets weight of operand being input.
y = GetOperatorWeight(num.top());//gets weight of operand on stack.
// if/else statements put here to organize operands.
You need break; statements after each case in GetOperatorWeight. By default, control just flows from case down to the next otherwise.
You can read a number or an operator from a stream by reading one character (after skipping whitespace). If it's a digit then put it back in the stream and scan the number. Otherwise the character is the operator:
1 2 3 4 5 6 7 8 9
char op, ch;
int num;
istr >> wc >> ch;
if (isdigit(ch)) {
istr.unget(ch);
istr >> num;
} else {
op = ch;
}
You will also want to set a flag of some sort to say whether you read an operator or a number. Work on this code, called parsing, until your program can read an infix expression. Then you can concentrate on what to do with the input.
The only tricky part is cin >> ws. This extracts and discards white space. It's necessary because otherwise cin >> op would read space characters, newlines, tabs etc.
#include <iostream>
#include <fstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <queue>
usingnamespace std;
int GetOperatorWeight(char op)//detects precedence of operator.
{
int weight = -1;
switch(op)
{
case'(':
weight = 3;
break;
case'+':
case'-':
weight = 1;
break;
case'*':
case'/':
weight = 2;
break;
case')':
weight = 0;
break;
}
return weight;
}
int main ()
{
int temp1 = 0;
int temp2 = 0;
int counter = 0;
stack <int> num; //used to sort through infix expression.
queue <int> display; //going to store all final values in queue to be displayed.
string number;
int x = 0;
int y = 0;
ifstream myfile;
myfile.open ("input.txt");
getline (myfile,number);
for(unsignedint i = 0; i<number.length(); i++) {//detects if item is digit.
if (isdigit(number[i]) )//if item is digit, push onto queue.
{
display.push(number[i]);
}
elseif (isalnum (number[i]) == 0)//detects if item is operand.
{
x = GetOperatorWeight(number[i]);//gets weight of operand being input.
y = GetOperatorWeight(num.top());//gets weight of operand on stack.
if (x > y) // if operand being input has greater precedence.
{
num.push(number[i]);//pushes operand onto stack.
counter++;
}
elseif ((x < y) && (x != 0)) //if operand being input has lower precedence and is not a parenthesis.
{
while (counter != 0) //while loop pushes values into display queue, and pops values off of stack.
{
display.push(num.top());
num.pop();
counter--;
}
}
elseif ((x = 3))//if operand being input is a open parenthesis.
{
num.push(number[i]);//push operand onto stack.
temp1 = i; //store place of open parenthesis.
}
elseif (x = 0)//if operand being input is a close parenthesis.
{
temp2 = i; //store place of closed parenthesis.
while(temp1 != temp2)//while loop pops everything between parenthesis off stack and into display queue.
{
display.push(num.top());
num.pop();
temp1++;
}
}
}
}
myfile.close();
return 0;
}
1. On lines 84 and 90 you're doing assignment (=), not comparison (==).
2. What is the 'counter' variable used for? It seems to be loosely correlated to num.size().
3. In several places you call num.top() and num.pop(), but these functions can only be called if num.size() > 0, yet I see nothing checking for this.
4. Please try to choose more meaningful names that 'temp1' and 'temp2'.