Guest User

Untitled

a guest
Feb 25th, 2011
971
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "MYCalculator.h"
  2.  
  3. @implementation MYCalculator
  4.  
  5. - (id) init {
  6.     self = [super init];
  7.     if (self) {
  8.         operator = 0;
  9.         currentVal = [[NSMutableString stringWithString: @"0"] retain];
  10.         previousVal = [[NSMutableString stringWithString: @"0"] retain];
  11.     }
  12.     return self;
  13. }
  14.  
  15. - (IBAction)butDigit:(id)sender {
  16.     NSMutableString * str = (NSMutableString *)[sender currentTitle];
  17.     if ([currentVal isEqualToString:@"0"])
  18.     {
  19.         currentVal = str;
  20.     }
  21.     else {
  22.         currentVal = [currentVal stringByAppendingString:str];
  23.     }
  24.     [txtViewer setText:currentVal];
  25. }
  26.  
  27. - (IBAction)butOperator:(id)sender {
  28.     NSString *tmpStr = [currentVal substringFromIndex:([currentVal length] - 1)];
  29.     if ([tmpStr isEqualToString:@"."]) {
  30.         currentVal = (NSMutableString *)[currentVal substringToIndex:([currentVal length] - 1)];
  31.         [txtViewer setText:currentVal];
  32.     }
  33.     NSMutableString* str = (NSMutableString *)[sender currentTitle];
  34.     if (operator >= 1 && operator <= 4) {
  35.         [self doEquals];
  36.     }
  37.     if (operator != 5) {
  38.         previousVal = [currentVal copy];
  39.         currentVal = @"0";
  40.     }
  41.     if ([str isEqualToString:@"+"]) {
  42.         operator = 1;
  43.     } else if ([str isEqualToString:@"-"]) {
  44.         operator = 2;
  45.     } else if ([str isEqualToString:@"x"]) {
  46.         operator = 3;
  47.     } else if ([str isEqualToString:@"/"]) {
  48.         operator = 4;
  49.     }
  50. }
  51.  
  52. - (IBAction)butEquals:(id)sender {
  53.     [self doEquals];
  54. }
  55.  
  56. - (void)doEquals {
  57.     if (operator >= 1 && operator <= 4) {
  58.         NSDecimalNumber* num1 = 0;
  59.         num1 = [NSDecimalNumber decimalNumberWithString:previousVal];
  60.         NSDecimalNumber* num2 = 0;
  61.         num2 = [NSDecimalNumber decimalNumberWithString:currentVal];
  62.         if (operator == 1){
  63.             num1 = [num1 decimalNumberByAdding:num2];
  64.             currentVal = [NSMutableString stringWithString:[num1 stringValue]];
  65.         } else if (operator == 2){
  66.             num1 = [num1 decimalNumberBySubtracting:num2];
  67.             currentVal = [NSMutableString stringWithString:[num1 stringValue]];
  68.         } else if (operator == 3){
  69.             num1 = [num1 decimalNumberByMultiplyingBy:num2];
  70.             currentVal = [NSMutableString stringWithString:[num1 stringValue]];
  71.         } else if (operator == 4){
  72.             if (![currentVal isEqualToString:@"0"]) {
  73.                 num1 = [num1 decimalNumberByDividingBy:num2];
  74.                 currentVal = [NSMutableString stringWithString:[num1 stringValue]];
  75.             }
  76.         }
  77.         [txtViewer setText:currentVal];
  78.         previousVal = [currentVal copy];
  79.         currentVal = @"0";
  80.         operator = 5;
  81.     }
  82. }
  83.  
  84. - (IBAction)butDecimal:(id)sender {
  85.     NSRange range = [currentVal rangeOfString:@"." options:(NSCaseInsensitiveSearch)];
  86.     if (range.location == NSNotFound) {
  87.         currentVal = [currentVal stringByAppendingString:@"."];
  88.     }
  89.     [txtViewer setText:currentVal];
  90. }
  91.  
  92. - (IBAction)butClear:(id)sender {
  93.     currentVal = @"0";
  94.     previousVal = @"0";
  95.     operator = 0;
  96.     [txtViewer setText:currentVal];
  97. }
  98.  
  99. - (IBAction)butAbout:(id)sender {
  100.     //Show Alert
  101.     UIAlertView *alert = [[UIAlertView alloc]
  102.                           initWithTitle:@"About iCalculator"
  103.                           message:@"iCalculator version 1.0\nAuthor Pasha L. Topchiyev"
  104.                           delegate:self
  105.                           cancelButtonTitle:@"OK"
  106.                           otherButtonTitles: nil];
  107.     [alert show];  
  108.     [alert release];
  109. }
  110.  
  111. @end
RAW Paste Data