计算器中的正数到负数和正数

时间:2014-04-17 17:24:07

标签: objective-c

我刚开始使用Objective-C,我的第一个应用程序就是完全可操作的基本计算器。

Haven说。当输入两个输入然后按下等于按钮时,我已经使它工作加,减,乘和除。但现在为+/-操作和百分比%。我希望他们能够自我操作。

怎么样?好吧,我希望他们工作,因为他们在运行iOS7计算器的iPhone上工作,当你输入一个数字,然后按+/-或百分比输出立即显示为结果,而不需要输入另一个数字并按下等号

我已经尝试过按照我一直使用的方法,但是我必须按两次按钮才能显示不是我正在寻找的显示器。

这是我的代码..

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(IBAction)Number1:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 1;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number2:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 2;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number3:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 3;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number4:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 4;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number5:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 5;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number6:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 6;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number7:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 7;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number8:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 8;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number9:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 9;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}
-(IBAction)Number0:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 0;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];

}

-(IBAction)Times:(id)sender{

    if (RunningTotal == 0) {
        RunningTotal = SelectNumber;
    } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            case 6:
                if(RunningTotal > 0){
                    RunningTotal = RunningTotal * -1;
                } else{
                    RunningTotal = RunningTotal * +1;
                }
                break;
            default:
                break;
        }
    }

    Method = 1;
    SelectNumber = 0;

}
-(IBAction)Divide:(id)sender{

    if (RunningTotal == 0) {
        RunningTotal = SelectNumber;
    } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            case 6:
                if(RunningTotal > 0){
                    RunningTotal = RunningTotal * -1;
                } else{
                    RunningTotal = RunningTotal * +1;
                }
                break;
            default:
                break;
        }
    }

    Method = 2;
    SelectNumber = 0;

}
-(IBAction)Substract:(id)sender{

    if (RunningTotal == 0) {
        RunningTotal = SelectNumber;
    } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            case 6:
                if(RunningTotal > 0){
                    RunningTotal = RunningTotal * -1;
                } else{
                    RunningTotal = RunningTotal * +1;
                }
                break;
            default:
                break;
        }
    }

    Method = 3;
    SelectNumber = 0;

}
-(IBAction)Plus:(id)sender{

    if (RunningTotal == 0) {
        RunningTotal = SelectNumber;
    } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            case 6:
                if(RunningTotal > 0){
                    RunningTotal = RunningTotal * -1;
                } else{
                    RunningTotal = RunningTotal * +1;
                }
                break;
            default:
                break;
        }
    }

    Method = 4;
    SelectNumber = 0;

}

- (IBAction)Percent:(id)sender {

    if (RunningTotal == 0) {
        RunningTotal = SelectNumber;
    } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            case 6:
                if(RunningTotal > 0){
                    RunningTotal = RunningTotal * -1;
                } else{
                    RunningTotal = RunningTotal * +1;
                }
                break;
            default:
                break;
        }
    }

    Method = 5;
    SelectNumber = 0;
    Screen.text = [NSString stringWithFormat:@"%.2f", RunningTotal];

}

- (IBAction)PositiveOrNegative:(id)sender {

    if (RunningTotal == 0) {
        RunningTotal = SelectNumber;
    } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            case 6:
                if(RunningTotal > 0){
                    RunningTotal = RunningTotal * -1;
                } else{
                    RunningTotal = RunningTotal * +1;
                }
                break;
            default:
                break;
        }
    }

    Method = 6;
    SelectNumber = 0;
    Screen.text = [NSString stringWithFormat:@"%.2f", RunningTotal];


}


- (IBAction)Dot:(id)sender {



}

-(IBAction)Equals:(id)sender{

    if (RunningTotal == 0) {
        RunningTotal = SelectNumber;
    } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            default:
                break;
        }
    }

    Method = 0;
    SelectNumber = 0;
    Screen.text = [NSString stringWithFormat:@"%.2f", RunningTotal];

}


-(IBAction)AllClear:(id)sender{

    Method = 0;
    RunningTotal = 0;
    SelectNumber = 0;

    Screen.text = [NSString stringWithFormat:@"0"];

}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

嗯,任何帮助或建议都非常感谢。如果您需要有关我的帖子的更多信息或任何可能有助于更好地理解我的问题的问题,请告诉我,我会提供给他们.. :)

1 个答案:

答案 0 :(得分:0)

这看起来很像一个错误:

        case 6:
            if(RunningTotal > 0){
                RunningTotal = RunningTotal * -1;
            } else{
                RunningTotal = RunningTotal * +1;
            }

我认为你不想乘以+1 ......翻转负数的符号,你也会乘以-1

我还建议在函数中封装Method上的重复开关。

此外,你还有一些其他令人不满意的事情 - Objective-C正在进行......你应该用setter和getter修改实例变量。您应该使用@property为您创建iVar和访问器。你的iVars和函数应该以小写字母开头,而类以大写字母开头。您的ViewController类应具有特定于功能的名称。您的ViewController类应该有自己的前缀,因为Objective C不支持名称空间。