0

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;
    }
}
sharkbait
  • 2,980
  • 16
  • 51
  • 89
user3575678
  • 115
  • 1
  • 9

1 Answers1

1

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().
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • This is perfectly working. Thanks for the suggestion. But can I make this calculator using different classes for Addition, multiplication, division, subtraction, modulus. I want to make one main class i.e. calculator and call all the classes in calculator class. And Execute that in main class. And I also want to use modulus operator in my calculator. And one more thing is it possible to do multiplication, division , and modulus first and then addition , subtraction. Like the priority check meaning higher priority operators should be done first and then less priority operators. – user3575678 Apr 26 '14 at 23:58
  • @user3575678 I don't think it makes sense to use different classes for different math operations, unless you wanted to learn some aspects of object-oriented programming and experiment with polymorphism, given the operations are so trivial, however it's your code so you can do as you like. In terms of operator associativity then that is something that effects a full-scale parsing framework (look-up *Parsing* on wikipedia) and not something that fits easily into the simple example I give, as there is no concept of grouping. As far a modulo is concerned, that is a trivial change. – trojanfoe Apr 27 '14 at 00:44
  • Sorry, It was my mistake. I want to make one class called calculator and want to define methods in that class and call the calculator class in main class. All I want to do is to evaluate this math expression using class concept. – user3575678 Apr 27 '14 at 00:57