網站首頁 編程語言 正文
本文實例為大家分享了iOS實現簡單計算器小功能的具體代碼,供大家參考,具體內容如下
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 ? //記錄數字按鈕點擊事件 - (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;//因為第一次進入程序會輸入數字,因此為YES ? ? } } ? //清零操作 - (IBAction)clearDS:(UIButton *)sender { ? ?? ? ? _displayScreen.text = @"0"; ? ? _isUserInputingNumber = NO;//表示沒有再輸入了 } ? //得到結果 - (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; } ? //彈出提示對話框 - (IBAction)hint:(UIButton *)sender { ? ? UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:@"本計算器由LJH出品" delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil]; ? ? [alert show]; } ? //進行四則運算 - (IBAction)act:(UIButton *)sender { ? ? //1.得到_displayScreen上的數字 ? ? _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; ? ? } } ? //進行回刪操作 - (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
相關推薦
- 2022-05-22 利用DOSBox運行匯編的詳細步驟_匯編語言
- 2023-12-12 InetSocketAddress的使用
- 2023-10-24 記ElementUI內置的$confirm確認消息彈框方法
- 2022-05-11 多線程編程(下):線程同步&通信
- 2022-03-23 C++?Boost?PropertyTree解析INI文件詳解_C 語言
- 2022-07-13 簡單的利用boost.python 和 boost.numpy 實現python和c++之間數據通信
- 2022-10-06 Android?Jetpack庫重要組件WorkManager的使用_Android
- 2022-06-25 python+opencv實現堆疊圖片_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支