Question

I want to make command line calculator using XCode/OSX application/Command line tool/Foundation type. In XCode, go to Products/Scheme/Edit Scheme. In this, we can add or delete command line arguments. These command line arguments are stored in argument vector i.e. argv[].
I am using NSArray to store these arguments in Objective-C array.
Now, I want to make calculator which can evaluate expression.
For example my arguments are argv[1]=5, argv[2]=+, argv[3]= 10, argv[4]=-, argv[5]=2. So, these arguments will evaluate the expression and give result. Result=13.

#import <Foundation/Foundation.h>

int main(int argc, const char* argv[])
{

    @autoreleasepool {
        NSArray *myarray =[[NSProcessInfo processInfo] arguments];

        for (int i=1; i<argc ; i++) {
            NSLog (@"Arguents %d=%@", i, myarray[i]);
        }

        return 0;
    }
}
Was it helpful?

Solution

Here's a simple calculator, knocked-up in a few minutes:

#import <Foundation/Foundation.h>

typedef enum {
    OP_NONE,
    OP_ADD,
    OP_SUB,
    OP_MULT,
    OP_DIV
} Op;

static int calc(NSArray *args) {
    Op op = OP_NONE;
    int result = 0;
    for (NSString *arg in args) {
        if ([arg isEqualToString:@"+"]) {
            op = OP_ADD;
        } else if ([arg isEqualToString:@"-"]) {
            op = OP_SUB;
        } else if ([arg isEqualToString:@"*"]) {
            op = OP_MULT;
        } else if ([arg isEqualToString:@"/"]) {
            op = OP_DIV;
        } else {
            int value = [arg intValue];              // NO ERROR CHECKING!!!
            switch(op) {
                case OP_ADD: result += value; break;
                case OP_SUB: result -= value; break;
                case OP_MULT: result *= value; break;
                case OP_DIV: result /= value; break;
                case OP_NONE: result = value; break;
                default: abort();
            }
            op = OP_NONE;
        }
    }
    return result;
}

int main(int argc, const char **argv) {
    @autoreleasepool {
        NSMutableArray *args = [NSMutableArray new];
        for (int i = 1; i < argc; i++)
            [args addObject:@(argv[i])];
        NSLog(@"result = %d", calc(args));
    }
    return 0;
}

Compile with:

$ clang -DDEBUG=1 -g -fobjc-arc -o calc calc.m -framework Foundation

Tests:

typhon:tinkering (master) $ ./calc 3 + 9
2014-04-26 13:23:05.628 calc[8728:507] result = 12
typhon:tinkering (master) $ ./calc 2 / 1
2014-04-26 13:23:20.500 calc[8738:507] result = 2
typhon:tinkering (master) $ ./calc 99 / 11
2014-04-26 13:23:25.364 calc[8742:507] result = 9
typhon:tinkering (master) $ ./calc 99 / 12
2014-04-26 13:23:27.740 calc[8746:507] result = 8
typhon:tinkering (master) $ ./calc 99 \* 11
2014-04-26 13:23:53.588 calc[8754:507] result = 1089

Notes:

  • It's only for integer maths at the moment, but would be easy to convert for floating point.
  • There is no error checking when parsing the number.
  • If you want to do multiplication you need to specify \* as * is a symbol to do shell globbing.
  • You don't need NSProcessInfo to get the command line arguments as they are passed to main().
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top