逻辑上的缺陷

时间:2014-06-05 09:58:14

标签: ios objective-c

乘法功能无法按照以下逻辑运行。 在任意两个数字相乘后我得到的输出是“无限”。这是除法逻辑的一部分。

请告诉我哪里出错了。

以下是代码。 阅读输入。

-(IBAction)clickDigit:(UIButton *) sender{

 digit = (int)sender.tag;
 currentnumber = currentnumber*10 + digit;
 display.text= [NSString stringWithFormat:@"%d",currentnumber];
 }

关于加载初始化代码

-(void)viewDidLoad {

   [super viewDidLoad];
   displayString = [NSMutableString stringWithCapacity:40];
   currentnumber = 0;
   digit = 0;
   op = 'a';
   firstOpr = YES;
   number1 = 0;
   }

乘法逻辑

 - (IBAction)clickMul
{
if(op != '*')
{
    if(op =='+')
    {
        [self clickPlus];
    }
    else if (op == '-')
    {
        [self clickMinus];
    }
    else if (op == '/')
    {
        [self clickDiv];
    }
}

if(firstOpr == YES)
{
    number1 = currentnumber;
    display.text = [NSString stringWithFormat:@"%d",number1];
    firstOpr = NO;
    digit =0;
    currentnumber = 0;
}
else
{
    number1 = number1*currentnumber;
    display.text = [NSString stringWithFormat:@"%d",number1];
    digit = 0;
    currentnumber = 0;
}
op = '*';

}

分区逻辑

 - (IBAction)clickDiv
 {
  if(op != '/')
  {
    if(op=='+')
    {
        [self clickPlus];
    }
    else if (op == '-')
    {
        [self clickMinus];

    }
    else if (op == '*')
    {
        [self clickMul];
    }
 }

if(firstOpr == YES)
{
    number1 = currentnumber;
    display.text = [NSString stringWithFormat:@"%d",number1];
    firstOpr = NO;
    digit =0;
    currentnumber = 0;
}
else
{
    if (currentnumber == 0)
    {
        display.text=[NSString stringWithFormat:@"Infinite"];
    }
    else
    {
        number1 = number1 / currentnumber;
        display.text = [NSString stringWithFormat:@"%d",number1];

    }
    digit = 0;
    currentnumber = 0;
}
op = '/';

}

0 个答案:

没有答案