//// ViewController.m// 計算器//屏幕的寬和高#define SCREEN_W self.view.frame.size.width#define SCREEN_H self.view.frame.size.height#import "ViewController.h"@i...
//
// ViewController.m
// 計算器
//屏幕的寬和高
#define SCREEN_W self.view.frame.size.width
#define SCREEN_H self.view.frame.size.height
#import "ViewController.h"
@interface ViewController ()
//用於存儲輸入的第一個數字
@property (nonatomic,assign) CGFloat num1;
//用於存儲輸入的第二個數字
@property (nonatomic,assign) CGFloat num2;
//用於存儲最終結果
@property (nonatomic,assign) CGFloat numResult;
//用於判斷符號的標記
@property (nonatomic,assign) NSInteger tag;
//顯示屏
@property (nonatomic,strong) UILabel *screen;
//
@property (nonatomic,strong) NSMutableString *str;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//開闢空間
_str = [[NSMutableString alloc]init];
// Do any additional setup after loading the view, typically from a nib.
#pragma mark - screen
//先創建一個標簽,用於顯示
_screen = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-400-5*10)];
//設置屏幕的字體顏色
[_screen setTextColor:[UIColor whiteColor]];
//設置屏幕的背景顏色
_screen.backgroundColor = [UIColor blackColor];
//設置屏幕的字體大小
_screen.font = [UIFont systemFontOfSize:40];
//設置屏幕的字體自適應
_screen.adjustsFontSizeToFitWidth = YES;
//設置字體的對齊方式
_screen.textAlignment = NSTextAlignmentRight;
//清零
_screen.text = [NSMutableString stringWithString:@"0"];
[self.view addSubview:_screen];
#pragma mark - buttonwith+_*/
NSArray *signArr = @[@"÷",@"x",@"-",@"+"];
for (int i = 0; i < [signArr count]; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//設置按鈕的文字
[btn setTitle:signArr[i] forState:UIControlStateNormal];
//設置按鈕的位置和大小
btn.frame = CGRectMake(((SCREEN_W-30)/4.0)*3.0+30, _screen.frame.size.height+90*(i%4)+10, (SCREEN_W-30)/4.0, 80);
//設置按鈕的背景顏色
btn.backgroundColor = [UIColor orangeColor];
//設置按鈕字體的大小
btn.titleLabel.font = [UIFont systemFontOfSize:40];
//給每個按鈕添加事件
[btn addTarget:self action:@selector(calculate:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
#pragma mark - 單獨添加“=”
UIButton *goBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//設置按鈕的大小和位置
goBtn.frame = CGRectMake(((SCREEN_W-30)/4.0)*3.0+30, _screen.frame.size.height+90*4+10, (SCREEN_W-30)/4.0, 80);
//設置按鈕的背景顏色
goBtn.backgroundColor = [UIColor orangeColor];
//設置按鈕的文字
[goBtn setTitle:@"=" forState:UIControlStateNormal];
//設置文字的大小
goBtn.titleLabel.font = [UIFont systemFontOfSize:40];
//為每個按鈕添加事件
[goBtn addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goBtn];
#pragma mark - number
NSArray *numArr = @[@"7",@"8",@"9",@"4",@"5",@"6",@"1",@"2",@"3"];
for (int i = 0 ; i < [numArr count]; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//設置按鈕的大小和位置
btn.frame = CGRectMake(((SCREEN_W-30)/4.0+10)*(i%3), _screen.frame.size.height+90*(i/3)+90+10, (SCREEN_W-30)/4.0, 80);
//設置按鈕的背景顏色
btn.backgroundColor = [UIColor lightGrayColor];
//設置按鈕的文字
[btn setTitle:numArr[i] forState:UIControlStateNormal];
//設置文字的大小
btn.titleLabel.font = [UIFont systemFontOfSize:40];
//設置文字的顏色
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//為每個按鈕添加事件
[btn addTarget:self action:@selector(expressNum:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
#pragma mark - 添加清除鍵
UIButton *cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//設置按鈕的大小和位置
cleanBtn.frame = CGRectMake(0, _screen.frame.size.height+10, (SCREEN_W-30)/4.0, 80);
//設置按鈕的背景顏色
cleanBtn.backgroundColor = [UIColor lightGrayColor];
//設置按鈕的文字
[cleanBtn setTitle:@"AC" forState:UIControlStateNormal];
//設置文字的大小
cleanBtn.titleLabel.font = [UIFont systemFontOfSize:40];
//設置文字的顏色
[cleanBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//為每個按鈕添加事件
[cleanBtn addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cleanBtn];
#pragma mark - 單獨添加0
UIButton *btn0 = [UIButton buttonWithType:UIButtonTypeCustom];
//設置按鈕的大小和位置
btn0.frame = CGRectMake(0, SCREEN_H - 80, (SCREEN_W-30)/4.0*2+10, 80);
//設置按鈕的背景顏色
btn0.backgroundColor = [UIColor lightGrayColor];
//設置按鈕的文字
[btn0 setTitle:@"0" forState:UIControlStateNormal];
//設置文字的大小
btn0.titleLabel.font = [UIFont systemFontOfSize:40];
//設置文字的顏色
[btn0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//為每個按鈕添加事件
[btn0 addTarget:self action:@selector(expressNum:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn0];
#pragma mark - 單獨添加百分號%
UIButton *btnPrecent = [UIButton buttonWithType:UIButtonTypeCustom];
//設置按鈕的大小和位置
btnPrecent.frame = CGRectMake(((SCREEN_W-30)/4.0+10)*2, _screen.frame.size.height+10, (SCREEN_W-30)/4.0, 80);
//設置按鈕的背景顏色
btnPrecent.backgroundColor = [UIColor lightGrayColor];
//設置按鈕的文字
[btnPrecent setTitle:@"%" forState:UIControlStateNormal];
//設置文字的大小
btnPrecent.titleLabel.font = [UIFont systemFontOfSize:40];
//設置文字的顏色
[btnPrecent setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//為每個按鈕添加事件
[btnPrecent addTarget:self action:@selector(precent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnPrecent];
#pragma mark - 單獨添加點"."
UIButton *btnPoint = [UIButton buttonWithType:UIButtonTypeCustom];
//設置按鈕的大小和位置
btnPoint.frame = CGRectMake(btn0.frame.size.width+10, SCREEN_H-80, (SCREEN_W-30)/4.0, 80);
//設置按鈕的背景顏色
btnPoint.backgroundColor = [UIColor lightGrayColor];
//設置按鈕的文字
[btnPoint setTitle:@"." forState:UIControlStateNormal];
//設置文字的大小
btnPoint.titleLabel.font = [UIFont systemFontOfSize:40];
//設置文字的顏色
[btnPoint setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//為每個按鈕添加事件
[btnPoint addTarget:self action:@selector(expressNum:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnPoint];
#pragma mark - 單獨添加點"+/-"
UIButton *btnSign = [UIButton buttonWithType:UIButtonTypeCustom];
//設置按鈕的大小和位置
btnSign.frame = CGRectMake(cleanBtn.frame.size.width+10, _screen.frame.size.height+10, (SCREEN_W-30)/4.0, 80);
//設置按鈕的背景顏色
btnSign.backgroundColor = [UIColor lightGrayColor];
//設置按鈕的文字
[btnSign setTitle:@"+/-" forState:UIControlStateNormal];
//設置文字的大小
btnSign.titleLabel.font = [UIFont systemFontOfSize:40];
//設置文字的顏色
[btnSign setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//為每個按鈕添加事件
[btnSign addTarget:self action:@selector(signNum:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnSign];
}
-(void) expressNum:(UIButton *)sender
{
//先判斷首字母是不是符號
if ([_str hasPrefix:@"+"]||[_str hasPrefix:@"-"]||[_str hasPrefix:@"x"]||[_str hasPrefix:@"÷"]) {
//如果是,就清零
_str = [NSMutableString stringWithString:@""];
}
//連續追加數字
[_str appendString:sender.currentTitle];
_screen.text = [NSString stringWithString:_str];
//將輸入的第一個數字保存在num1中
_num1 = [_screen.text doubleValue];
//用於調試
//NSLog(@"-----%ld",_num1);
}
//判斷符號
-(void)calculate:(UIButton *)sender
{
//先清零
_str = [NSMutableString stringWithString:@""];
//再重新追加符號
[_str appendString:sender.currentTitle];
//顯示(這個可寫可不寫)
_screen.text = [NSString stringWithString:_str];
if ([_str hasPrefix:@"+"]) {
_num2 = _num1;
_tag = 1;
}else if ([_str hasPrefix:@"-"]){
_num2 = _num1;
_tag = 2;
}else if ([_str hasPrefix:@"x"]) {
_num2 = _num1;
_tag = 3;
}else if ([_str hasPrefix:@"÷"]) {
_num2 = _num1;
_tag = 4;
}
}
//計算結果
-(void)go:(UIButton *)sender
{
switch (_tag) {
case 1:
_numResult = _num2 + _num1;
break;
case 2:
_numResult = _num2 - _num1;
break;
case 3:
_numResult = _num2 * _num1;
break;
case 4:
_numResult = _num2 / _num1;
break;
}
_screen.text = [NSString stringWithFormat:@"%lg",_numResult];
_num1 = _numResult;
}
//清除
-(void)clean:(UIButton *)sender
{
_str = [NSMutableString stringWithString:@""];
_screen.text = @"0";
_num1 = 0;
_num2 = 0;
_numResult = 0;
}
//百分號
-(void)precent:(UIButton *)sender
{
_num1 = _num1 * 0.01;
//註意,這裡要以最簡形式(占用寬度最小)輸出,並且不會輸出無意義的0
_screen.text = [NSString stringWithFormat:@"%lg",_num1];
}
//正負號
-(void)signNum:(UIButton *)sender
{
//先判斷數字是否大於0,如果是,就變負
if (_num1 >= 0) {
//添加負號
[_str insertString:@"-" atIndex:0];
_num1 = [_str doubleValue];
_screen.text = [NSString stringWithFormat:@"%lg",_num1];
}
//如果數字小於0,那麼變為正數
else if (_num1 < 0)
{
//刪除負號
[_str deleteCharactersInRange:NSMakeRange(0, 1)];
_num1 = [_str doubleValue];
_screen.text = [NSString stringWithFormat:@"%lg",_num1];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//大家看了之後覺得哪裡代碼可以更加優化,或者功能上有什麼補充的,麻煩評論下,感謝了