#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
#import "CXViewController.h"
enum {
plusOperation = 101,
minusOperation = 102,
multiplyOperation = 103,
divisionOperation = 104,
percentPlusOperation = 106,
percentMinusOperation = 107,
degreeOperation = 108,
radicalOperation = 109,
floatingPointOperation = 111
};
@interface CXViewController ()
@end
@implementation CXViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)clearButton:(id)sender {
x = 0;
y = 0;
enterFlag = NO;
yFlag = NO;
[self calcScreen];
}
- (IBAction)digit:(id)sender {
if (enterFlag) {
y = x;
x = 0;
enterFlag = NO;
}
x = (10 * x) + [sender tag];
[self calcScreen];
}
- (IBAction)constPi:(id)sender {
x = 3.14;
[self calcScreen];
}
- (IBAction)waitingOperation:(id)sender {
if (yFlag && !enterFlag) {
switch (operation) {
case plusOperation:
x = y + x;
break;
case minusOperation:
x = y - x;
break;
case multiplyOperation:
x = y * x;
break;
case divisionOperation:
x = y / x;
break;
case degreeOperation:
x = pow(y, x);
break;
// вот тут проблема
case radicalOperation:
x = sqrt(x);
break;
default:
break;
}
}
y = x;
enterFlag = YES;
yFlag = YES;
operation = [sender tag];
[self calcScreen];
}
- (void)calcScreen {
NSString *calculatorStr = [NSString stringWithFormat:@"%g", x];
[displayLabel setText:calculatorStr];
}
@end
#import "CXViewController.h"
enum {
plusOperation = 101,
minusOperation = 102,
multiplyOperation = 103,
divisionOperation = 104,
percentPlusOperation = 106,
percentMinusOperation = 107,
degreeOperation = 108,
radicalOperation = 109
};
@interface CXViewController ()
@end
@implementation CXViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)clearButton:(id)sender {
x = 0;
y = 0;
enterFlag = NO;
yFlag = NO;
[self calcScreen];
}
- (IBAction)digit:(id)sender {
if (enterFlag) {
y = x;
x = 0;
enterFlag = NO;
}
x = (10 * x) + [sender tag];
[self calcScreen];
}
- (IBAction)operation:(id)sender {
if (yFlag && !enterFlag) {
switch (operation) {
case plusOperation:
x = y + x;
break;
case minusOperation:
x = y - x;
break;
case multiplyOperation:
x = y * x;
break;
case divisionOperation:
x = y / x;
break;
case degreeOperation:
x = pow(y, x);
break;
// вот тут проблема
case radicalOperation:
x = sqrt(x);
break;
default:
break;
}
}
y = x;
enterFlag = YES;
yFlag = YES;
operation = [sender tag];
[self calcScreen];
}
- (void)calcScreen {
NSString *str = [NSString stringWithFormat:@"%g", x];
[displayLabel setText:str];
}