Facebook
From Gruff Guinea Pig, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 304
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4.  
  5. #define BUFSIZE 1000
  6. #define MAXVALUE 1000
  7. #define MAXOP 1000
  8. #define NUMBER '0'
  9.  
  10. char bufor[BUFSIZE];
  11. double heap[MAXVALUE];
  12. int bufindex;
  13. int heapindex;
  14.  
  15. int getch(void);
  16. void ungetch(int c);
  17. int getoperator(char s[]);
  18. void push(double f);
  19.  
  20.  
  21. int main()
  22. {
  23.     int op;
  24.     double temp;
  25.     char s[MAXOP];
  26.  
  27.     while((op == getoperator(s)) != EOF){
  28.         switch(op) {
  29.             case NUMBER:
  30.                 push(atof(op));
  31.                 break;
  32.             case '*':
  33.                 push(pop()*pop());
  34.                 break;
  35.         }
  36.     }
  37. }
  38.  
  39. void push(double f){
  40.     if(heapindex < MAXVALUE)
  41.         heap[heapindex++] = f;
  42.     else
  43.         printf("Koniec stosu!");
  44. }
  45.  
  46. double pop(void){
  47.     if (heapindex > 0)
  48.         return heap[--heapindex];
  49.     else {
  50.         printf("Pusty stos!");
  51.         return 0;
  52.     }
  53. }
  54.  
  55.  
  56. int getoperator(char s[]){
  57.     int i, c;
  58.     while(c = getch() == ' ' || c == 't');
  59.  
  60.     s[1] = '';
  61.  
  62.     i = 0;
  63.  
  64.     if(!isdigit(c) && c != '.' && c != '-')
  65.         return c;
  66.  
  67.     if(c=='-'){
  68.         if(isdigit(c=getch()) || c == '.')
  69.             s[++i] = c;
  70.         else{
  71.             if(c != EOF){
  72.                 ungetch(c);
  73.             return '-';
  74.             }
  75.         }
  76.     }
  77.  
  78.     if(isdigit(c))
  79.         while(isdigit(s[++i] = c = getch()));
  80.     if(c == '.')
  81.         while(isdigit(s[++i] = c = getch()));
  82.  
  83.     s[i] = '';
  84.  
  85.     if (c != EOF)
  86.         ungetch(c);
  87.     return NUMBER;
  88. }
  89.  
  90. int getch(void){
  91.     return(bufindex > 0) ? bufor[--bufindex] : getchar();
  92. }
  93.  
  94. void ungetch(int c){
  95.     if(bufindex >= BUFSIZE)
  96.         printf("Poza zakresem!");
  97.     else
  98.         bufor[bufindex++] = c;
  99. }
  100.