Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <math.h>
- #include <stdlib.h>
- #define SIZE 50 /* Size of Stack */
- double s[SIZE];
- int peak=-1; /* Global declarations */
- char pofx[50];
- double pop()
- { /* Function for POP operation */
- return(s[peak--]);
- }
- double push(double elem) {
- if (peak + 1 >= SIZE) {
- printf("Stack overflown");
- return 0;
- }
- s[++peak] = elem;
- }
- void main()
- { /* Main Program */
- printf("Enter the Postfix Expression:");
- // fgets(pofx,100,stdin); // 100??
- fgets(pofx, sizeof pofx, stdin); // better
- postfixtoresult();
- printf("Result: %lfn",s[peak]);
- }
- void postfixtoresult()
- {
- int i=0;
- const char *st = pofx;
- while (*st) {
- char *end; //location to store end of FP parsing
- double value = strtod(st, &end);
- if (end > st) {
- push(value);
- st = end;
- } else if (isspace((unsigned char) *st)) {
- st++;
- } else {
- switch (*st) {
- case '+':push(pop() + pop());break; // pop order irrelevant
- case '-':{ double t = pop(); push(pop() - t);break; } // pop order relevant
- case '*':push(pop() * pop());break; // pop order irrelevant
- case '/':{ double u = pop(); push(pop() / u);break; } // pop order relevant
- case '^':{ double v = pop(); push(pow(pop(),v));break; } // pop order relevant
- default: {
- printf("Input invalid operator: character code %dn", *st);
- return 0;
- }
- } // end switch
- st++;
- }
- }
- }
Add Comment
Please, Sign In to add comment