home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / calc.m < prev    next >
Encoding:
Text File  |  1995-10-16  |  1.6 KB  |  94 lines

  1. #import "Calc.h"
  2. #include "integrate.h"
  3.  
  4. //digits are 0 through 9
  5. #define DIVIDE 10
  6. #define MULTIPLY 11
  7. #define SUBTRACT 12
  8. #define ADD 13
  9. #define NOOP 20
  10.  
  11. @implementation Calc
  12.  
  13. - init
  14. {
  15.     [super init];
  16.     [self clearAll];
  17.     return self;
  18. }
  19.  
  20. - (void)clearAll
  21. {
  22.     xRegister = 0.0;
  23.     pendingOperation = NOOP;        
  24. }
  25.  
  26. - (double)performOperationWith:(double)value
  27. {
  28.     switch (pendingOperation){
  29.         case ADD:
  30.             xRegister += value;
  31.             break;
  32.         case SUBTRACT:
  33.             xRegister -= value;
  34.             break;
  35.         case MULTIPLY:
  36.             xRegister *= value;
  37.             break;
  38.         case DIVIDE:
  39.             xRegister /= value;
  40.             break;
  41.         default:
  42.             xRegister = value;
  43.             break;
  44.     }
  45.     pendingOperation = NOOP;
  46.     return xRegister;
  47. }
  48.  
  49. - (double)setOperation:(int)operator forValue:(double)value
  50. {
  51.     xRegister = [self performOperationWith:value];  // do any pending stuff
  52.     pendingOperation = operator;
  53.     return xRegister;
  54. }
  55.  
  56. - (void)changePendingOperation:(int)operator
  57. {
  58.     pendingOperation = operator;
  59. }
  60.  
  61. @end
  62.  
  63. void *createCalcObject()
  64. {
  65.     Calc *aCalc = [[Calc alloc] init];
  66.  
  67.     return (void *)aCalc;
  68. }
  69.  
  70. void clearAll(void *aCalc)
  71. {
  72.     [(id)aCalc clearAll];
  73. }
  74.  
  75. double performOperationWith(void *aCalc, double value)
  76. {
  77.     return [(id)aCalc performOperationWith:value];
  78. }
  79.  
  80. double setOperationForValue(void *aCalc, int operator, double value)
  81. {
  82.     return [(id)aCalc setOperation:operator forValue:value];
  83. }
  84.  
  85. void changePendingOperation(void *aCalc, int operator)
  86. {
  87.     [(id)aCalc changePendingOperation:operator];
  88. }
  89.  
  90. void *destroyCalcObject(void *aCalc)
  91. {
  92.     return [(id)aCalc free];
  93. }
  94.