Question

Im trying to make a calculator app. I want it to conform with thing like order of operations, etc. The way I thought do this is to have the numbers and the operation added to the end of a double when an operation is pressed. I can't find any way to add numbers and an operation (+,-,*,/) to the end of a double, or any number related class in objective c.

If you know of any way to do this, or have a different idea on how to do this that would be awesome. Thanks.

Was it helpful?

Solution

This is not a good approach on many levels. First, just adding an operation to the "end" of a number (whatever you mean by "end" here) does not get you to order-of-operation. You need to keep track of the entire expression (or at least more than just the last operand). Since you're working in ObjC, you'll probably want an object-oriented approach to manage your expressions. Google for "object oriented calculator." There are many examples. (Do not worry whether the example is in ObjC; you should be able to apply the same object model in any OOP language.) @trojanfoe offers the beginnings of this, but getting to correct order-of-operation is more complicated. Luckily, it's been solved many times. See DDMathParser for one ObjC solution.

The next thing that's going to bite you is that double is a dangerous type for a calculator. Your user expects to work in Base-10. double works in Base-2. There are going to be rounding errors, and you wind up with results like 4.999999999 when you clearly meant 5. If you intend to display decimal, then work in decimal. Cocoa offers the NSDecimalNumber class for this, as well as the NSDecimal Foundation type.

OTHER TIPS

As the user types in their operations, you need to collect them, so you need a custom object for each operation:

typedef enum {
    CalcOperatonTypeValue,    // value is valid
    CalcOperatonTypeAdd,      // +
    CalcOperatonTypeSub,      // -
    CalcOperatonTypeMult,     // *
    CalcOperatonTypeDiv       // %
} CalcOperationType;

@interface CalcOperation : NSObject
@property (assign) CalcOperationType type;
@property (assign) double value;
@end

Then collect these into a mutable array as the user performs their operation (note that CalcOperation.value is only valid when type is CalcOperatonTypeValue).

And then when you want to know what the current value is (to display), perform something like this:

double total = 0.0, lastValue = 0.0;
for (CalcOperation *op in operations) {
    switch (op.type) {
    case CalcOperatonTypeValue:
        lastValue = op.value;
        break;
    case CalcOperatonTypeAdd:
        total += lastValue;
        break;
    case CalcOperatonTypeSub:
        total -= lastValue;
        break;
    case CalcOperatonTypeMult:
        total *= lastValue;
        break;
    case CalcOperatonTypeDiv:
        total /= lastValue;
        break;
    default:
        NSAssert(NO, @"Invalid operation type");
        break;
    }
}

Note: there is a lot more error checking that needs to be done before this becomes solid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top