Argument-based calculator in JM-

Here's a simple argument-based calculator made in JM-

// Simple Argument-based JM- Calculator Example
#incd .jargh // Include JMDash Argument Handler Library

// Create needed variables
str programName, num1, num2;
char op;
bool argCheck;

// Store program's name
arg.store{<< [0] >> programName};

// Check if any arguments were parsed
if (argCheck == true) : {
    arg.store{<< [1] >> num1, << [2] >> op, << [3] >> num2}; // Store numbers and operator
    // Calculate accordingly
    switch.char(op) {
        1*'+':
            con.out{<< num1 + num2};
            sgo;
        2*'-':
            con.out{<< num1 - num2};
            sgo;
        3*'*':
            con.out{<< num1 * num2};
            sgo;
        4*'/':
            con.out{<< num1 / num2};
            sgo;
        default:
            con.out{<< "Invalid operator :\n"};
            con.out{<< "Correct usage: " << programName << " <firstNumber> <+|-|*|/> <secondNumber>"};
            sgo;
    };
}
else : {
    con.out{<< "Invalid argument/s!\n"};
    con.out{<< "Correct usage: " << programName << " <firstNumber> <+|-|*|/> <secondNumber>"};
};

Last updated