步驟:
1.打開Xcode,單機Creat a new Xcode project
2.左邊選擇ios下Application,右邊選擇single view Application
3.填寫項目名稱單機Next
4.ViewController.h中定義成員和方法
// // ViewController.h // TestCalculator4 // // Created by heyonggang on 13-11-28. // Copyright (c) 2013年 MiracleHe. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property(retain,nonatomic) UIButton *button; @property(retain,nonatomic) UILabel *label; @property(retain,nonatomic) NSMutableString *string; //NSMutableString用來處理可變對象,如需要處理字符串並更改字符串中的字符 @property(assign,nonatomic) double num1,num2; @property(assign,nonatomic) NSString *str; @end
5.ViewController.m
// // ViewController.m // TestCalculator4 // // Created by heyonggang on 13-11-28. // Copyright (c) 2013年 MiracleHe. All rights reserved. // #import "ViewController.h" #import <QuartzCore/QuartzCore.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //創建標簽 self.label=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 200, 50)]; [self.view addSubview:_label]; self.label.backgroundColor=[UIColor greenColor]; //設置背景顏色 self.label.textColor=[UIColor blackColor]; //字體顏色 //self.label.textAlignment = UITextAlignmentRight; //字體居右 self.label.textAlignment = NSTextAlignmentCenter; self.label.font=[UIFont systemFontOfSize:32.4]; //設置字體 //添加1-9數字 NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]; int n=0; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; self.button.frame=CGRectMake(30+65*j, 150+65*i, 60, 60); [self.button setTitle:[array objectAtIndex:n++] forState:UIControlStateNormal]; //注意:[array objectAtIndex:n++] [self.view addSubview:_button]; [self.button addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside]; //addTarget:self 的意思是說,這個方法在本類中也可以傳入其他類的指針 } } //單獨添加0 UIButton *button0=[UIButton buttonWithType:UIButtonTypeCustom]; //創建一個圓角矩形的按鈕 /* CALayer *layer = button0.layer; [layer setMasksToBounds:YES]; [layer setCornerRadius:5.0]; button0.layer.borderColor = [UIColor lightGrayColor].CGColor; button0.layer.borderWidth = 1.0f; */ [button0 setFrame:CGRectMake(30, 345, 60, 60)]; //設置button在view上的位置 //也可以這樣用:button0.frame:CGRectMake(30, 345, 60, 60); [button0 setTitle:@"0" forState:UIControlStateNormal]; //設置button主題 button0.titleLabel.textColor = [UIColor blackColor]; //設置0鍵的顏色 [button0 addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside]; //按下按鈕,並且當手指離開離開屏幕的時候觸發這個事件 //觸發了這個事件后,執行shuzi方法,action:@selector(shuzi:) [self.view addSubview:button0]; //顯示控件 //添加運算符 NSArray *array1=[NSArray arrayWithObjects:@"+",@"-",@"*",@"/",nil]; for (int i=0; i<4; i++) { UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [button1 setFrame:CGRectMake(225, 150+65*i, 60, 60)]; [button1 setTitle:[array1 objectAtIndex:i] forState:UIControlStateNormal]; //[array1 objectAtIndex:i]為獲取按鈕的屬性值 [self.view addSubview:button1]; [button1 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside]; } //添加= UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [button2 setFrame:CGRectMake(160, 410, 125, 35)]; [button2 setTitle:@"=" forState:UIControlStateNormal]; [button2 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button2]; //添加清除鍵 UIButton *button3=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [button3 setFrame:CGRectMake(30, 410, 125, 35)]; [button3 setTitle:@"AC" forState:UIControlStateNormal]; [button3 addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button3]; //添加. UIButton *button4=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [button4 setFrame:CGRectMake(95, 345, 60, 60)]; [button4 setTitle:@"." forState:UIControlStateNormal]; [button4 addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button4]; //后退 UIButton *button5=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [button5 setFrame:CGRectMake(160, 345, 60, 60)]; [button5 setTitle:@"back" forState:UIControlStateNormal]; [button5 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button5]; self.string=[[NSMutableString alloc]init];//初始化可變字符串,分配內存 self.str = [[NSString alloc]init]; // Do any additional setup after loading the view, typically from a nib. } //0-9方法 - (void)shuzi:(id)sender { [self.string appendString:[sender currentTitle]]; //數字連續輸入 self.label.text=[NSString stringWithString:_string]; //顯示數值 self.num1=[self.label.text doubleValue]; //保存輸入的數值 NSLog(@"self.num1 is %f",self.num1); } //計算方法 -(void)go:(id)sender { if ([self.str isEqualToString:@""])//當str里為空 { self.num2=self.num1; NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithString:_string]; //只要是符號就顯示數值 [self.string setString:@""]; //字符串清零 self.str=[sender currentTitle]; //保存運算符為了作判斷作何種運算 NSLog(@"%@",_str); [self.string appendString:self.str]; self.label.text=[NSString stringWithString:_string]; //顯示數值 [self.string setString:@""]; //字符串清零 } else { //輸出上次計算結果 if ([self.str isEqualToString:@"+"])//之前的符號是+ { [self.string setString:@""];//字符串清零 self.num2+=self.num1;//num2是運算符號左邊的數值,還是計算結果 //輸出上次結果后判斷這次輸入的是何符號 if ([[sender currentTitle]isEqualToString:@"="]) { NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithFormat:@"%f",self.num2]; self.str=@""; } else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"]) { NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithFormat:@"%f",self.num2]; [self.string setString:@""]; //字符串清零 self.str=[sender currentTitle];//保存運算符為了作判斷作何種運算 NSLog(@"%@",_str); [self.string appendString:self.str]; [self.string setString:@""];//字符串清零 } } else if ([self.str isEqualToString:@"-"])//之前的符號是- { [self.string setString:@""];//字符串清零 self.num2-=self.num1; //輸出上次結果后判斷這次輸入的是何符號 if ([[sender currentTitle]isEqualToString:@"="]) { NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithFormat:@"%f",self.num2]; self.str=@""; } else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"]) { NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithFormat:@"%f",self.num2]; [self.string setString:@""];//字符串清零 self.str=[sender currentTitle];//保存運算符為了作判斷作何種運算 NSLog(@"%@",_str); [self.string appendString:self.str]; [self.string setString:@""];//字符串清零 } } else if([self.str hasPrefix:@"*"])//之前的符號是* hasPrefix:方法的功能是判斷創建的字符串內容是否以某個字符開始 { [self.string setString:@""];//字符串清零 self.num2*=self.num1; //輸出上次結果后判斷這次輸入的是何符號 if ([[sender currentTitle] isEqualToString:@"="]) { NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithFormat:@"%f",self.num2]; self.str=@""; } else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"]) { NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithFormat:@"%f",self.num2]; [self.string setString:@""]; //字符串清零 self.str=[sender currentTitle]; //保存運算符為了作判斷作何種運算 NSLog(@"%@",_str); [self.string appendString:self.str]; //在字符串后增加新的東西,[a appendString:] [self.string setString:@""]; //字符串清零 } } else if ([self.str isEqualToString:@"/"])//之前的符號是/ { [self.string setString:@""];//字符串清零 self.num2/=self.num1; //判斷輸出上次結果后判斷這次輸入的是何符號 if ([[sender currentTitle]isEqualToString:@"="]) { NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithFormat:@"%f",self.num2]; self.str=@""; } else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"]) { NSLog(@"self.num2 is %f",self.num2); self.label.text=[NSString stringWithFormat:@"%f",self.num2]; [self.string setString:@""];//字符串清零 self.str=[sender currentTitle];//保存運算符為了作判斷作何種運算 NSLog(@"%@",_str); [self.string appendString:self.str]; [self.string setString:@""];//字符串清零 } } } } //當按下清除建時,所有數據清零 -(void)clean:(id)sender { [self.string setString:@""];//清空字符 self.num1=0; self.num2=0; self.label.text=@"0";//保證下次輸入時清零 } //返回鍵 -(void)back:(id)sender { if (![self.label.text isEqualToString:@""])//判斷不是空 { [self.string deleteCharactersInRange:NSMakeRange ([self.string length]-1,1)];//刪除最后一個字符 self.label.text=[NSString stringWithString:_string];//顯示結果 } } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
