我正在研究计算器的课堂作业。我差不多完成了。
现在,我的计算器可以做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
答案 0 :(得分:0)
在您评论的部分下:
// if operationFirst is = to + or -
if子句中有一些代码开始:
if (([operation isEqual:@"*"]) || ([operation isEqual:@"/"])){
此代码永远不会运行,因为它包含在一个if块中:
if (([operationFirst isEqual:@"+"]) || ([operationFirst isEqual:@"-"])){
NSLog(@"RUN02 ");
我不知道这是不是你的问题。您还可能希望将解析器和语法用于您尝试实现的计算器语言。阿尔弗雷德·阿霍(Alfred Aho)和其他人的书“编纂者,原理,技术和工具”称之为“龙书”,因为它在封面上有数十年的龙,是制作和理解语法分析器的传统来源,如桌面计算器(这是它使用的一个例子),这正是你想要编写的代码。除了为桌面计算器提供完美的语法外,它还可以多次增加您对编译器的了解。