home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / Calc.m < prev    next >
Encoding:
Text File  |  1995-08-31  |  1.3 KB  |  85 lines

  1. #import "Calc.h"
  2.  
  3. //digits are 0 through 9
  4. #define DIVIDE 10
  5. #define MULTIPLY 11
  6. #define SUBTRACT 12
  7. #define ADD 13
  8. #define NOOP 20
  9.  
  10. @implementation Calc
  11.  
  12. - init
  13. {
  14.     [super init];
  15.     [self clearAll];
  16.     return self;
  17. }
  18.  
  19. - (void)clearAll
  20. {
  21.     xRegister = 0.0;
  22.     pendingOperation = NOOP;    
  23. }
  24.  
  25. - (double)performOperationWith:(double)value
  26. {
  27.     switch (pendingOperation){
  28.         case ADD:
  29.             xRegister += value;
  30.             break;
  31.         case SUBTRACT:
  32.             xRegister -= value;
  33.             break;
  34.         case MULTIPLY:
  35.             xRegister *= value;
  36.             break;
  37.         case DIVIDE:
  38.             xRegister /= value;
  39.             break;
  40.         default:
  41.             xRegister = value;
  42.             break;
  43.     }
  44.     pendingOperation = NOOP;
  45.     return xRegister;
  46. }
  47.  
  48. - (double)setOperation:(int)operator forValue:(double)value
  49. {
  50.     xRegister = [self performOperationWith:value];    // do any pending stuff
  51.     pendingOperation = operator;
  52.     return xRegister;
  53. }
  54.  
  55. - (void)changePendingOperation:(int)operator
  56. {
  57.     pendingOperation = operator;
  58. }
  59.  
  60. @end
  61.  
  62.  
  63. int main(int argc, char *argv[])
  64. {
  65.     id calcObj;
  66.     id conn;
  67.  
  68.     [NXPort worryAboutPortInvalidation];
  69.  
  70.     calcObj = [[Calc alloc] init];
  71.     conn = [NXAutoreleaseConnection registerRoot:calcObj
  72.         withName:"Calculator"];
  73.     if (conn) {
  74.         fprintf(stderr, "Calculator registered\n");
  75.         [conn run];
  76.         [calcObj free];
  77.     } else {
  78.         fprintf(stderr, "Couldn't register calculator - exiting.\n");
  79.         exit(-1);
  80.     }
  81.  
  82.     exit(0);
  83. }
  84.  
  85.