//清空处理
- (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 alloc] initWithFormat:@"%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、设置断点来调试程序。