Umgekehrte polnische Notation in der Kommandozeile



  • #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> /* wird benötigt für ungets */
    #include <ctype.h>
    #include <conio.h>
    
    #define MAXOP 100
    #define NUMBER '0'
    
    int getop(char []);
    void ungets(char []);
    void push(double);
    double pop(void);
    
    int main(int argc, char* argv[])
    {
      char s[MAXOP];
      double op2;
    
      argc = 7;
      while (--argc > 0)
      {
        ungets(" ");
        ungets(*++argv);
        switch(getop(s))
        {
          case NUMBER:
            push(atof(s));
            break;
          case '+':
            push(pop() + pop());
            break;
          case '*':
            push(pop() * pop());
            break;
          case '-':
            op2 = pop();
            push(pop() - op2);
            break;
          case '/':
            op2 = pop();
            if (op2 != 0.0)
              push(pop() / op2);
            else
              printf("error: zero divisor\n");
              break;
          default:
            printf("error: Unknown Command %s\n", s);
            argc = 1;
            break;
        }
      }
      printf("\t%.8g\n", pop());
      return 0;
    }
    
    /* getop: nächsten Operator oder numerischen Operanden holen */
    int getop (char s[])
    {
      int c, i;
      while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
      s[1] = '\0';
      i = 0;
      if (!isdigit(c) && c != '.' && c != '-')
        return c;
        if (c == '-')
          if (isdigit(c && c != getch()) || c == '.')
            s[++i] = c;
          else
          {
            if (c != EOF)
              ungetch(c);
              return '-';
          }
          if (isdigit(c))
            while (isdigit(s[++i] = c = getch()))
              ;
            if (c == '.')
              while (isdigit(s[++i] = c = getch()))
                ;
              s[i] = '\0';
              if (c != EOF)
                ungetch(c);
                return NUMBER;
    }
    
    /* ungets: Zeichenkette in die Eingabe zurückstellen */
    void ungets(char s[])
    {
      int len = strlen(s);
      ungetch(len);
    
      while (len > 0)
        ungetch(s[--len]);
    }
    
    /* getch: Nächstes (eventuell) zurückgestelltes) Zeichen holen */
    int getch(void)
    {
      int c, buf;
      if (buf != 0)
        c = buf;
        else
          c = getchar();
          buf = 0;
          return c;
    }
    
    /* pop: Wert vom Stack holen und liefern */
    double pop(void)
    {
      int sp = 0;
      double val[MAXOP];
      if (sp > 0)
        return val[--sp];
      else
      {
        printf("error: stack empty\n");
        return 0.0;
      }
    }
    
    /* push: f auf den Stack bringen */
    void push (double f)
    {
      int sp = 0;
      double val[MAXOP];
      if (sp < MAXOP)
        val[sp++] = f;
      else
        printf("error: stack full, can't push %g\n", f);
    }
    

    Ich hab dieses Programm geschrieben, dass die Eingabe von einer Rechenoperation in der Schreibweise (2 3 4 + 😉 richtig berechnet wird.
    Ich bekomme aber immer an dieser Stelle

    ungets(*++argv);
    

    einen Speicheradressenfehler... woran kann das denn liegen ? 😕


Anmelden zum Antworten