您好,登錄后才能下訂單哦!
本篇文章為大家展示了iOS是怎樣實(shí)現(xiàn)簡單計(jì)算器小功能,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
SimpleCaculatorViewController.h
// // SimpleCaculatorViewController.h // SimpleCaculator // // Created by LI Junui on 14-2-12. // Copyright (c) 2014年 LEE JUNHUI. All rights reserved. // #import <UIKit/UIKit.h> @interface SimpleCaculatorViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *displayScreen; - (IBAction)numberBtnClick:(UIButton *)sender; - (IBAction)clearDS:(UIButton *)sender; - (IBAction)caculate:(UIButton *)sender; - (IBAction)hint:(UIButton *)sender; - (IBAction)act:(UIButton *)sender; - (IBAction)clearBack:(UIButton *)sender; @property(assign, nonatomic) BOOL isUserInputingNumber; @property(assign, nonatomic) int num1; @property(assign, nonatomic) int num2; @property(assign, nonatomic) int tagForAct; @end
SimpleCaculatorViewController.m
// // SimpleCaculatorViewController.m // SimpleCaculator // // Created by LI Junui on 14-2-12. // Copyright (c) 2014年 LEE JUNHUI. All rights reserved. // #import "SimpleCaculatorViewController.h" @interface SimpleCaculatorViewController () @end @implementation SimpleCaculatorViewController //記錄數(shù)字按鈕點(diǎn)擊事件 - (IBAction)numberBtnClick:(UIButton *)sender { if(self.isUserInputingNumber){ int re = [_displayScreen.text intValue] * 10 + [sender.currentTitle intValue]; _displayScreen.text = [NSString stringWithFormat:@"%d",re]; } else{ [_displayScreen setText:sender.currentTitle]; _isUserInputingNumber = YES;//因?yàn)榈谝淮芜M(jìn)入程序會(huì)輸入數(shù)字,因此為YES } } //清零操作 - (IBAction)clearDS:(UIButton *)sender { _displayScreen.text = @"0"; _isUserInputingNumber = NO;//表示沒有再輸入了 } //得到結(jié)果 - (IBAction)caculate:(UIButton *)sender { int re = 0; _num2 = [_displayScreen.text intValue]; switch (_tagForAct) { case 1: //加法 re = _num1 + _num2; break; case 2: //減法 re = _num1 - _num2; break; case 3: //乘法 re = _num1 * _num2; break; case 4: //除法 re = _num1 / _num2; break; } _displayScreen.text = [NSString stringWithFormat:@"=%d", re]; _num1 = 0; _num2 = 0; } //彈出提示對(duì)話框 - (IBAction)hint:(UIButton *)sender { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:@"本計(jì)算器由LJH出品" delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil]; [alert show]; } //進(jìn)行四則運(yùn)算 - (IBAction)act:(UIButton *)sender { //1.得到_displayScreen上的數(shù)字 _num1 = [_displayScreen.text intValue]; _displayScreen.text = sender.currentTitle; _isUserInputingNumber =YES; switch (sender.tag) { case 1: //加法 _tagForAct = 1; break; case 2: //減法 _tagForAct = 2; break; case 3: //乘法 _tagForAct = 3; break; case 4: //除法 _tagForAct = 4; break; } } //進(jìn)行回刪操作 - (IBAction)clearBack:(UIButton *)sender { int length = [_displayScreen.text length]; int temp = [_displayScreen.text intValue]; temp = temp/length; }
上述內(nèi)容就是iOS是怎樣實(shí)現(xiàn)簡單計(jì)算器小功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。