All pastes #933051 Raw Edit

Meins

public text v1 · immutable
·published 2008-03-08 02:17 UTC
rendered paste body
#include <vld.h>
#include <stdio.h>
#include <string.h>

#include "stack.h"

int main( int argc, char* argv[]) {
 Stack st = 0;
 char inp[16], exit=0;
 size_t count;
 StackDataType number, n1, n2;

 printf("*  * ***      ***   **   *     ***\n");
 printf("*  * *  *    *     *  *  *    *\n");
 printf("**** ***     *     ****  *    *\n");
 printf("*  * *       *    *    * *    *\n");
 printf("*  * *        *** *    * ****  ***\n\n");

 printf("please enter mathematical expression in postorder notation\n\n\n");

 initStack( &st);

 do {
  do {
   if( !scanf( "%15s", inp))
    continue;
   count = strlen( inp);
   if( sscanf(inp, "%lf", &number) > 0) {
    pushStack( &st, number);
    continue;
   }
   if( count == 1 && ( inp[0] == '+' || inp[0] == '-' || inp[0] == '*' || inp[0] == '/') ) {
    if( popStack( &st, &n2) || popStack( &st, &n1)) {
     printf("Not enough parameters for action - restart\n");
     exit = 1;
     break;
    }
    switch(inp[0]) {
     case '+':
      n1 += n2;
      break;
     case '-':
      n1 -= n2;
      break;
     case '/':
      if( n2 == 0) {
       printf(" <> division by zero detected - restart");
       exit = 1;
       break;
      }
      n1 /= n2;
      break;
     case '*':
      n1 *= n2;
      break;
    }
    pushStack( &st, n1);
    printf( " > %f\n", n1);
   }
   if( !strcmp( inp, "reset")) {
    exit = 1;
    break;
   }
   if( !strcmp( inp, "quit")) {
    exit = 2;
    break;
   }
  } while(!exit);

  if( exit == 1) {
   while( !isStackEmpty( st))
    popStack( &st, 0);
   exit = 0;
  }

 } while( exit != 2);

}