iOS实现简单计算器小功能
程序员文章站
2022-06-09 19:17:22
本文实例为大家分享了ios实现简单计算器小功能的具体代码,供大家参考,具体内容如下simplecaculatorviewcontroller.h//// simplecaculatorviewcon...
本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 小程序中视频作为页面背景
下一篇: Java Object类和包装类深入解读