我正在开发一个计算器应用程序(下面的代码),它通过 UITextField 获取值,当按下操作数按钮时将它们存储到数组(varArray)中。按下操作数按钮还会将该操作数存储在另一个数组 (opArray) 中并清除 varTextField。按下相等按钮(尚未完全实现,仍在尝试获取值和在我继续之前计算的操作数)将执行计算并将其显示在标签 (equationLabel) 中。所有这些都是为了提供多变量功能。最后,我希望能够输入如下内容:var1、op1、var2、op2、var3...等,并将整个方程式/结果显示在 equationLabel 中。
我还将操作数等同于开关的 int 值。
我现在被卡住了,因为组合变量和操作数的开关似乎根本没有运行。结果项被发送到 equationLabel,但只有默认值 0。
我对编码比较陌生,对objective-c 更是如此,所以我可能只是在这里做一些愚蠢的事情。任何有关开关问题的帮助将不胜感激。
我还应该提到,在每个 switch 案例中,我从 xCode 收到以下警告:“'result' 的本地声明隐藏了实例变量。
更新后的 code2 工作得很好,除了应用程序似乎没有写入 opButtonPush 中的数组,直到第一次推送 equalButton。数组值在该 UI 事件之前返回为 null。
- (IBAction)opButtonPush:(id)sender //if +, -, /, or x buttons are pushed, add value in varTextField to varArray and button label text to setOpArray
{
UIButton *tmpButton = (UIButton *)sender;
float textValue=0;
//add operand to opArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction
if ([[[tmpButton titleLabel] text] isEqualToString:@"+"])
{
textValue = [[varTextField text] floatValue];
[opArray addObject:[NSNumber numberWithInt:0]];
[varOpArray addObject:[NSNumber numberWithFloat:textValue]];
[varOpArray addObject:@"+"];
[varArray addObject:[NSNumber numberWithFloat:textValue]];
varTextField.text = @"";
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"-"])
{
textValue = [[varTextField text] floatValue];
[opArray addObject:[NSNumber numberWithInt:1]];
[varOpArray addObject:[NSNumber numberWithFloat:textValue]];
[varOpArray addObject:@"-"];
[varArray addObject:[NSNumber numberWithFloat:textValue]];
varTextField.text = @"";
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"x"])
{
textValue = [[varTextField text] floatValue];
[opArray addObject:[NSNumber numberWithInt:2]];
[varOpArray addObject:[NSNumber numberWithFloat:textValue]];
[varOpArray addObject:@"x"];
[varArray addObject:[NSNumber numberWithFloat:textValue]];
varTextField.text = @"";
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"/"])
{
textValue = [[varTextField text] floatValue];
[opArray addObject:[NSNumber numberWithInt:3]];
[varOpArray addObject:[NSNumber numberWithFloat:textValue]];
[varOpArray addObject:@"/"];
[varArray addObject:[NSNumber numberWithFloat:textValue]];
varTextField.text = @"";
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"+/-"])
{
[varTextField setText:@"-"];
}
[equationLabel setText:[varOpArray description]]; //send vars and ops, as entered, to equationLabel
//NSLog(@"%@", varOpArray);
}
- (IBAction)calculator:(id)sender
{
UIButton *tmpButton = (UIButton *)sender;
int opCount=0;
float finalTextValue=0;
float result = [[varArray objectAtIndex:0] floatValue];
if ([[[tmpButton titleLabel] text] isEqualToString:@"="]) {
finalTextValue = [[varTextField text] floatValue];
[varArray addObject:[varTextField text]];
[varOpArray addObject:[varTextField text]];
[varOpArray addObject:@"="];
[equationLabel setText:[varOpArray description]];
for (int i=1; i<[varArray count]; i++) { //use opArray[i] to eval the result and new var
//then write it to an array
switch ([[opArray objectAtIndex:opCount] intValue]) {
case 0:
result = result + [[varArray objectAtIndex:(i)] floatValue];
[resultArray addObject:[NSNumber numberWithFloat:result]];
[equationLabel setText:[varOpArray description]]; //send vars and ops, as entered, to equationLabel
break;
case 1:
result = result - [[varArray objectAtIndex:(i)] floatValue];
[resultArray addObject:[NSNumber numberWithFloat:result]];
[equationLabel setText:[varOpArray description]]; //send vars and ops, as entered, to equationLabel
break;
case 2:
result = result * [[varArray objectAtIndex:(i)] floatValue];
[resultArray addObject:[NSNumber numberWithFloat:result]];
[equationLabel setText:[varOpArray description]]; //send vars and ops, as entered, to equationLabel
break;
case 3:
result = result / [[varArray objectAtIndex:(i)] floatValue];
[resultArray addObject:[NSNumber numberWithFloat:result]];
[equationLabel setText:[varOpArray description]]; //send vars and ops, as entered, to equationLabel
break;
default:
NSLog(@"default");
break;
}
opCount++;
}
[varOpArray addObject:[NSNumber numberWithFloat:result]];
[equationLabel setText:[varOpArray description]];
[varTextField setText:@""];
//reset arrays for next function
varOpArray = [NSMutableArray new];
varArray = [NSMutableArray new];
opArray = [NSMutableArray new];
}
}
更新代码1:
- (IBAction)opButtonPush:(id)sender //if +, -, /, or x buttons are pushed, add value in varTextField to varArray and button label text to setOpArray
{
UIButton *tmpButton = (UIButton *)sender;
float textValue=0;
//add operand to opArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction
if ([[[tmpButton titleLabel] text] isEqualToString:@"+"])
{
textValue = [[varTextField text] floatValue];
[opArray addObject:[NSNumber numberWithInt:0]];
[varOpArray addObject:[NSNumber numberWithFloat:textValue]];
[varOpArray addObject:@"+"];
[varArray addObject:[NSNumber numberWithFloat:textValue]];
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"-"])
{
textValue = [[varTextField text] floatValue];
[opArray addObject:[NSNumber numberWithInt:1]];
[varOpArray addObject:[NSNumber numberWithFloat:textValue]];
[varOpArray addObject:@"-"];
[varArray addObject:[NSNumber numberWithFloat:textValue]];
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"x"])
{
textValue = [[varTextField text] floatValue];
[opArray addObject:[NSNumber numberWithInt:2]];
[varOpArray addObject:[NSNumber numberWithFloat:textValue]];
[varOpArray addObject:@"x"];
[varArray addObject:[NSNumber numberWithFloat:textValue]];
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"/"])
{
textValue = [[varTextField text] floatValue];
[opArray addObject:[NSNumber numberWithInt:3]];
[varOpArray addObject:[NSNumber numberWithFloat:textValue]];
[varOpArray addObject:@"/"];
[varArray addObject:[NSNumber numberWithFloat:textValue]];
}
//add value of float var to arrays to be accessed by calculator
[equationLabel setText:[varOpArray description]]; //send vars and ops, as entered, to equationLabel
varTextField.text = @"";
NSLog(@"%@", varOpArray);
}
- (IBAction)calculator:(id)sender
{
UIButton *tmpButton = (UIButton *)sender;
int opCount=0;
float finalTextValue=0;
float result=0.0;
if ([[[tmpButton titleLabel] text] isEqualToString:@"="]) {
finalTextValue = [[varTextField text] floatValue];
//[opArray addObject:[NSNumber numberWithInt:4]];
[varOpArray addObject:[NSNumber numberWithFloat:finalTextValue]];
[varOpArray addObject:@"="];
[varArray addObject:[NSNumber numberWithFloat:finalTextValue]];
for (int i=0; i<[varArray count]; i+=2) {
switch ([[opArray objectAtIndex:opCount] intValue]) {
case 0:
result = [[varArray objectAtIndex:i] floatValue] + [[varArray objectAtIndex:(i+1)] floatValue];
[varOpArray addObject:[NSNumber numberWithFloat:result]];
varTextField.text = @"";
break;
case 1:
result = [[varArray objectAtIndex:i] floatValue] - [[varArray objectAtIndex:(i+1)] floatValue];
[varOpArray addObject:[NSNumber numberWithFloat:result]];
varTextField.text = @"";
break;
case 2:
result = [[varArray objectAtIndex:i] floatValue] * [[varArray objectAtIndex:(i+1)] floatValue];
[varOpArray addObject:[NSNumber numberWithFloat:result]];
varTextField.text = @"";
break;
case 3:
result = [[varArray objectAtIndex:i] floatValue] / [[varArray objectAtIndex:(i+1)] floatValue];
[varOpArray addObject:[NSNumber numberWithFloat:result]];
varTextField.text = @"";
break;
default:
NSLog(@"default");
break;
}
opCount++;
}
varOpArray = [NSMutableArray new];
varArray = [NSMutableArray new];
opArray = [NSMutableArray new];
[varTextField resignFirstResponder];
}
}
- (IBAction)textFieldClear:(id)sender
{
if ([varTextField isFirstResponder])
{
}
else
{
[varTextField becomeFirstResponder];
varTextField.text = @"";
}
}
原始代码
- (IBAction)opButtonPush:(id)sender //if +, -, /, or * buttons are pushed, add value in varTextField to varArray and button label text to setOpArray
{
UIButton *tmpButton = (UIButton *)sender;
//add value of float var to varArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction
if ([[tmpButton titleLabel] text] != @"=" || [[tmpButton titleLabel] text] != @"+/-") {
varArray = [[NSMutableArray alloc] initWithObjects:varTextField.text, nil];
}
//add operand to opArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction
if ([[[tmpButton titleLabel] text] isEqualToString:@"+"])
{
opArray = [[NSMutableArray alloc] initWithObjects:@"+", nil];
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"-"])
{
opArray = [[NSMutableArray alloc] initWithObjects:@"-", nil];
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"x"])
{
opArray = [[NSMutableArray alloc] initWithObjects:@"*", nil];
}
else if ([[[tmpButton titleLabel] text] isEqualToString:@"/"])
{
opArray = [[NSMutableArray alloc] initWithObjects:@"/", nil];
}
varTextField.text = @""; //clear varTextField
}
- (IBAction)calculator:(id)sender
{
[equationLabel setText:@"x + y = z"];
NSString *tmpVar;
NSString *tmpOp;
float var;
int varCount = [varArray count];
int opCount = [opArray count];
UIButton *tmpButton = (UIButton *)sender;
if ([[[tmpButton titleLabel] text] isEqualToString:@"="]) {
//for each instance of varArray and opArray, add it to the string "equation" and equation label
for (int i=0; i<[varArray count]; i++) {
tmpVar = [varArray objectAtIndex:i];
var = ([tmpVar floatValue]);
for (int j=0; j<[opArray count]; j++) {
tmpOp = [opArray objectAtIndex:j];
//equationLabel =
}
}
}
//result =
}