home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ENTERPRS / CPM / UTILS / S / SCI-12.ARC / CALC.SCI next >
Text File  |  1986-12-16  |  2KB  |  108 lines

  1. char *Lineptr;
  2. int Stack[ 10 ], Stackptr, Stacktop;
  3.  
  4. calc()
  5. {
  6.    char line[ 80 ];
  7.  
  8.    Stacktop = 10;
  9.    while(1)
  10.    {
  11.       puts("-> ");
  12.       if ( gets(Lineptr=line) )
  13.       {
  14.          if( *Lineptr=='x' )
  15.             return;
  16.          addition();
  17.          printf( "%d\n",pop() );
  18.       }
  19.    }
  20. }
  21.  
  22. number()
  23. {
  24.    if( isdigit( *Lineptr ) )
  25.    {
  26.       push( atoi( Lineptr ) );
  27.       while( isdigit( *Lineptr ) )
  28.          Lineptr = Lineptr+1;
  29.    }
  30. }
  31.  
  32. addition()
  33. {
  34.    int num;
  35.  
  36.    while(1)
  37.    {
  38.       multiplication();
  39.       if( *Lineptr=='+' )
  40.       {
  41.          Lineptr = Lineptr+1;
  42.          multiplication();
  43.          push( pop() + pop() );
  44.       }
  45.       else if( *Lineptr=='-' )
  46.       {
  47.          Lineptr = Lineptr+1;
  48.          multiplication();
  49.          num = pop();
  50.          push( pop() - num );
  51.       }
  52.       else
  53.          return;
  54.    }
  55. }
  56.  
  57. multiplication()
  58. {
  59.    int num;
  60.  
  61.    while(1)
  62.    {
  63.       number();
  64.       if( *Lineptr=='*' )
  65.       {
  66.          Lineptr = Lineptr+1;
  67.          number();
  68.          push( pop() * pop() );
  69.       }
  70.       else if( *Lineptr=='/' )
  71.       {
  72.          Lineptr = Lineptr+1;
  73.          number();
  74.          num = pop();
  75.          push( pop() / num );
  76.       }
  77.       else
  78.          return;
  79.    }
  80. }
  81.  
  82. push( n )
  83. {
  84.    if( Stackptr<Stacktop )
  85.    {
  86.       Stack[ Stackptr ] = n;
  87.       Stackptr = Stackptr+1;
  88.       return n;
  89.    }
  90.    puts( "stack overflow\n" );
  91. }
  92.  
  93. pop()
  94. {
  95.    if ( Stackptr>0 )
  96.    {
  97.       Stackptr = Stackptr-1;
  98.       return Stack[ Stackptr ];
  99.    }
  100.    puts( "stack underflow\n" );
  101. }
  102.  
  103. isdigit( c )
  104. char c;
  105. {
  106.    return '0'<=c && c<='9';
  107. }
  108. tion