私は、UITextField を介して値を取得し、オペランド ボタンが押されたときにそれらを配列 (varArray) に格納する電卓アプリ (以下のコード) に取り組んでいます。オペランド ボタンを押すと、そのオペランドが別の配列 (opArray) に保存され、varTextField がクリアされます。等号ボタン (まだ完全には実装されておらず、先に進む前に計算された値とオペランドを取得しようとしています) を押すと、計算が実行され、ラベル (equationLabel) に表示されます。これらはすべて、多変数機能を提供するためのものです。最後に、var1、op1、var2、op2、var3...などを入力して、方程式/結果全体を方程式ラベルに表示できるようにしたいと考えています。
また、オペランドをスイッチの int 値と同一視しました。
変数とオペランドを結合するスイッチがまったく実行されていないように見えるため、現時点では立ち往生しています。結果アイテムは方程式ラベルに送信されますが、デフォルト値は 0 のみです。
私はコーディングに比較的慣れておらず、objective-c についてはさらに初心者なので、ここでばかげたことをしているだけかもしれません。スイッチの問題に関するヘルプは大歓迎です。
また、各スイッチ ケースで、xCode から次の警告が表示されることにも言及する必要があります。
更新され たコード 2 完全に機能しますが、equalButton が初めて押されるまで、アプリが opButtonPush の配列に書き込みを行わないように見えることを除きます。その 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 =
}