简易计算器各响应函数与功能函数

本文详细介绍了如何使用Swift和UIKit开发一个基本的四则运算计算器应用,包括按钮交互、数值输入、运算符处理和等号功能实现。通过实例演示,帮助开发者掌握iOS应用程序的基本开发流程和关键组件的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//清空处理

- (IBAction)clearZero:(id)sender{

isPointPressed =FALSE;

whichButtonPressed =0;

pointFlag =0;

value =0.0;

secondValue =0.0;

NSString *clearText = [[NSStringalloc]initWithFormat:@"0"];

math.text = clearText;

[clearTextrelease];

}

//处理负数

- (IBAction)negative:(id)sender{

value = -value;

NSString *negativeValue = [[NSString allocinitWithFormat:@"%g",value];

math.text = negativeValue;

[negativeValue release];

}


//记录按下的数值,并在textfield中显示

-(IBAction)numberPressed:(id)sender{

UIButton *onButton = (UIButton *)sender;

float temp = onButton.tag;

if(value >= 0)

{

if(TRUE == isPointPressed)

{

value += pow(0.1,++pointFlag)*onButton.tag; 

}

else

{

value = value*10 + temp;

}

}

else{

value = -value;

if(TRUE == isPointPressed)

{

value += pow(0.1,++pointFlag)*onButton.tag; 

}

else

{

value = value*10 + temp;

}

value = -value;

}

NSString *newValue = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue;

[newValuerelease];

}


//响应点号运算符的点击

- (IBAction)pointPressed:(id)sender{

isPointPressed =TRUE;

}


//响应运算符号的点击

- (IBAction)mathPressed:(id)sender{

self.equalFunc;//执行等号操作

UIButton *newButton = (UIButton *)sender;

whichButtonPressed = newButton.tag;

secondValue = [math.textfloatValue];

switch(whichButtonPressed) {

case 20:

secondValue = [math.text floatValue];

NSString *newAllValue0 = [[NSString alloc] initWithFormat:@"%g +",secondValue];

allMath.text = newAllValue0;

[newAllValue0 release];

break;

case 21:

secondValue = [math.text floatValue];

NSString *newAllValue1 = [[NSString alloc] initWithFormat:@"%g -",secondValue]; 

allMath.text = newAllValue1;

[newAllValue1 release];

break;

case 22:

secondValue = [math.text floatValue];

NSString *newAllValue2 = [[NSString alloc] initWithFormat:@"%g ×",secondValue]; 

allMath.text = newAllValue2;

[newAllValue2 release];

break;

case 23:

secondValue = [math.text floatValue];

NSString *newAllValue3 = [[NSString alloc] initWithFormat:@"%g ÷",secondValue]; 

allMath.text = newAllValue3;

[newAllValue3 release];

break;

default:

break;

}


pointFlag =0;

value =0.0;

NSString *value0 = [[NSString alloc] initWithFormat:@"%g",value];

math.text = value0;

[value0 release];

isPointPressed =FALSE;

}


//运算符实现等号运算

- (void)equalFunc{

switch (whichButtonPressed) {

case20:

value +=secondValue;

NSString *newValue0 = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue0;

whichButtonPressed =0;

[newValue0release];

break;

case21:

value =secondValue -value;

NSString *newValue1 = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue1;

whichButtonPressed =0;

[newValue1release];

break;

case22:

value *=secondValue;

NSString *newValue2 = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue2;

whichButtonPressed =0;

[newValue2release];

break;

case23:

value =secondValue/value;

NSString *newValue3 = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue3;

whichButtonPressed =0;

[newValue3release];

break;

default:

break;

}

}


//响应等号运算,求值并在textfield中显示

- (IBAction)equalPressed:(id)sender{

switch (whichButtonPressed) {//通过按钮的tag来选择相应的运算

case20://加法运算

value +=secondValue;

NSString *newValue0 = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue0;

whichButtonPressed =0;

[newValue0release];

break;

case21://减法运算

value =secondValue -value;

NSString *newValue1 = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue1;

whichButtonPressed =0;

[newValue1release];

break;

case22://乘法运算

value *=secondValue;

NSString *newValue2 = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue2;

whichButtonPressed =0;

[newValue2release];

break;

case23://除法运算

value =secondValue/value;

NSString *newValue3 = [[NSStringalloc]initWithFormat:@"%10g",value];

math.text = newValue3;

whichButtonPressed =0;

[newValue3release];

break;

default:

break;

}

}


个人总结:

1、通过此简易计算器,基本上熟悉了button和textfield按钮的使用,可以使用tag对各个按钮进行区分;

2、了解了iphone应用程序开发的基本步骤,IB中进行界面的组合,xcode中编写功能函数,xib将其与界面中各按钮进行连接;

3、虚拟键盘的使用,选择使用哪种键盘,禁用虚拟键盘,以及虚拟键盘的关闭,将view换成UIControl或者加入一个满屏幕的button按钮;

4、给应用程序添加图标;

5、设置断点来调试程序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值