38 lines
1.0 KiB
C
38 lines
1.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
void help() {
|
|
printf("Commands\n'add' - Add two numbers together. Ex 'add 3 5'\n"
|
|
"'sub' - subtract two numbers. Ex 'sub 6 4'\n"
|
|
"'multi' - multiply two numbers. Ex 'multi 4 2'\n"
|
|
"'div' divide two numbers. Ex 'div 2 1'\n");
|
|
}
|
|
|
|
int add(const int numOne, const int numTwo) {
|
|
return numOne + numTwo;
|
|
}
|
|
|
|
int main(void) {
|
|
while (1) {
|
|
char cmd[24];
|
|
printf("Welcome to the BasicCalc, written in C\nBasic calculator functionality (add, subtract, multiply, and divide)\nType 'help' to begin!\n");
|
|
scanf("%s", cmd);
|
|
|
|
if (strcmp(cmd, "help") == 0) {
|
|
help();
|
|
|
|
} else if (strcmp(cmd, "add") == 0) {
|
|
char * args = strtok(cmd, " ");
|
|
while (args != NULL) {
|
|
int numOne = atoi(args[1]);
|
|
int numTwo = atoi(args[2]);
|
|
|
|
int finalNum = add(numOne, numTwo);
|
|
printf("Your number is: %d", finalNum);
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
} |