電卓の授業に取り組んでいます。ほぼ完成しました。
今、私の計算機は、例えば2 + 3 + 5x6-3/2x6x7-8x9と6*9+7を行うことができます
しかし、2 * 4 *8......はできません。
誰でもコードの何が問題なのか教えてくれますか?これは以下の私のコードです:
@implementation CalculatorBrain
- (void)setOperand:(double)aDouble
{
operand = aDouble;
}
- (double)performOperation:(NSString *)operation
{
if ([operation isEqual:@"sqrt"]){
operand = sqrt(operand);
}
else if ([@"+/-" isEqual:operation]) {
operand = - operand;
}
else if ([@"%" isEqual:operation]) {
operand = operand/ 100;
}
else if ([@"1/x" isEqual:operation]) {
operand = 1/operand;
}
else if ([@"C" isEqual:operation]){
operand = 0;
}
else if ([@"AC" isEqual:operation]) {
operand = 0;
alwaysFirstDigit = 0;
digi1 = 0;
digi2 = 0;
}else {
//Assign the first digit to alwaysFirstDigit, and decide the second digit to digi1 or digi2
if (start == 0){
alwaysFirstDigit = operand;
NSLog(@"alwaysFirstDigit = %f", alwaysFirstDigit);
operationFirst = operation;
NSLog(@"operationFirst = %@", operationFirst);
start = 1;
}else if (signFlag == 1){
digi2 = operand;
NSLog(@"digi2 = %f", digi2);
}else {
digi1 = operand;
NSLog(@"digi1 = %f", digi1);
}
// This is an accumulator if first operation is equal to * or /
if (signFlag == 1) {
NSLog(@"RUN01 ");
if ([@"+" isEqual:operationSecond])
{
digi1 = digi1 + digi2;
}
else if ([@"*" isEqual:operationSecond])
{
digi1 = digi1 * digi2;
}
else if ([@"-" isEqual:operationSecond])
{
digi1 = digi1 - digi2;
}
else if ([@"/" isEqual:operationSecond])
{
digi1 = digi1 / digi2;
}
digi2 = 0;
signFlag = 0;
NSLog(@"This is digi1 in / = %f, %f", digi1, digi2);
}
// if operationFirst is = to + or -
if (([operationFirst isEqual:@"+"]) || ([operationFirst isEqual:@"-"])){
NSLog(@"RUN02 ");
// if operationFirst is = to + or -
if (([operation isEqual:@"+"]) || ([operation isEqual:@"-"])){
if ([@"+" isEqual:operationFirst])
{
alwaysFirstDigit = alwaysFirstDigit + digi1;
}
else if ([@"-" isEqual:operationFirst])
{
alwaysFirstDigit = alwaysFirstDigit - digi1;
}
operationFirst = operation; //
operand = alwaysFirstDigit; //
}
if (([operation isEqual:@"*"]) || ([operation isEqual:@"/"])){
NSLog(@"RUN03 ");
signFlag = 1; //
operationSecond = operation; //
NSLog(@"operationSecond = %@", operationSecond);
}
}
// it's work
if (([operationFirst isEqual:@"*"]) || ([operationFirst isEqual:@"/"])){
NSLog(@"This is digi1 in *** = %f", alwaysFirstDigit);
NSLog(@"RUN04 ");
if (([operation isEqual:@"+"]) || ([operation isEqual:@"-"])){
NSLog(@"RUN05 ");
if ([@"*" isEqual:operationFirst])
{
alwaysFirstDigit = alwaysFirstDigit * digi1;
}
else if ([@"/" isEqual:operationFirst])
{
alwaysFirstDigit = alwaysFirstDigit / digi1;
}
operationFirst = operation; //
operand = alwaysFirstDigit; //
}
**// maybe here is the problem !!!!!**
if (([operation isEqual:@"*"]) || ([operation isEqual:@"/"])){
NSLog(@"RUN06 ");
temp1 = digi1;
operationFirst = operation; //
operand = alwaysFirstDigit; //
}
}
if([operation isEqual:@"="]){
NSLog(@"This is temp1 in ***** = %f", temp1);
if ([@"+" isEqual:operationFirst])
{
alwaysFirstDigit = alwaysFirstDigit + digi1;
}
else if ([@"*" isEqual:operationFirst])
{
alwaysFirstDigit = alwaysFirstDigit * digi1;
}
else if ([@"-" isEqual:operationFirst])
{
alwaysFirstDigit = alwaysFirstDigit - digi1;
}
else if ([@"/" isEqual:operationFirst])
{
alwaysFirstDigit = alwaysFirstDigit / digi1;
}
digi1 = 0;
operand = alwaysFirstDigit;
operation = @"";
start = 0;
}
}
return operand;
}
@end