網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)簡單計(jì)算器小功能的具體代碼,供大家參考,具體內(nèi)容如下
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; } @end
原文鏈接:https://blog.csdn.net/u011132324/article/details/19128333
相關(guān)推薦
- 2022-07-22 git提交代碼設(shè)置某些文件不可上傳
- 2022-06-21 C語言字符串函數(shù)與內(nèi)存函數(shù)精講_C 語言
- 2023-06-13 C語言函數(shù)多個(gè)返回值方式_C 語言
- 2022-10-10 React實(shí)時(shí)預(yù)覽react-live源碼解析_React
- 2022-05-31 docker-compose+nginx部署前后端分離的項(xiàng)目實(shí)踐_docker
- 2022-04-24 python?Django實(shí)現(xiàn)增刪改查實(shí)戰(zhàn)代碼_python
- 2022-01-19 解決form表單reset()報(bào)錯(cuò),$(...)[0].reset is not a functio
- 2022-05-06 詳析Python面向?qū)ο笾械睦^承_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支