home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / apple2 / 35 < prev    next >
Encoding:
Text File  |  1990-12-02  |  4.4 KB  |  251 lines

  1. Path: wuarchive!usc!cs.utexas.edu!rutgers!aramis.rutgers.edu!paul.rutgers.edu!yoko.rutgers.edu!jac
  2. From: jac@yoko.rutgers.edu (Jonathan A. Chandross)
  3. Newsgroups: comp.sources.apple2
  4. Subject: v001SRC011:  Simple Desk Calculator in C
  5. Message-ID: <Dec.1.16.42.38.1990.24520@yoko.rutgers.edu>
  6. Date: 1 Dec 90 21:42:39 GMT
  7. Organization: Rutgers Univ., New Brunswick, N.J.
  8. Lines: 240
  9. Approved: jac@paul.rutgers.edu
  10.  
  11.  
  12. Submitted-by: NONE
  13. Posting-number: Volume 1, Source:11
  14. Archive-name: util/calculator/dc
  15. Architecture: ANY_2
  16. Version-number: 1.00
  17.  
  18. Enclosed is a simple desk calculator in C.
  19.  
  20. Enjoy.
  21.  
  22.  
  23. =dc.c
  24. -/*
  25. - * dc.c
  26. - *
  27. - * Desk calculator. Supports +,-,* and / on integer and floating point.
  28. - *
  29. - * Usage:
  30. - *     dc
  31. - *
  32. - * Commands:
  33. - *    d         duplicate top of stack (TOS)
  34. - *    p         print TOS
  35. - *    f         print all stack entries
  36. - *    c         clear stack
  37. - *    q         quit
  38. - *    *         replace TOS, NOS with NOS * TOS
  39. - *    /         replace TOS, NOS with NOS / TOS
  40. - *    +         replace TOS, NOS with NOS * TOS
  41. - *    -         replace TOS, NOS with NOS - TOS
  42. - *    <number>     push <number> onto stack, where number is:
  43. - *                [+|-] digit+
  44. - *                [+|-] [digit+] . digit+
  45. - *
  46. - * Adapted from C Reference Manual
  47. - *
  48. - * Notes: must be linked to flt.lib
  49. - *
  50. - * Contributed Anonymously.  Written: November 1983
  51. - *
  52. - * Version 1.00
  53. - *
  54. - */
  55. -
  56. -#include "stdio.h"    /* contains extra defines for EOF, etc */
  57. -
  58. -#define BLANK ' '
  59. -#define TAB   '\t'
  60. -#define NL    '\n'
  61. -#define EOS   '\0'
  62. -
  63. -#define MAXOP  20
  64. -#define NUMBER '0'
  65. -#define TOOBIG '9'
  66. -
  67. -main()
  68. -{
  69. -    int type ;
  70. -    char s[MAXOP] ;
  71. -    double op2, atof(), pop(), push() ;
  72. -
  73. -    while( (type = getop(s,MAXOP)) != EOF )
  74. -        switch( type ) {
  75. -            case NUMBER:
  76. -                push( atof(s) ) ;
  77. -                break ;
  78. -            case '+':
  79. -                push( pop() + pop() ) ;
  80. -                break ;
  81. -            case '*':
  82. -                push( pop() * pop() ) ;
  83. -                break ;
  84. -            case '-':
  85. -                op2 = pop() ;
  86. -                push( pop() - op2 ) ;
  87. -                break ;
  88. -            case '/':
  89. -                op2 = pop() ;
  90. -                if( op2 != 0.0 )
  91. -                    push( pop() / op2 ) ;
  92. -                else
  93. -                    fprintf(stderr, "dc: divide by zero\n");
  94. -                break ;
  95. -            case 'd':
  96. -                push( push( pop() ) ) ;
  97. -                break ;
  98. -            case 'p':
  99. -                top() ;
  100. -                break ;
  101. -            case 'f':
  102. -                print() ;
  103. -                break ;
  104. -            case 'c':
  105. -                empty() ;
  106. -                break ;
  107. -            case 'q':
  108. -                printf("\n") ;
  109. -                exit(0) ;
  110. -            case TOOBIG:
  111. -                fprintf(stderr, "dc: %.20s is too long\n", s ) ;
  112. -                break ;
  113. -            default:
  114. -                fprintf(stderr, "dc: unknown commnd %c\n", type ) ;
  115. -                break ;
  116. -        }
  117. -
  118. -} /* end main */
  119. -
  120. -/* stack declarations */
  121. -
  122. -#define STACKSIZE 100
  123. -
  124. -int sp = 0 ;
  125. -double stack[STACKSIZE] ;
  126. -
  127. -/* push - push new value on stack */
  128. -
  129. -double push(f)
  130. -double f ;
  131. -{
  132. -    if( sp < STACKSIZE )
  133. -        return stack[sp++] = f ;
  134. -    else {
  135. -        printf("dc: stack is full\n");
  136. -        empty() ;
  137. -        return 0 ;
  138. -    }
  139. -
  140. -} /* end push */
  141. -
  142. -/* pop - return top of stack */
  143. -
  144. -double pop()
  145. -{
  146. -    if( sp > 0 )
  147. -        return stack[--sp] ;
  148. -    else {
  149. -        printf("dc: stack empty\n") ;
  150. -        empty() ;
  151. -        return 0 ;
  152. -    }
  153. -
  154. -} /* end pop */
  155. -
  156. -/* empty - empty the stack */
  157. -
  158. -empty()
  159. -{
  160. -    sp = 0 ;
  161. -}
  162. -
  163. -/* top - print top stack entry */
  164. -
  165. -top()
  166. -{
  167. -    int i ;
  168. -    double d ;
  169. -
  170. -    if( sp <= 0 )
  171. -        return ;
  172. -    d = stack[ sp-1 ] ;
  173. -    i = d ;
  174. -    if( ((double) i) == d )
  175. -        printf("%c%d\n", TAB, i ) ;
  176. -    else
  177. -        printf("%c%f\n", TAB, d ) ;
  178. -}
  179. -
  180. -/* print - show all stack entries */
  181. -
  182. -print()
  183. -{
  184. -    int i, s ;
  185. -    double d ;
  186. -
  187. -    for( s=sp-1 ; s >= 0 ; s-- ) {
  188. -        d = stack[ s ] ;
  189. -        i = d ;
  190. -        if( ((double) i) == d )
  191. -            printf("%c%d\n", TAB, i ) ;
  192. -        else
  193. -            printf("%c%f\n", TAB, d ) ;
  194. -    }
  195. -}
  196. -
  197. -/* getop - get next operator, operand */
  198. -
  199. -getop( s, limit )
  200. -char s[] ;
  201. -int limit ;
  202. -{
  203. -    int i, c, oldc ;
  204. -
  205. -    i = 0 ;
  206. -    do
  207. -        c = agetc(stdin) ;
  208. -        while( c == BLANK || c == TAB || c == NL ) ;
  209. -
  210. -    i = 0 ;
  211. -    if( c != '.' && c != '-' && c != '+' && (c < '0' || c > '9') )
  212. -        return c ;
  213. -
  214. -    if( c == '+' || c == '-' ) {
  215. -        oldc = c ;
  216. -        c = agetc(stdin) ;
  217. -        if( c != '.' && (c < '0' || c > '9') ) {
  218. -            ungetc( c, stdin ) ;
  219. -            return oldc ;
  220. -        }
  221. -        else
  222. -            s[i++] = oldc ;
  223. -    }
  224. -    s[i++] = c ;
  225. -    for( ; (c = agetc(stdin)) >= '0' && c <= '9' ; i++ )
  226. -        if( i < limit )
  227. -            s[i] = c ;
  228. -    if( c == '.' ) {
  229. -        if( i < limit )
  230. -            s[i] = c ;
  231. -        for( i++ ; (c = agetc(stdin)) >= '0' && c <= '9' ; i++ )
  232. -            if( i < limit )
  233. -                s[i] = c ;
  234. -    }
  235. -    if( i < limit ) {
  236. -        ungetc( c, stdin ) ;
  237. -        s[i] = EOS ;
  238. -        return NUMBER ;
  239. -    }
  240. -    else {
  241. -        while( c != NL && c != EOF )
  242. -            c = agetc(stdin) ;
  243. -        s[limit-1] = EOS ;
  244. -        return TOOBIG ;
  245. -    }
  246. -
  247. -} /* end getop */
  248. -
  249. -
  250. + END OF ARCHIVE
  251.