Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
#import "SMViewController.h" @interface SMViewController () { NSNumber *x, *y; NSString *operation; } @property (weak, nonatomic) IBOutlet UILabel *display; @end @implementation SMViewController - (void)viewDidLoad { [super viewDidLoad]; [self.display setText:@"0"]; } - (IBAction)digitButton:(id)sender { NSString *stringDigit = self.display.text; if ([operation isEqualToString:@"+"] || [operation isEqualToString:@"-"] || [operation isEqualToString:@"x"] || [operation isEqualToString:@"÷"]) { operation = stringDigit; } else if ([operation isEqualToString:@"="]) { NSArray *resultArr = [self.display.text componentsSeparatedByString:operation]; if ([resultArr count] == 2) { x = [NSNumber numberWithFloat:[resultArr [0] floatValue]]; y = [NSNumber numberWithFloat:[resultArr [1] floatValue]]; [self goOperation]; return; } else { [self.display setText:@"error"]; [self performSelector:@selector(clearDisplay) withObject:nil afterDelay:1]; } } // else // Get digit on display { if ([self.display.text isEqualToString:@"0"]) { [self.display setText:@""]; } NSString *getDigitOnDisplay = [self.display.text stringByAppendingString:stringDigit]; [self.display setText:getDigitOnDisplay]; } } - (void)goOperation { if (x > 0 || x < 0 || y > 0 || y < 0) { NSString *result = @""; if ([operation isEqualToString:@"+"]) { NSNumber *resultNumber = [NSNumber numberWithFloat:[x floatValue] + [y floatValue]]; result = [NSString stringWithFormat:@"%@", resultNumber]; } else if ([operation isEqualToString:@"-"]) { NSNumber *resultNumber = [NSNumber numberWithFloat:[x floatValue] - [y floatValue]]; result = [NSString stringWithFormat:@"%@", resultNumber]; } else if ([operation isEqualToString:@"x"]) { NSNumber *resultNumber = [NSNumber numberWithFloat:[x floatValue] * [y floatValue]]; result = [NSString stringWithFormat:@"%@", resultNumber]; } else if ([operation isEqualToString:@"÷"]) { NSNumber *resultNumber = [NSNumber numberWithFloat:[x floatValue] / [y floatValue]]; result = [NSString stringWithFormat:@"%@", resultNumber]; } else if ([operation isEqualToString:@"+%"]) { NSNumber *resultNumber = [NSNumber numberWithFloat:[x floatValue] + [y floatValue] / 100 * [x floatValue]]; result = [NSString stringWithFormat:@"%@", resultNumber]; } else if ([operation isEqualToString:@"-%"]) { NSNumber *resultNumber = [NSNumber numberWithFloat:[x floatValue] - [y floatValue] / 100 * [x floatValue]]; result = [NSString stringWithFormat:@"%@", resultNumber]; } else if ([operation isEqualToString:@"."]) { result = [NSString stringWithFormat:@"."]; } [self.display setText:result]; } } - (void)clearDisplay { [self.display setText:@"0"]; } - (IBAction)clearDisplayButton:(id)sender { [self.display setText:@"0"]; } - (IBAction)clearLastElementFromDisplay:(id)sender { NSString *clearLastElement = self.display.text; clearLastElement = [clearLastElement substringToIndex:[clearLastElement length] - 1]; [self.display setText:clearLastElement]; } @end