Basic Math program (environment: C++)
// Program called basicMath calculates two numbers in case valid inputs
#include <iostream>
#include <cstdlib>
#define add(a,b) a+b
#define subtract(a,b) a-b
#define multiply(a,b) a*b
#define divide(a,b) a/b
bool isValid(int argc, char* argv[]) { // function to check validation of the number of input variables, the sign variable, and the number variables
int i = 1;
int j;
bool checkVari = false;
int checkNum = 0;
int checkSum = strlen(argv[1]) + strlen(argv[3]);
if((argc == 4) && (*argv[2] == '+' || *argv[2] == '-' || *argv[2] == '*' || *argv[2] == '/')) {
checkVari = true;
for(;i<argc;i++) {
for(j=0;j<strlen(argv[i]);j++) {
if(i!=2) {
if(argv[i][j] >= 48 && argv[i][j] <= 57) {
checkNum += 1;
}
}
}
}
}
return (checkVari == true && checkNum == checkSum)?true:false;
}
int main(int argc, char* argv[]) {
if(isValid(argc, argv)) {
int arg1 = atof(argv[1]);
int arg3 = atof(argv[3]);
if(*argv[2] == '+') {
std::cout << add(arg1,arg3) << std::endl;
}
else if(*argv[2] == '-') {
std::cout << subtract(arg1,arg3) << std::endl;
}
else if(*argv[2] == '*') {
std::cout << multiply(arg1,arg3) << std::endl;
}
else {
std::cout << divide(arg1,arg3) << std::endl;
}
}
else {
std::cout << "bm <number> <+-*/> <number><Enter>" << std::endl;
}
return 0;
}