home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / DeveloperLabs / Lab1 / Calculator.m < prev    next >
Encoding:
Text File  |  1993-01-18  |  2.6 KB  |  128 lines

  1. /*     
  2. **     Calculator exercise, lab #1.
  3. **     Authors: Bruce Blumberg and Doug Keislar, NeXT Technical Support
  4. */
  5.  
  6. #import "Calculator.h"
  7. #import <string.h>
  8. #import <math.h>    /* for extensions to existing functionality */    
  9. #import <appkit/Application.h>
  10. #import <appkit/TextField.h>
  11.  
  12. @implementation Calculator:Object
  13.  
  14. +new;
  15. {    
  16.     self = [super new];        /* create new instance of calculator */
  17.     strcpy(viewerStr,"");        /* initialize string for viewer */
  18.     topOfStack = -1;        /* initialize stack */
  19.     frozen = NO;
  20.     return self;
  21. }
  22.  
  23. -(double)pop
  24. {
  25.     if(topOfStack>-1)     
  26.         return(stack[topOfStack--]);
  27.     else return(0);
  28. }
  29.  
  30. -push:(double)aNum
  31. {
  32.     if(topOfStack<STACKSIZE-1)
  33.         stack[++topOfStack] = aNum;
  34.     return self;
  35. }
  36.  
  37. -(double)getOperand
  38. {    
  39.         if (viewerStr[0]) 
  40.         [self enter:self];    /* push displayed value to stack,
  41.                     if it hasn't already been done */
  42.     return ([self pop]);
  43. }
  44.  
  45. -displayValue:(double)aNum
  46. {
  47.     [viewer setFloatValue:aNum];     /* display current value */
  48.     strcpy(viewerStr,"");        /* and empty out string for next */
  49.     return self;
  50. }
  51.  
  52. -pushAndDisplay:(double)aNum
  53. {
  54.     [self push:aNum];
  55.     [self displayValue:aNum];
  56.     return self;
  57. }
  58.  
  59. -enter:(id)sender
  60. {    
  61.     [self push:[viewer floatValue]];
  62.     strcpy(viewerStr,"");        
  63.     return self;
  64. }
  65.  
  66. -clearDisplay:(id)sender
  67. {
  68.     if (frozen)  
  69.         frozen = NO;         
  70.     strcpy(viewerStr,"");
  71.     [self displayValue:0.0];
  72.     return self;
  73. }
  74.  
  75. -digit:(id)sender
  76. {
  77.     char    digitBuf[2];
  78.             /* get the new digit from the  button's tag and 
  79.               append it to the displayed string */
  80.     sprintf(digitBuf,"%d",[sender selectedTag]);
  81.             /* we use selectedTag instead of tag, in case the
  82.             button is part of a matrix */    
  83.     strcat(viewerStr,digitBuf);                 
  84.     [viewer setStringValue:viewerStr];
  85.     return self;
  86. }
  87.     
  88. -period:(id)sender {}        /* EXERCISE: Fill in this method */
  89.     
  90. -add:(id)sender
  91. {    
  92.  
  93.     if (!frozen) 
  94.         [self pushAndDisplay:[self getOperand]+[self getOperand]];
  95.     return self;
  96. }
  97.             /* EXERCISE: Fill in the following methods */
  98.  
  99. -subtract:(id)sender {}
  100. -multiply:(id)sender {}
  101. -divide:(id)sender {}
  102. -changeSign:(id)sender {}
  103.  
  104. -stackError:(STR)errorMsg; {}   /* OPTIONAL EXERCISE: Display error message and  
  105.                 reinitialize stack.  Also, all buttons except 
  106.                 "clear" should freeze.  Most of the preceding 
  107.                 methods will require some modification. */
  108.  
  109.  
  110.                 /* the following methods are complete
  111.                    and are generated by Interface Builder */
  112.                                 
  113. - setAWindow:anObject        /* associate the outlet "aWindow" with
  114.                    the user-interface window, "anObject"  */
  115. {
  116.       aWindow = anObject;
  117.        return self;
  118. }
  119.  
  120. - setViewer:anObject        /*  initialize the other outlet, viewer  */
  121. {
  122.       viewer = anObject;
  123.       return self;
  124. }
  125.  
  126. @end
  127.  
  128.