language: C (gcc-4.7.2)
date: 548 days 15 hours ago
link:
visibility: public
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define OFFSET '0'
 
typedef double stackElementT;
 
typedef struct {
  stackElementT *contents;
  int maxSize;
  int top;
  int min2;
} stackT;
 
void StackInit(stackT *stackP, int maxSize) {
        stackElementT *newContents;
        newContents = (stackElementT *)malloc(sizeof(stackElementT)*maxSize);
        if (newContents == NULL) {
                fprintf(stderr, "Not enough memory.\n");
                exit(1);
        }
 
        stackP->contents = newContents;
        stackP->maxSize = maxSize;
        stackP->top = -1;
}
 
void StackDestroy(stackT *stackP) {
        free(stackP->contents);
        stackP->contents = NULL;
        stackP->maxSize = 0;
        stackP->top = -1;
}
 
int StackIsEmpty(stackT *stackP) { return stackP->top < 0; }
 
int StackIsFull(stackT *stackP) { return stackP->top >= stackP->maxSize-1; }
 
void StackPush(stackT *stackP, stackElementT element) {
        if(StackIsFull(stackP)) {
                fprintf(stderr, "Can't push element: stack is full.\n");
                exit(1);
        }
        stackP->contents[++stackP->top] = element;
}
 
stackElementT StackPop(stackT *stackP) {
        if(StackIsEmpty(stackP)) {
                fprintf(stderr, "Can't pop element: stack is empty.\n");
                exit(1);
        }
        return stackP->contents[stackP->top--];
}
 
int is_operand(char x) {
    switch(x) {
        case '+': case '-': case '/': case '*': case '^': return 1;
        default : return 0;
    }
}
 
double postfix(char* expr) {
        int length = strlen(expr), i;
        double temp = 0, temp2;
        stackT stack;
        StackInit(&stack, 100000);
        for (i = 0; i < length; i++) {
                if ((expr[i] >= '0') && (expr[i] <= '9')) {
                        temp *= 10;
                        temp += expr[i]-OFFSET;
                }
        else if (expr[i] == ' ') {
                        StackPush(&stack, temp);
                        temp = 0;
        }
 
                else {
                        switch (expr[i]) {
 
                                case '+': {
                                        temp = StackPop(&stack);
                                        temp2 = StackPop(&stack);
                                        printf("%.0f+%.0f -> ", temp2, temp);
                                        StackPush(&stack, temp2+temp);
                                        temp = temp2+temp;
                                }
                                        break;
 
                                case '-': {
                                        temp = StackPop(&stack);
                                        temp2 = StackPop(&stack);
                                        printf("%.0f-%.0f -> ", temp2, temp);
                                        StackPush(&stack, temp2-temp);
                                        temp = temp2-temp;
                                }
                                        break;
 
                                case '/': {
                                        temp = StackPop(&stack);
                                        temp2 = StackPop(&stack);
                                        printf("%.0f/%.0f -> ", temp2, temp);
                                        StackPush(&stack, temp2/temp);
                                        temp = temp2/temp;
                                }
                                        break;
 
                                case '*': {
                                        temp = StackPop(&stack);
                                        temp2 = StackPop(&stack);
                                        printf("%.0f*%.0f -> ", temp2, temp);
                                        StackPush(&stack, temp2*temp);
                                        temp = temp2*temp;
                                }
                                        break;
 
                                case '^': {
                                        temp = StackPop(&stack);
                                        temp2 = StackPop(&stack);
                                        printf("%.0f^%.0f -> ", temp2, temp);
                                        StackPush(&stack, pow(temp2, temp));
                                        temp = pow(temp2, temp);
                                }
 
                                default:
                                        break;
 
                        }
                }
        }
 
        return StackPop(&stack);
}
 
int main() {
        while (1) {
                printf("post>   ");
                char *expr = malloc(1000*sizeof(char) + 1);
                fgets(expr, 1000, stdin);
                printf("\n     %.0f\n", postfix(expr));
                free(expr);
        }
}
prog.c: In function ‘main’:
prog.c:139: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result
  • upload with new input
  • result: Success     time: 0s    memory: 2640 kB     returned value: 1

    post>   
    Can't pop element: stack is empty.