#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.strNum = [NSMutableString string];
self.intNum = 0;
self.arr = 0;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(0, 405, 160, 75);
button1.tag = 10;
button1.backgroundColor = [UIColor blackColor];
[button1 setTitle:@"0⃣️" forState:UIControlStateNormal];
[button1 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.frame = CGRectMake(160, 405, 80, 75);
button2.tag = 11;
button2.backgroundColor = [UIColor blackColor];
[button2 setTitle:@"." forState:UIControlStateNormal];
[button2 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
button3.frame = CGRectMake(240, 405, 80, 75);
button3.tag = 12;
button3.backgroundColor = [UIColor orangeColor];
[button3 setTitle:@"=" forState:UIControlStateNormal];
[button3 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button3];
[button3 addTarget:self action:@selector(buttonResult:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];
button4.frame = CGRectMake(0, 330, 80, 75);
button4.tag = 1;
button4.backgroundColor = [UIColor blackColor];
[button4 setTitle:@"1⃣️" forState:UIControlStateNormal];
[button4 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button4];
[button4 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button5 = [UIButton buttonWithType:UIButtonTypeSystem];
button5.frame = CGRectMake(80, 330, 80, 75);
button5.tag = 2;
button5.backgroundColor = [UIColor blackColor];
[button5 setTitle:@"2⃣️" forState:UIControlStateNormal];
[button5 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button5];
[button5 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button6 = [UIButton buttonWithType:UIButtonTypeSystem];
button6.frame = CGRectMake(160, 330, 80, 75);
button6.tag = 3;
button6.backgroundColor = [UIColor blackColor];
[button6 setTitle:@"3⃣️" forState:UIControlStateNormal];
[button6 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button6];
[button6 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button7 = [UIButton buttonWithType:UIButtonTypeSystem];
button7.frame = CGRectMake(240, 330, 80, 75);
button7.tag = 13;
button7.backgroundColor = [UIColor blackColor];
[button7 setTitle:@"➕" forState:UIControlStateNormal];
[button7 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button7];
[button7 addTarget:self action:@selector(buttonArith:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button8 = [UIButton buttonWithType:UIButtonTypeSystem];
button8.frame = CGRectMake(0, 255, 80, 75);
button8.tag = 4;
button8.backgroundColor = [UIColor blackColor];
[button8 setTitle:@"4⃣️" forState:UIControlStateNormal];
[button8 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button8];
[button8 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button9 = [UIButton buttonWithType:UIButtonTypeSystem];
button9.frame = CGRectMake(80, 255, 80, 75);
button9.tag = 5;
button9.backgroundColor = [UIColor blackColor];
[button9 setTitle:@"5⃣️" forState:UIControlStateNormal];
[button9 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button9];
[button9 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button10 = [UIButton buttonWithType:UIButtonTypeSystem];
button10.frame = CGRectMake(160, 255, 80, 75);
button10.tag = 6;
button10.backgroundColor = [UIColor blackColor];
[button10 setTitle:@"6⃣️" forState:UIControlStateNormal];
[button10 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button10];
[button10 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button11 = [UIButton buttonWithType:UIButtonTypeSystem];
button11.frame = CGRectMake(240, 255, 80, 75);
button11.tag = 14;
button11.backgroundColor = [UIColor blackColor];
[button11 setTitle:@"➖" forState:UIControlStateNormal];
[button11 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button11];
[button11 addTarget:self action:@selector(buttonArith:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button12 = [UIButton buttonWithType:UIButtonTypeSystem];
button12.frame = CGRectMake(0, 180, 80, 75);
button12.tag = 7;
button12.backgroundColor = [UIColor blackColor];
[button12 setTitle:@"7⃣️" forState:UIControlStateNormal];
[button12 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button12];
[button12 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button13 = [UIButton buttonWithType:UIButtonTypeSystem];
button13.frame = CGRectMake(80, 180, 80, 75);
button13.tag = 8;
button13.backgroundColor = [UIColor blackColor];
[button13 setTitle:@"8⃣️" forState:UIControlStateNormal];
[button13 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button13];
[button13 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button14 = [UIButton buttonWithType:UIButtonTypeSystem];
button14.frame = CGRectMake(160, 180, 80, 75);
button14.tag = 9;
button14.backgroundColor = [UIColor blackColor];
[button14 setTitle:@"9⃣️" forState:UIControlStateNormal];
[button14 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button14];
[button14 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button15 = [UIButton buttonWithType:UIButtonTypeSystem];
button15.frame = CGRectMake(240, 180, 80, 75);
button15.tag = 15;
button15.backgroundColor = [UIColor blackColor];
[button15 setTitle:@"✖️" forState:UIControlStateNormal];
[button15 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button15];
[button15 addTarget:self action:@selector(buttonArith:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button16 = [UIButton buttonWithType:UIButtonTypeSystem];
button16.frame = CGRectMake(0, 105, 80, 75);
button16.backgroundColor = [UIColor blackColor];
[button16 setTitle:@"AC" forState:UIControlStateNormal];
[button16 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button16];
UIButton *button17 = [UIButton buttonWithType:UIButtonTypeSystem];
button17.frame = CGRectMake(80, 105, 80, 75);
button17.backgroundColor = [UIColor blackColor];
[button17 setTitle:@"+/-" forState:UIControlStateNormal];
[button17 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button17];
UIButton *button18 = [UIButton buttonWithType:UIButtonTypeSystem];
button18.frame = CGRectMake(160, 105, 80, 75);
button18.backgroundColor = [UIColor blackColor];
[button18 setTitle:@"%" forState:UIControlStateNormal];
[button18 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button18];
UIButton *button19 = [UIButton buttonWithType:UIButtonTypeSystem];
button19.frame = CGRectMake(240, 105, 80, 75);
button19.tag = 16;
button19.backgroundColor = [UIColor blackColor];
[button19 setTitle:@"➗" forState:UIControlStateNormal];
[button19 setTintColor:[UIColor whiteColor]];
[self.view addSubview:button19];
[button19 addTarget:self action:@selector(buttonArith:) forControlEvents:UIControlEventTouchUpInside];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 300, 80)];
label.backgroundColor = [UIColor blackColor];
label.tag = 100;
[self.view addSubview:label];
}
// 点击数字
- (void)button:(UIButton *)number
{
NSNumber *nsNum = [NSNumber numberWithInt:number.tag];
NSMutableString *num = [NSMutableString stringWithFormat:@"%@",nsNum];
[self.strNum appendString:num];
UILabel *view = (UILabel *)[self.view viewWithTag:100];
view.text = self.strNum;
[view setTextColor:[UIColor redColor]];
}
// 点击+-* / 运算符方法
- (void)buttonArith:(UIButton *)arith
{
self.intNum = [self.strNum integerValue];
if (13 == arith.tag) {
self.strNum = [NSMutableString string];
UILabel *view = (UILabel *)[self.view viewWithTag:100];
view.text = self.strNum;
self.arr = 1;
} if (14 == arith.tag) {
self.strNum = [NSMutableString string];
UILabel *view = (UILabel *)[self.view viewWithTag:100];
view.text = self.strNum;
self.arr = 2;
} if (15 == arith.tag) {
self.strNum = [NSMutableString string];
UILabel *view = (UILabel *)[self.view viewWithTag:100];
view.text = self.strNum;
self.arr = 3;
} if (16 == arith.tag) {
self.strNum = [NSMutableString string];
UILabel *view = (UILabel *)[self.view viewWithTag:100];
view.text = self.strNum;
self.arr = 4;
}
}
// 点击=方法
- (void)buttonResult:(UIButton *)result
{
int sum1 = self.intNum;
int sum2 = [self.strNum integerValue];
if (1 == self.arr) {
int sum = sum1 + sum2;
NSMutableString *s = [NSMutableString stringWithFormat:@"%d", sum];
self.strNum = [NSMutableString stringWithFormat:@"%@",s];
} if (2 == self.arr) {
int sum = sum1 - sum2;
NSMutableString *s = [NSMutableString stringWithFormat:@"%d", sum];
self.strNum = [NSMutableString stringWithFormat:@"%@",s];
} if (3 == self.arr) {
int sum = sum1 * sum2;
NSMutableString *s = [NSMutableString stringWithFormat:@"%d", sum];
self.strNum = [NSMutableString stringWithFormat:@"%@",s];
} if (4 == self.arr) {
int sum = sum1 / sum2;
NSMutableString *s = [NSMutableString stringWithFormat:@"%d", sum];
self.strNum = [NSMutableString stringWithFormat:@"%@",s];
}
UILabel *view = (UILabel *)[self.view viewWithTag:100];
view.text = self.strNum;
}
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@property (nonatomic, retain) NSMutableString *strNum;
@property (nonatomic, assign) NSInteger intNum;
@property (nonatomic, assign) NSInteger arr;
@end