A way to make infix > postfix with more than 1 char/digit

So I implemented a stack, that should turn an infix to postfix but sadly, I can't get it to work for more than 1 digit/character, for example a or 1 works but 12 for example doesn't its treated as 1 2 instead, is there a way I can modify my code to do so?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <bits/stdc++.h>


#define fl(i,n)    for(int i = 0; i < n; i++)

#define ll   long long
#define nl   endl
#define pb   push_back
#define mp   make_pair
#define PII  pair<int,int>

#define EPS  1e-9
#define INF  1e9


using namespace std;


class Stack{
private:
    char data;
    Stack *next;
public:
    void push(Stack *&top, char x){
        Stack *temp = new Stack;
        temp->data = x;
        temp->next = top;
        top = temp;
    }

    void pop(Stack *&top){
        if(top == nullptr) return;
        Stack *temp = top;
        temp = top->next;
        delete top;
        top = temp;
    }
    void Print(Stack *top)
    {
        Stack* temp = top;
        while(temp!=nullptr)
        {
            cout << temp->data<<" ";
            temp = temp->next;
        }
        cout << nl;
    }

    bool isEmpty(Stack *top){
        if(top == nullptr) return true;
        return false;
    }

    int top_element(Stack *top){
        if(isEmpty(top)) return -1;
        return (top->data);
    }
    void free_stack(Stack *&top){
        if(top == nullptr) return;
        free_stack(top->next);
        pop(top);
    }

};




bool isOperator(char x){
return (x == '+' || x == '-' || x == '*' || x == '/' || x == '^');
}
bool isOperand(char x){
return ( (x >= '0' && x <= '9') || (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') );
}

int weight(char x){
int wei;
switch(x){
case '*':
case '/':
      wei = 2;
case '+':
case '-':
      wei = 1;
case '^':
      wei = 3;
}
return wei;
}

bool Higher(char x, char y){
int weight1 = weight(x);
int weight2 = weight(y);

if(weight1 == weight2){
    if(x == '^') return false;
    else return true;
}
return (weight1 > weight2? true : false);
}


int main()
{
    string s;
    getline(cin, s);
    string res = "";
    Stack *top = nullptr;
    Stack operators;
    for(unsigned int i = 0; i < s.length(); i++){
        if(s[i] == '.' || s[i] == ' ' || s[i] == ',') continue;

        if(isOperand(s[i])){
            res += s[i] ;
        }
        else if(isOperator(s[i])){
            while(!operators.isEmpty(top) && Higher(operators.top_element(top),s[i]) && operators.top_element(top) != '('){
                res += operators.top_element(top);
                operators.pop(top);
            }
            operators.push(top,s[i]);
        }
        else if(s[i] == '('){
            operators.push(top,s[i]);
        }
        else if(s[i] == ')'){
            while(!operators.isEmpty(top) && operators.top_element(top) != '('){
                    res += operators.top_element(top);
                    operators.pop(top);
                  }
                  operators.pop(top);
        }
    }
    while(operators.isEmpty(top) != 1){
        res += operators.top_element(top);
        operators.pop(top);
    }

    cout << res;
    

    return 0;
}


I thought of a way to ask the user how many variables will he use then let them input the equation in the form of characters so if he wants to have 12 or 123 as a number it'll be stored as character literal 'a' for example and later one when the equation is out I just change it back to infix then calculate the operands using the operators in between them then at the time it'll be that a = 12 or a = 123 so it'll be evaluated that way instead? I hope someone has a better idea
You have a stack of char's that's why you only process a char at a time. You should process a string of characters at a time, rather than just one. For example 100+1 yields three tokens, "100", "+" and "1".

This might help: http://courses.cse.tamu.edu/jmichael/sp15/121/slide/CSCE121_09-calc.pptx
Last edited on
@kbw Ah I'll try that, thanks!
Topic archived. No new replies allowed.