开发者

Tutorial for iPhone calculator with decimal point [duplicate]

开发者 https://www.devze.com 2023-04-07 04:31 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How do I change the number of decimal places iOS?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How do I change the number of decimal places iOS?

If using Xcode to make an iOS calculator, how would I add a decimal button?

I am looking for an iOS tutorial on how to make a (simple) iPhone calculator that can do at least the following:add, subtract, multiply, divide, and lets the user input decimal points. I have not been able to find a tutorial like this, so if anyone knows of one, please let me know. I actually did find one where you can add, subtract, multiply, divide, but not do decimal points. If you can tell me how to let the user input a decimal point, there is no need for a further tutorial. Th开发者_如何学Ce code i am using right now is below.

in the header file:

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController {

float result;
IBOutlet UILabel *calculatorScreen;
int currentOperation;
float currentNumber;

}


-(IBAction)buttonDigitPressed:(id)sender;
-(IBAction)buttonOperationPressed:(id)sender;
-(IBAction)cancelInput;
-(IBAction)cancelOperation;

in the main file:

#import "CalculatorViewController.h"

@implementation CalculatorViewController


-(IBAction)buttonDigitPressed:(id)sender {


currentNumber = currentNumber *10 + (float)[sender tag];
calculatorScreen.text = [NSString stringWithFormat:@"%.1f", currentNumber];

}

-(IBAction)buttonOperationPressed:(id)sender {
if (currentOperation ==0) result = currentNumber;
else {        

    switch (currentOperation) {
        case 1:
            result = result + currentNumber;
            break;
        case 2:
            result = result - currentNumber;
            break;
        case 3:
            result = result * currentNumber;
            break;
        case 4:
            result = result / currentNumber;
            break;
        case 5:
            currentOperation = 0;
            break;

    }
}

currentNumber = 0;
calculatorScreen.text = [NSString stringWithFormat:@"%.1f", result];
if ([sender tag] ==0) result=0;
currentOperation = [sender tag];
}



-(IBAction)cancelInput {

currentNumber =0;
calculatorScreen.text = @"0";

 }


-(IBAction)cancelOperation {



currentNumber = 0;
calculatorScreen.text = @"0";
currentOperation = 0;

}


This should work. Simple and direct.

Changed the currentNumber to store the current string value of the ongoing calculation (Make this NSMutableString), added an outlet (buttonDecialPointPressed) for the decimal point button.

-(IBAction) buttonDigitPressed:(id)sender
{
    currentNumber = [currentNumber appendFormat:@"%d"];
    calculatorScreen.text = currentNumber;
}

- (IBAction) buttonDecialPointPressed:(id)sender
{
    if(behindTheDecimal == YES)
        return;
    behindTheDecimal = YES;
    currentNumber = [currentNumber appendString:@"."];
    calculatorScreen.text = currentNumber;
}

-(IBAction)buttonOperationPressed:(id)sender
{
    float number = [currentNumber floatValue];
    switch (currentOperation)
    {
        case 0:
            result = number;
            break;
        case 1:
            result = result + number;
            break;
        case 2:
            result = result - number;
            break;
        case 3:
            result = result * number;
            break;
        case 4:
            result = result / number;
            break;
        case 5:
            currentOperation = 0;
            break;
        case 6:
            break;
    }

    currentNumber = [currentNumber stringWithFormat:@"%f", result];
    calculatorScreen.text = currentNumber;

    // why is this needed, how is it used?
    currentOperation = [sender tag];
}


-(IBAction)cancelInput
{
    behindTheDecimal = NO;
    currentNumber = @"0";
    calculatorScreen.text = currentNumber;  
}

-(IBAction)cancelOperation
{
    behindTheDecimal = NO;
    currentNumber = @"0";
    calculatorScreen.text = currentNumber;
    currentOperation = 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消