Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "MYCalculator.h"
- @implementation MYCalculator
- - (id) init {
- self = [super init];
- if (self) {
- operator = 0;
- currentVal = [[NSMutableString stringWithString: @"0"] retain];
- previousVal = [[NSMutableString stringWithString: @"0"] retain];
- }
- return self;
- }
- - (IBAction)butDigit:(id)sender {
- NSMutableString * str = (NSMutableString *)[sender currentTitle];
- if ([currentVal isEqualToString:@"0"])
- {
- currentVal = str;
- }
- else {
- currentVal = [currentVal stringByAppendingString:str];
- }
- [txtViewer setText:currentVal];
- }
- - (IBAction)butOperator:(id)sender {
- NSString *tmpStr = [currentVal substringFromIndex:([currentVal length] - 1)];
- if ([tmpStr isEqualToString:@"."]) {
- currentVal = (NSMutableString *)[currentVal substringToIndex:([currentVal length] - 1)];
- [txtViewer setText:currentVal];
- }
- NSMutableString* str = (NSMutableString *)[sender currentTitle];
- if (operator >= 1 && operator <= 4) {
- [self doEquals];
- }
- if (operator != 5) {
- previousVal = [currentVal copy];
- currentVal = @"0";
- }
- if ([str isEqualToString:@"+"]) {
- operator = 1;
- } else if ([str isEqualToString:@"-"]) {
- operator = 2;
- } else if ([str isEqualToString:@"x"]) {
- operator = 3;
- } else if ([str isEqualToString:@"/"]) {
- operator = 4;
- }
- }
- - (IBAction)butEquals:(id)sender {
- [self doEquals];
- }
- - (void)doEquals {
- if (operator >= 1 && operator <= 4) {
- NSDecimalNumber* num1 = 0;
- num1 = [NSDecimalNumber decimalNumberWithString:previousVal];
- NSDecimalNumber* num2 = 0;
- num2 = [NSDecimalNumber decimalNumberWithString:currentVal];
- if (operator == 1){
- num1 = [num1 decimalNumberByAdding:num2];
- currentVal = [NSMutableString stringWithString:[num1 stringValue]];
- } else if (operator == 2){
- num1 = [num1 decimalNumberBySubtracting:num2];
- currentVal = [NSMutableString stringWithString:[num1 stringValue]];
- } else if (operator == 3){
- num1 = [num1 decimalNumberByMultiplyingBy:num2];
- currentVal = [NSMutableString stringWithString:[num1 stringValue]];
- } else if (operator == 4){
- if (![currentVal isEqualToString:@"0"]) {
- num1 = [num1 decimalNumberByDividingBy:num2];
- currentVal = [NSMutableString stringWithString:[num1 stringValue]];
- }
- }
- [txtViewer setText:currentVal];
- previousVal = [currentVal copy];
- currentVal = @"0";
- operator = 5;
- }
- }
- - (IBAction)butDecimal:(id)sender {
- NSRange range = [currentVal rangeOfString:@"." options:(NSCaseInsensitiveSearch)];
- if (range.location == NSNotFound) {
- currentVal = [currentVal stringByAppendingString:@"."];
- }
- [txtViewer setText:currentVal];
- }
- - (IBAction)butClear:(id)sender {
- currentVal = @"0";
- previousVal = @"0";
- operator = 0;
- [txtViewer setText:currentVal];
- }
- - (IBAction)butAbout:(id)sender {
- //Show Alert
- UIAlertView *alert = [[UIAlertView alloc]
- initWithTitle:@"About iCalculator"
- message:@"iCalculator version 1.0\nAuthor Pasha L. Topchiyev"
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles: nil];
- [alert show];
- [alert release];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement