您的位置:首页 > 移动开发 > IOS开发

2015.12.29 iOS SimpleCalculator简单计算器

2015-12-29 21:20 453 查看
  这是开始写的第一个iOS程序。

1.首先创建一个SingleViewApplication界面(iOS中),这个模式就是默认为我们提供了一个界面。

      

#import "ViewController.h"

typedef enum{
kStatusNum,
kStatusOperation
}kStatus;

typedef enum{
kOperationTypeAdd = 1,
kOperationTypeMinus,
kOperationTypeMultiply,
kOperationTypeDevide,
kOperationTypeEqual,
kOperationTypeNone,
kOperationTypeSign = 11,
kOperationTypePercent,
kOperationTypePoint
}kOperationType;

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
@property (assign, nonatomic) double firstParameter;
@property (assign, nonatomic) kOperationType lastOperation, isOtherOperation;
@property (assign, nonatomic) kStatus status;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//赋初值
self.lastOperation = kOperationTypeNone;
self.status = kStatusNum;
self.isOtherOperation = kOperationTypeNone;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

//数字键
- (IBAction)numButtonDidClicked:(UIButton *)sender {

NSMutableString *showNum = [NSMutableString stringWithCapacity:0];
//进行判断,是输入数字并且上一次操作不是正负
if (self.status == kStatusNum && self.isOtherOperation != kOperationTypeSign) {
//进行显示的拼接
[showNum appendFormat:@"%@%@", self.resultLabel.text, sender.titleLabel.text];
//判断第一个字符是“0”切第二个字符不为“.”的情况
BOOL compareResultA, compareResultB;

compareResultA = [self comparationIsEqual:[showNum substringToIndex:1] anotherNSString:@"0"];
compareResultB = [self comparationIsEqual:[showNum substringWithRange:NSMakeRange(1, 1)] anotherNSString:@"."];
//这种情况需要删除开头的“0”
if (compareResultA == YES && compareResultB == NO) {
[showNum deleteCharactersInRange:NSMakeRange(0, 1)];
}

} else{
[showNum appendFormat:@"%@", sender.titleLabel.text];
self.status = kStatusNum;
self.isOtherOperation = kOperationTypeNone;
}

//显示结果
self.resultLabel.text = showNum;
}

- (BOOL)comparationIsEqual:(NSString *)StringA anotherNSString:(NSString *)StringB{
if ([StringA isEqualToString:StringB]) {
return YES;
} else{
return NO;
}
}

//+ - * / = 键
- (IBAction)operationButtonDidClicked:(UIButton *)sender {
if (self.status != kStatusOperation) {
self.status = kStatusOperation;

//有两种情况
//1.第一次按操作,只需要保存这次操作
if (self.lastOperation != kOperationTypeNone) {
//2.前面有操作需要计算,保存这次操作
[self calculate];
} else{
//第一个参数输入完毕 保存
self.firstParameter = [self.resultLabel.text doubleValue];
}
}

//保存这一次操作
if (sender.tag == kOperationTypeEqual) {
self.lastOperation = kOperationTypeNone;
} else{
self.lastOperation = (kOperationType)sender.tag;
}
}

//清零
- (IBAction)clearButtonDidClicked:(UIButton *)sender {
self.resultLabel.text = @"0";
self.firstParameter = 0;
self.lastOperation = kOperationTypeNone;
self.status = kStatusNum;
self.isOtherOperation = kOperationTypeNone;
}

//计算结果
- (void)calculate{
//获取第二个参数
double secondParameter = [self.resultLabel.text doubleValue];
double result;

switch (self.lastOperation) {
case kOperationTypeAdd:
result = self.firstParameter + secondParameter;
break;
case kOperationTypeMinus:
result = self.firstParameter - secondParameter;
break;
case kOperationTypeMultiply:
result = self.firstParameter * secondParameter;
break;
case kOperationTypeDevide:
result = self.firstParameter / secondParameter;
break;
default:
break;
}
//显示最终结果
self.resultLabel.text = [NSString stringWithFormat:@"%g", result];

//当前结果就是写一次的参数值
self.firstParameter = result;

//更改操作符
self.lastOperation = kOperationTypeNone;
}

//± % 键
- (IBAction)otherOperationButtonDidClicked:(UIButton *)sender {
double temp;
//判断按键
switch (sender.tag) {
case kOperationTypeSign:
temp = [self.resultLabel.text doubleValue];
self.resultLabel.text = [NSString stringWithFormat:@"%g", temp * (-1)];
self.firstParameter = [self.resultLabel.text doubleValue];
self.isOtherOperation = kOperationTypeSign;
break;
case kOperationTypePercent:
temp = [self.resultLabel.text doubleValue];
self.resultLabel.text = [NSString stringWithFormat:@"%g", temp / 100];

break;
default:
break;
}
}

//. 键
- (IBAction)pointButtonDidClicked:(UIButton *)sender {
if (self.isOtherOperation != kOperationTypePoint) {
self.resultLabel.text = [NSString stringWithFormat:@"%@.", self.resultLabel.text];
self.isOtherOperation = kOperationTypePoint;
}

}

@end


SimpleCalculator

请教一下各位大神,小数点的问题有什么更好地想法没有,我觉得自己的解决方案有些繁琐,并不是最简便的。谢谢。Orz~

      
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: