language: C++ 4.7.2 (gcc-4.7.2)
date: 170 days 10 hours ago
link:
visibility: private
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//*****************************************************************************
// Postfix expression evaluation
// 11/13/2012
// by DJH
//
// all applicable copyrights apply
//
// this program evaluates an input postfix string
// 
//
// created using Dev-C++ 5.2.0.3
//*****************************************************************************
 
// ----------------------------------libraries---------------------------------
 
#include <iostream>                     // For cin, cout and endl
#include <string>
#include <cctype>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
 
using namespace std;
// ------------------------------ Globals -------------------------------------
string postfix;
 
// --------------------------- stack class ------------------------------------
class Stack{
public:
  enum {MaxStack = 50};
  void init() {top = -1;}
  void push( double p ){
    if ( isFull() ) {
      cerr << "Full Stack. DON'T PUSH\n";
      return;
    }
    else {
      arr[ ++top ] = p;
      cout << "Just pushed " << p << endl;
      return;}
  }
  int pop() {
    if (isEmpty() ) {
      cerr << "\tEmpty Stack. Don't Pop\n\n";
      return 1;
    }
    else 
      return arr[top--];
  }
  bool isEmpty() {return top < 0 ? 1 : 0;}
  bool isFull() {return top >= MaxStack -1 ? top : 0;}
  void dump_stack() {
    cout << "The Stack contents, from top to bottom, from a stack dump are: " << endl;
    for (int s = top; s >= 0; s--)
      cout << "\t\t" << arr[s] << endl;
  }
private:  
  int top;
  double arr[MaxStack];
}pStack;
// ------------------------------ end stack class -----------------------------
 
// -----------------------------function prototypes----------------------------
void evalPostfix (string);
double decimalEvaluate (string, int, double);
// ----------------------------------------------------------------------------
 
// -----------------------------------Main()-----------------------------------
int main()
{
        cout << "Enter a postfix expression\n\t (without spaces - using '_' for delimiters):\n\t";
        cout << "For example: 8_5_3_+_*_2_/_5_+\n" << endl;
        getline(cin, postfix);
 
//      postfix = "7_2_*_5_+_2_*";
        cout << "You entered: " << postfix << endl;
        
int c=0;
 
while ( postfix[c] !='\0' )
        {
        ++c;                    // this loop counts the characters in the input string including whitespace
        }
 
cout << "\tThe string length is:\t" << c << endl;
evalPostfix (postfix);
int result = pStack.pop();
cout << "The result of the postfix expression is: " << result << endl;
 
        
/*
stack commands:
        Stack a_stack;                  // creates new stack
        a_stack.init();                 // initializes top element
        a_stack.pop();                  // pops top of stack
        a_stack.push(n);                // push element to top of stack
        a_stack.dump_stack();   // displays the contents of the stack from top to bottom
*/      
        return 0;
        cin.get();
}
 
 
 
// --------------------------------end of Main()-------------------------------
 
 
// ------------------------------functions follow------------------------------
void evalPostfix (string)
{
        double ch=0,dc=0,b=0,a=0,d=0;
        double tempDC=0;
        double tempDCD=0;
        char op;
        int i=0,j=0,k=0,m=0,n=0,q=0;
        while (postfix[i] != '\0')
        {
 
        if (postfix[i] == '_') {i++;}
        
        else if (isdigit (postfix[i]))
                        {
                                ch = postfix[i] - '0'; // for numbers only
                                j=i+1;
                                while (postfix[j] != '_')
                                {
                                        if (isdigit (postfix[j]))
                                        {
                                        ch = ch*10 + (postfix[j] - '0');
                                        k=j+1;
                                         if (postfix[k] == '.') // this accounts for decimals by skipping the '.' and conducting operations on trailing numbers
                                                {
                                                dc=0;
                                                decimalEvaluate (postfix, k, dc);
                                                dc = tempDC / tempDCD;
                                                d=ch+dc;
                                                k++;
                                                
                                                }
                                        j=k-1;
                                        j++;                                    
                                        }
                                }
cout << "Post decimal function k: " << k << endl;
cout << "Post decimal function dc: " << setprecision (12) << dc << endl;
cout << "Post decimal function d: " << d << endl;
                                pStack.push(d);
                                i=j-1;
                                i++;
                        }
        else if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '^')
                        {  
                        b=pStack.pop();
                        a=pStack.pop();
                        op = postfix[i]; // for operators only
                        switch(op)
                                {
                                case '+':pStack.push(a+b);
                                        break;
                                case '-':pStack.push(a-b);
                                        break;
                                case '*':pStack.push(a*b);
                                        break;
                                case '^':pStack.push(pow(a,b));
                                        break;
                                case '/':if(b==0)
                                         {
                                                 cout << "Division by zero not allowed!" << endl;
                                                 cin.get();
                                                 exit(0);
                                         }
                                         pStack.push(a/b);
                                default:cout << "Invalid Operation" << endl;
                                }
                        i++;
                        }
        
        }
}
// ----------------------------------------------------------------------------
 
double decimalEvaluate (string postfix, int k, double dc)
{
        dc=0;
        double tempDC=0;
        double tempDCD=0;
        
        int n=0, m=0,lenDC=0;
        
        n=k;
 
while ( postfix[n] != '_' )
        {
        if ((isdigit (postfix[n]))== false)
                {
                n++;
                }
cout << "This step (1) n: " << n << endl;               
        if (isdigit (postfix[n]))
                {
                m=n;
                while ( postfix[m] != '_' ) // assumes characters between a '.' and '_' are all digits (may need to check)
                        {
                        lenDC++;
                        m++;                    // this loop counts the digits in the input trailing a decimal point
                        }
cout << "This step (2) m: " << m << endl;
cout << "This step (2) lenDC: " << lenDC << endl;                                                               
                while ((postfix[n]) != '_')
                        {
                        tempDC = tempDC*10 + (postfix[n]) - '0';
                        n++;
                        }
cout << "This step (3) n: " << n << endl;
cout << "This step (3) tempDC: " << tempDC << endl;
                }
        k=n;
        
        tempDCD = pow(10,lenDC);
        
        dc = tempDC / tempDCD;
 
cout << "This step (4) k: " << k << endl;
cout << "This step (4) tempDCD: " << tempDCD << endl;
cout << "This step (4) tempDC: " << tempDC << endl;
cout << "This step (4) dc: " << dc << endl;
        }
        return dc,k,tempDC,tempDCD;
}
 
 
  • upload with new input
  • result: Success     time: 0.02s    memory: 2868 kB     returned value: 0

    17.3_13.7_+
    Enter a postfix expression
    	 (without spaces - using '_' for delimiters):
    	For example: 8_5_3_+_*_2_/_5_+
    
    You entered: 17.3_13.7_+
    	The string length is:	11
    This step (1) n: 3
    This step (2) m: 4
    This step (2) lenDC: 1
    This step (3) n: 4
    This step (3) tempDC: 3
    This step (4) k: 4
    This step (4) tempDCD: 10
    This step (4) tempDC: 3
    This step (4) dc: 0.3
    Post decimal function k: 4
    Post decimal function dc: nan
    Post decimal function d: nan
    Just pushed nan
    This step (1) n: 8
    This step (2) m: 9
    This step (2) lenDC: 1
    This step (3) n: 9
    This step (3) tempDC: 7
    This step (4) k: 9
    This step (4) tempDCD: 10
    This step (4) tempDC: 7
    This step (4) dc: 0.7
    Post decimal function k: 9
    Post decimal function dc: nan
    Post decimal function d: nan
    Just pushed nan
    Just pushed -4294967296
    The result of the postfix expression is: -2147483648