您好,登錄后才能下訂單哦!
類和文件
AppDelegate.m
#import "AppDelegate.h" #import "CalculatorController.h" //#import "ViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; CalculatorController *calculator = [[CalculatorController alloc] init]; self.window.rootViewController = calculator; [calculator release]; // ViewController *view = [[ViewController alloc]init]; // self.window.rootViewController = view; [_window release]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
CalculatorController.h
#import <UIKit/UIKit.h> @interface CalculatorController : UIViewController //@property (nonatomic , retain)UILabel *label; //@property (nonatomic , assign)NSInteger firstNumber; ////存儲當前選擇的符號(1:+ 2:- 3:* 4:/) //@property (nonatomic , assign)NSInteger sumbol; ////是不是剛剛點擊過符號鍵 //@property (nonatomic , assign)BOOL isSymbol; @property(retain,nonatomic)UIButton *button; @property(retain,nonatomic)UILabel *label; @property(nonatomic,retain)UILabel *label1; @property(retain,nonatomic)NSMutableString *string; @property(assign,nonatomic)double num1,num2,num3,num4; @end
CalculatorController.m
#import "CalculatorController.h" @interface CalculatorController () @end @implementation CalculatorController @synthesize button,label,label1,string,num1,num2,num3,num4;//string保存字符,顯示數值。num1是存輸入的數值,num2是存運算符前的數值,num3是運算結果,num4是判斷進行何種運算 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.view.alpha = 0.3; // Do any additional setup after loading the view. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 280, 330)]; view.layer.cornerRadius = 15; view.alpha = 0.8; view.backgroundColor = [UIColor brownColor]; [self.view addSubview:view]; [view release]; self.label=[[UILabel alloc]initWithFrame:CGRectMake(40 , 75, 240, 30)]; self.label.backgroundColor=[UIColor blackColor];//清空背景顏色 self.label.textColor=[UIColor whiteColor];//字體顏色 self.label.textAlignment = NSTextAlignmentRight;;//字體居右 self.label.font=[UIFont systemFontOfSize:20]; [self.view addSubview:label]; self.label1=[[UILabel alloc]initWithFrame:CGRectMake(40 , 60, 240, 15)]; self.label1.text = @""; self.label1.backgroundColor=[UIColor blackColor];//清空背景顏色 self.label1.textColor=[UIColor whiteColor];//字體顏色 self.label1.textAlignment = NSTextAlignmentRight;;//字體居右 self.label1.font=[UIFont systemFontOfSize:15]; [self.view addSubview:self.label1]; // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20 , 20, 255, 35)]; // label.text = @""; // label.textColor = [UIColor yellowColor]; // label.font = [UIFont systemFontOfSize:18]; // label.textAlignment = NSTextAlignmentCenter; // [self.view addSubview:label]; // [label release]; // UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30 , 70, 255, 35)]; // textField.layer.borderWidth = 1; // textField.layer.cornerRadius = 10; // textField.backgroundColor = [UIColor whiteColor]; //// textField.alpha = 0.2; // [self.view addSubview:textField]; // [textField release]; UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(30, 110, 260, 260)]; view1.layer.cornerRadius = 15; // view1.alpha = 0.8; view1.backgroundColor = [UIColor blackColor]; [self.view addSubview:view1]; [view1 release]; //C UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 55, 40)]; [button1 setTitle:@"C" forState:UIControlStateNormal]; [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button1 setShowsTouchWhenHighlighted:YES]; button1.backgroundColor = [UIColor orangeColor]; // button1.alpha = 0.5; button1.layer.cornerRadius = 8; [button1 addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button1]; [button1 release]; //DEL UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem]; button2.frame = CGRectMake(70, 10, 55, 40); [button2 setTitle:@"DEL" forState:UIControlStateNormal]; [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button2 setShowsTouchWhenHighlighted:YES]; button2.backgroundColor = [UIColor orangeColor]; button2.layer.cornerRadius = 8; [button2 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button2]; //除號 UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem]; button3.frame = CGRectMake(130, 10, 55, 40); [button3 setTitle:@"/" forState:UIControlStateNormal]; [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button3 setShowsTouchWhenHighlighted:YES]; button3.backgroundColor = [UIColor orangeColor]; button3.layer.cornerRadius = 8; [button3 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button3]; //* UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem]; button4.frame = CGRectMake(190, 10, 55, 40); [button4 setTitle:@"*" forState:UIControlStateNormal]; [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button4 setShowsTouchWhenHighlighted:YES]; button4.backgroundColor = [UIColor orangeColor]; button4.layer.cornerRadius = 8; [button4 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button4]; //7 UIButton *button5 = [UIButton buttonWithType:UIButtonTypeSystem]; button5.frame = CGRectMake(10 , 60, 55, 40); [button5 setTitle:@"7" forState:UIControlStateNormal]; [button5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button5 setShowsTouchWhenHighlighted:YES]; button5.backgroundColor = [UIColor whiteColor]; // button5.alpha = 0.3; button5.layer.cornerRadius = 8; [button5 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button5]; //8 UIButton *button7 = [UIButton buttonWithType:UIButtonTypeSystem]; button7.frame = CGRectMake(70 , 60, 55, 40); [button7 setTitle:@"8" forState:UIControlStateNormal]; [button7 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button7 setShowsTouchWhenHighlighted:YES]; button7.backgroundColor = [UIColor whiteColor]; button7.layer.cornerRadius = 8; [button7 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button7]; //9 UIButton *button8 = [UIButton buttonWithType:UIButtonTypeSystem]; button8.frame = CGRectMake(130 , 60, 55, 40); [button8 setTitle:@"9" forState:UIControlStateNormal]; [button8 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button8 setShowsTouchWhenHighlighted:YES]; button8.backgroundColor = [UIColor whiteColor]; button8.layer.cornerRadius = 8; [button8 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button8]; //- UIButton *button9 = [UIButton buttonWithType:UIButtonTypeSystem]; button9.frame = CGRectMake(190 , 60, 55, 40); [button9 setTitle:@"-" forState:UIControlStateNormal]; [button9 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button9 setShowsTouchWhenHighlighted:YES]; button9.backgroundColor = [UIColor orangeColor]; button9.layer.cornerRadius = 8; [button9 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button9]; //4 UIButton *button10 = [UIButton buttonWithType:UIButtonTypeSystem]; button10.frame = CGRectMake(10 , 110, 55, 40); [button10 setTitle:@"4" forState:UIControlStateNormal]; [button10 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button10 setShowsTouchWhenHighlighted:YES]; button10.backgroundColor = [UIColor whiteColor]; // button5.alpha = 0.3; button10.layer.cornerRadius = 8; [button10 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button10]; //5 UIButton *button11 = [UIButton buttonWithType:UIButtonTypeSystem]; button11.frame = CGRectMake(70 , 110, 55, 40); [button11 setTitle:@"5" forState:UIControlStateNormal]; [button11 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button11 setShowsTouchWhenHighlighted:YES]; button11.backgroundColor = [UIColor whiteColor]; button11.layer.cornerRadius = 8; [button11 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button11]; //6 UIButton *button12 = [UIButton buttonWithType:UIButtonTypeSystem]; button12.frame = CGRectMake(130 , 110, 55, 40); [button12 setTitle:@"6" forState:UIControlStateNormal]; [button12 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button12 setShowsTouchWhenHighlighted:YES]; button12.backgroundColor = [UIColor whiteColor]; button12.layer.cornerRadius = 8; [button12 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button12]; //+ UIButton *button13 = [UIButton buttonWithType:UIButtonTypeSystem]; button13.frame = CGRectMake(190 , 110, 55, 40); [button13 setTitle:@"+" forState:UIControlStateNormal]; [button13 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button13 setShowsTouchWhenHighlighted:YES]; button13.backgroundColor = [UIColor orangeColor]; button13.layer.cornerRadius = 8; [button13 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button13]; //1 UIButton *button14 = [UIButton buttonWithType:UIButtonTypeSystem]; button14.frame = CGRectMake(10 , 160, 55, 40); [button14 setTitle:@"1" forState:UIControlStateNormal]; [button14 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button14 setShowsTouchWhenHighlighted:YES]; button14.backgroundColor = [UIColor whiteColor]; // button5.alpha = 0.3; button14.layer.cornerRadius = 8; [button14 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button14]; //2 UIButton *button15 = [UIButton buttonWithType:UIButtonTypeSystem]; button15.frame = CGRectMake(70 , 160, 55, 40); [button15 setTitle:@"2" forState:UIControlStateNormal]; [button15 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button15 setShowsTouchWhenHighlighted:YES]; button15.backgroundColor = [UIColor whiteColor]; button15.layer.cornerRadius = 8; [button15 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button15]; //3 UIButton *button16 = [UIButton buttonWithType:UIButtonTypeSystem]; button16.frame = CGRectMake(130 , 160, 55, 40); [button16 setTitle:@"3" forState:UIControlStateNormal]; [button16 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button16 setShowsTouchWhenHighlighted:YES]; button16.backgroundColor = [UIColor whiteColor]; button16.layer.cornerRadius = 8; [button16 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button16]; //0 UIButton *button17 = [UIButton buttonWithType:UIButtonTypeSystem]; button17.frame = CGRectMake(10 , 210, 110, 40); [button17 setTitle:@"0" forState:UIControlStateNormal]; [button17 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button17 setShowsTouchWhenHighlighted:YES]; button17.backgroundColor = [UIColor whiteColor]; button17.layer.cornerRadius = 8; [button17 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button17]; //. UIButton *button18 = [UIButton buttonWithType:UIButtonTypeSystem]; button18.frame = CGRectMake(130 , 210, 55, 40); [button18 setTitle:@"." forState:UIControlStateNormal]; [button18 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button18 setShowsTouchWhenHighlighted:YES]; button18.backgroundColor = [UIColor whiteColor]; button18.layer.cornerRadius = 8; [button18 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button18]; //= UIButton *button20 = [UIButton buttonWithType:UIButtonTypeSystem]; button20.frame = CGRectMake(190 , 160, 55, 90); [button20 setTitle:@"=" forState:UIControlStateNormal]; [button20 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button20 setShowsTouchWhenHighlighted:YES]; button20.backgroundColor = [UIColor orangeColor]; button20.layer.cornerRadius = 8; [button20 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button20]; self.string=[[NSMutableString alloc]init]; } -(void)one:(id)sender { //保證是符號時在輸入數字時隱藏 if ([self.string hasPrefix:@"+"]||[self.string hasPrefix:@"-"]||[self.string hasPrefix:@"*"]||[self.string hasPrefix:@"/"])//判斷是否為運算符 { [self.string setString:@""];//字符串清零 } [self.string appendString:[sender currentTitle]];//數字連續輸入 // self.label1.text = [NSString stringWithString:string]; self.label1.text = [self.label1.text stringByAppendingString:[sender currentTitle]];//將計算公式顯示在label1中 self.label.text=[NSString stringWithString:string];//顯示數值 self.num1=[self.label.text doubleValue];//保存輸入的數值 NSLog(@"%f",self.num1); } -(void)two:(id)sender { [self.string setString:@""];//字符串清零 [self.string appendString:[sender currentTitle]]; self.label.text=[NSString stringWithString:string]; self.label1.text = [self.label1.text stringByAppendingString:[sender currentTitle]]; //顯示輸入的符號 //判斷輸入是+號 if ([self.string hasPrefix:@"+"])//hasPrefix:判斷字符串以加號開頭 { self.num2=self.num1;//將前面的數值保存在num2里 self.num4=1; } //判斷輸入是-號 else if([self.string hasPrefix:@"-"])//hasPrefix:判斷字符串以減號開頭 { self.num2=self.num1; self.num4=2; } //判斷輸入是*號 else if([self.string hasPrefix:@"*"])//hasPrefix:判斷字符串以乘號開頭 { self.num2=self.num1; self.num4=3; } //判斷輸入是/號 else if([self.string hasPrefix:@"/"])//hasPrefix:判斷字符串以除號開頭 { self.num2=self.num1; self.num4=4; } } -(void)go:(id)sender { //判斷輸入是+號 if (self.num4==1) { self.num3=self.num2+[self.label.text doubleValue];//[self.label.text doubleValue]是每次后輸入的數字 self.label.text=[NSString stringWithFormat:@"%g",self.num3];//顯示結果 // self.label1.text = [self.label1.text stringByAppendingString:@"="]; //將等于好在label1中顯示出來 self.num1=[self.label.text doubleValue];//為了可以連加。保存結果 self.label1.text = @""; //滯空 ,用來存放結果 self.label1.text = [NSString stringWithFormat:@"%g",self.num3]; //上面顯示結果 self.num3=0; [self.string setString:@""];//保證每次結果正確輸出后,再次計算,不用按清除鍵 } //判斷輸入是-號 else if(self.num4==2) { self.num3=self.num2-[self.label.text doubleValue]; self.label.text=[NSString stringWithFormat:@"%g",self.num3]; self.label1.text = [self.label1.text stringByAppendingString:@"="]; //將等于好在label1中顯示出來 self.num1=[self.label.text doubleValue]; self.label1.text = @""; //滯空 ,用來存放結果 self.label1.text = [NSString stringWithFormat:@"%g",self.num3]; //上面顯示結果 self.num3=0; [self.string setString:@""]; } //判斷輸入是*號 else if(self.num4==3) { self.num3=self.num2*[self.label.text doubleValue]; self.label.text=[NSString stringWithFormat:@"%g",self.num3]; self.label1.text = [self.label1.text stringByAppendingString:@"="]; //將等于好在label1中顯示出來 self.num1=[self.label.text doubleValue]; self.label1.text = @""; //滯空 ,用來存放結果 self.label1.text = [NSString stringWithFormat:@"%g",self.num3]; //上面顯示結果 self.num3=0; [self.string setString:@""]; } //判斷輸入是/號 else if(self.num4 == 4) { self.num3=self.num2/[self.label.text doubleValue]; self.label.text=[NSString stringWithFormat:@"%g",self.num3];//計算結果顯示出來 self.label1.text = [self.label1.text stringByAppendingString:@"="]; //將等于好在label1中顯示出來 self.num1=[self.label.text doubleValue];//把計算的結果保存一下 self.label1.text = @""; //滯空 ,用來存放結果 self.label1.text = [NSString stringWithFormat:@"%g",self.num3]; //上面顯示結果 self.num3=0; [self.string setString:@""]; } } //當按下清除建時,所有數據清零 -(void)clean:(id)sender{ [self.string setString:@""];//清空字符串 self.num3=0; self.num2 = 0; self.label.text=@"";//保證下次輸入時清零 self.label1.text = @""; } //返回鍵 -(void)back:(id)sender { if ([self.label.text length] > 0 && [self.label1.text length] > 0) { self.label.text = [self.label.text substringToIndex:[self.label.text length] - 1]; self.label1.text = [self.label1.text substringToIndex:[self.label1.text length] - 1]; }else if ([self.label.text length] > 0 || [self.label1.text length] > 0){ // self.label1.text = [self.label1.text substringToIndex:[self.label1.text length] - 1]; [self.string setString:@""]; self.num3=0; self.num2 = 0; self.label1.text = @""; self.label.text=@""; } self.num1=[self.label.text doubleValue]; } //- (void)numberAction:(UIButton *)button //{ // NSLog(@"數字"); // if (self.isSymbol == YES) { // //如果剛剛點擊過符號鍵,就重新輸入 // self.label.text = button.currentTitle; // }else{ // self.label.text = [NSString stringWithFormat:@"%@%@",self.label.text,button.currentTitle]; // } // self.isSymbol = NO; // //} // //- (void)addAction:(UIButton *)button //{ // //只要點擊符號鍵,就將屬性為yes // self.isSymbol = YES; // //保存第一個數字 // self.firstNumber = [self.label.text integerValue]; // //保存當前選擇的符號(+ - * /) // if ([button.currentTitle isEqualToString:@"+"]) { // self.sumbol = 1; // }else if ([button.currentTitle isEqualToString:@"-"]){ // self.sumbol = 2; // }else if ([button.currentTitle isEqualToString:@"*"]){ // self.sumbol = 3; // }else if ([button.currentTitle isEqualToString:@"/"]){ // self.sumbol = 4; // } // NSLog(@"符號"); //} //- (void)equalAction:(UIButton *)equal //{ // //在等號方法中做計算 // // //計算 // if (self.sumbol == 1) { // //獲取第二個數字 // NSInteger secondNumber = [self.label.text integerValue]; // NSInteger result = _firstNumber + secondNumber; // //將計算結果給label顯示 // self.label.text = [NSString stringWithFormat:@"%d",result]; // // }if (self.sumbol == 2) { // NSInteger secondNumber = [self.label.text integerValue]; // NSInteger result = _firstNumber - secondNumber; // //將計算結果給label顯示 // self.label.text = [NSString stringWithFormat:@"%d",result]; // } // // // // NSLog(@"等于"); //} // - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
下面是沒有實現的,在網上找的代碼參考 ........謹慎使用
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property(retain,nonatomic)UIButton *button; @property(retain,nonatomic)UILabel *label; @property(retain,nonatomic)NSMutableString *string; @property(assign,nonatomic)double num1,num2,num3,num4; @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize button,label,string,num1,num2,num3,num4;//string保存字符,顯示數值。num1是存輸入的數值,num2是存運算符前的數值,num3是運算結果,num4是判斷進行何種運算 - (void)viewDidLoad { [super viewDidLoad]; //設置背景圖片 NSBundle *bundle=[NSBundle mainBundle]; NSData *data=[[NSData alloc]initWithContentsOfFile: [bundle pathForResource:@"3" ofType:@"jpg"]];//找到NSBundle的某一資源 UIImage *img=[UIImage p_w_picpathWithData:data];//創建了可用的圖像對象 [self.view setBackgroundColor:[UIColor colorWithPatternImage:img]];//UIColor colorWithPatternImage:方法是把圖片轉化為color類型 將背景換做提供的圖片 //創建標簽 self.label=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 200, 50)]; [self.view addSubview:label]; self.label.backgroundColor=[UIColor clearColor];//清空背景顏色 self.label.textColor=[UIColor blueColor];//字體顏色 self.label.textAlignment = NSTextAlignmentRight;;//字體居右 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.backgroundColor = [UIColor whiteColor]; [self.button setTitle:[array objectAtIndex:n++] forState:UIControlStateNormal]; self.button.titleLabel.font = [UIFont systemFontOfSize:30]; self.button.layer.cornerRadius = 6; [self.view addSubview:button]; [self.button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; [self.button addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; } } //單獨添加0 UIButton *button0=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [button0 setFrame:CGRectMake(30, 345, 60, 60)]; button0.layer.cornerRadius = 6; button0.backgroundColor = [UIColor whiteColor]; [button0 setTitle:@"0" forState:UIControlStateNormal]; button0.titleLabel.font = [UIFont systemFontOfSize:30]; [button0 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; [button0 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [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.backgroundColor = [UIColor whiteColor]; button1.layer.cornerRadius = 6; [button1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; [button1 setTitle:[array1 objectAtIndex:i] forState:UIControlStateNormal]; [self.view addSubview:button1]; button1.titleLabel.font = [UIFont systemFontOfSize:30]; [button1 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside]; } //添加= UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect]; button2.backgroundColor = [UIColor whiteColor]; [button2 setFrame:CGRectMake(160, 410, 125, 35)]; [button2 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; button2.titleLabel.font = [UIFont systemFontOfSize:30]; [button2 setTitle:@"=" forState:UIControlStateNormal]; button2.layer.cornerRadius = 6; [button2 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button2]; //添加清除鍵 UIButton *button3=[UIButton buttonWithType:UIButtonTypeRoundedRect]; button3.backgroundColor = [UIColor whiteColor]; [button3 setFrame:CGRectMake(30, 410, 125, 35)]; button3.layer.cornerRadius = 6; [button3 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; [button3 setTitle:@"AC" forState:UIControlStateNormal]; button3.titleLabel.font = [UIFont systemFontOfSize:20]; [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.titleLabel.font = [UIFont systemFontOfSize:30]; button4.layer.cornerRadius = 6; [button4 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; button4.backgroundColor = [UIColor whiteColor]; [button4 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button4]; //后退 UIButton *button5=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [button5 setFrame:CGRectMake(160, 345, 60, 60)]; [button5 setTitle:@"back" forState:UIControlStateNormal]; button5.backgroundColor = [UIColor whiteColor]; button5.layer.cornerRadius = 6; [button5 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; button5.titleLabel.font = [UIFont systemFontOfSize:20]; [button5 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button5]; self.string=[[NSMutableString alloc]init];//初始化可變字符串,分配內存 // Do any additional setup after loading the view, typically from a nib. } -(void)one:(id)sender { //保證是符號時在輸入數字時隱藏 if ([self.string hasPrefix:@"+"]||[self.string hasPrefix:@"-"]||[self.string hasPrefix:@"*"]||[self.string hasPrefix:@"/"])//判斷是否為運算符 { [self.string setString:@""];//字符串清零 } [self.string appendString:[sender currentTitle]];//數字連續輸入 self.label.text=[NSString stringWithString:string];//顯示數值 self.num1=[self.label.text doubleValue];//保存輸入的數值 NSLog(@"%f",self.num1); } -(void)two:(id)sender { [self.string setString:@""];//字符串清零 [self.string appendString:[sender currentTitle]]; self.label.text=[NSString stringWithString:string]; //判斷輸入是+號 if ([self.string hasPrefix:@"+"])//hasPrefix:判斷字符串以加號開頭 { self.num2=self.num1;//將前面的數值保存在num2里 self.num4=1; } //判斷輸入是-號 else if([self.string hasPrefix:@"-"])//hasPrefix:判斷字符串以減號開頭 { self.num2=self.num1; self.num4=2; } //判斷輸入是*號 else if([self.string hasPrefix:@"*"])//hasPrefix:判斷字符串以乘號開頭 { self.num2=self.num1; self.num4=3; } //判斷輸入是/號 else if([self.string hasPrefix:@"/"])//hasPrefix:判斷字符串以除號開頭 { self.num2=self.num1; self.num4=4; } } -(void)go:(id)sender { //判斷輸入是+號 if (self.num4==1) { self.num3=self.num2+[self.label.text doubleValue];//[self.label.text doubleValue]是每次后輸入的數字 self.label.text=[NSString stringWithFormat:@"%g",self.num3];//顯示結果 self.num1=[self.label.text doubleValue];//為了可以連加。保存結果 self.num3=0; [self.string setString:@""];//保證每次結果正確輸出后,再次計算,不用按清除鍵 } //判斷輸入是-號 else if(self.num4==2) { self.num3=self.num2-[self.label.text doubleValue]; self.label.text=[NSString stringWithFormat:@"%g",self.num3]; self.num1=[self.label.text doubleValue]; self.num3=0; [self.string setString:@""]; } //判斷輸入是*號 else if(self.num4==3) { self.num3=self.num2*[self.label.text doubleValue]; self.label.text=[NSString stringWithFormat:@"%g",self.num3]; self.num1=[self.label.text doubleValue]; self.num3=0; [self.string setString:@""]; } //判斷輸入是/號 else if(self.num4 == 4) { self.num3=self.num2/[self.label.text doubleValue]; self.label.text=[NSString stringWithFormat:@"%g",self.num3];//計算結果顯示出來 self.num1=[self.label.text doubleValue];//把計算的結果保存一下 self.num3=0; [self.string setString:@""]; } } //當按下清除建時,所有數據清零 -(void)clean:(id)sender{ [self.string setString:@""];//清空字符串 self.num3=0; self.num2 = 0; self.label.text=@"";//保證下次輸入時清零 } //返回鍵 -(void)back:(id)sender { if ([self.label.text length] > 0) { self.label.text = [self.label.text substringToIndex:[self.label.text length] - 1]; } } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } @end
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。