Polish notation C++ code

I saw many people have problem with Polish notation code, so I decide to share my "Polish notation Code" (write in C++). Hope this would help someone. This is just an example as a reference, don't take it as a complete program!!!

Math.txt -- Put something like: 5+6-7*3+5*6-7*9-3
MathOutput.txt -- Leave empty and your computer will fill with: 5673*-56*79*-3-++ (if you use Output function in void main())

here is PolishNotation.cpp
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
 //#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "string"
#include "Stack.h"
#include "stack"
using namespace std;

#define FI "Math.txt"
#define FO "MathOutput.txt"

ifstream fi;
ofstream fo;

void Input(char A[])
{
    fi.open(FI);
    fi>>A;
    fi.close();
}
void Output(char B[])
{
    fo.open(FO);
    fo<<B;
    fo.close();
}

int calculation(int oper1, int oper2, char chr)
{
    switch (chr)
    {
        case '+':
            return oper1 + oper2;
        case '-':
            return oper1 - oper2;
        case '*':
            return oper1 * oper2;
        case '/':
            return oper1 / oper2;
        default: cerr << "undefined behavor\n";
                         break;
    }
}
void Trans(char A[],char B[])
{
    int i=0,j=0;
    Stack<char> S;
    while(A[i] != 0)
    {
        if(A[i] >=48 && A[i] <=57)
        {
            B[j] = A[i];
            j++;
        }
        if(A[i] == '*' || A[i] == '/')
        {
            S.push(A[i]);
        }
        if(A[i] == '+' || A[i] == '-')
        {
            if(S.IsEmpty())
                S.push(A[i]);
            else
                if(S.top() == '*' || S.top() == '/' || S.top() == '-')
                {
                    while(S.top() == '*' || S.top() == '/' || S.top() == '-')
                    {
                    B[j] = S.pop();
                    j++;                   
                    }
                    S.push(A[i]);
                }
                else
                {
                    S.push(A[i]);
                }
        }
        i++;
    }
    while(!S.IsEmpty())
    {
        B[j] = S.pop();
        j++;
    }
}
int ReadMath(char A[])
{
    int i=0,n1,n2;
    Stack<int> Num;
    while(A[i] != 0)
    {
        if(A[i] >=48 && A[i] <=57)
        {
            Num.push(A[i]-48);
        }
        if(A[i] == '*' || A[i] == '/' || A[i] == '+' || A[i] == '-')
        {
            n2=Num.pop();
            n1=Num.pop();
            Num.push( calculation(n1,n2,A[i]));
        }
        i++;
    }
    return Num.top();
}

void main()
{
    char M[30]={NULL};
    char N[30]={NULL};
    Input(M);
   
    Trans(M,N);
    //Output(N);
    cout<<N<<endl;
    cout<<M<<"= "<<ReadMath(N)<<endl;
}

here is Stack.h (you can write this yourself)
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
144
#include "iostream"
#include "fstream"

using namespace std;

template<class T>
struct Node
{
    T Data;
    Node* pNext;
};
template<class T>
class Stack
{
protected:
    Node<T> *pHead;
    Node<T> *pTail;
    Node<T>* GetNODE(T x);
    void AddTail(Node<T>* x);
    void AddFirst(Node<T>* x);
    T RemoveHead();
public:
    Stack();
    bool IsEmpty();
    void push(T x);
    T pop();
    T top();
    ~Stack();
};

template<class T>
Stack<T>::Stack()
{
    this->pHead = NULL;
    this->pTail = NULL;
}

template<class T>
bool Stack<T>::IsEmpty()
{
    if(this->pHead == NULL)
        return true;
    else
        return false;
}

template<class T>
Node<T>* Stack<T>::GetNODE(T x)
{
    Node<T> *p;
    p = new Node<T>;
    if(p==NULL)
    {
        cout<<"\nMemory overload\n";
        return NULL;
    }
    p->Data = x;
    p->pNext = NULL;
    return p;
}
template<class T>
void Stack<T>::AddTail(Node<T>* x)
{
    if(this->pHead == NULL)
    {
        this->pHead = x;
        this->pTail = this->pHead;
    }
    else
    {
        this->pTail->pNext = x;
        this->pTail = x;
    }
}
template<class T>
void Stack<T>::AddFirst(Node<T>* x)
{
    if(this->pHead == NULL)
    {
        this->pHead = x;
        this->pTail = this->pHead;
    }
    else
    {
        x->pNext = this->pHead;
        this->pHead = x;
    }
}

template<class T>
void Stack<T>::push(T x)
{
    Stack<T>::AddFirst(GetNODE(x));
}

template<class T>
T Stack<T>::RemoveHead()
{
    Node<T>* p;
    T x;
    if(this->pHead != NULL)
    {
        p = this->pHead;
        x = p->Data;
        this->pHead = this->pHead->pNext;
        delete p;
        if(this->pHead == NULL)
            this->pTail = NULL;
        return x;
    }
    return NULL;
}

template<class T>
T Stack<T>::pop()
{
    T x;
    if(Stack<T>::IsEmpty())
        return NULL;
    x = Stack<T>::RemoveHead();
    return x;
}

template<class T>
T Stack<T>::top()
{
    if(Stack<T>::IsEmpty())
        return NULL;
    return this->pHead->Data;
}

template<class T>
Stack<T>::~Stack()
{
    Node<T> *p;
    while(this->pHead != NULL)
    {
        p = this->pHead;
        this->pHead = this->pHead->pNext;
        delete p;
    }
    this->pTail = NULL;
    //cout<<endl<<"            \1 \3 \2 HAVE A NICE DAY \1 \3 \2"<<endl;
}
Topic archived. No new replies allowed.