diff --git a/calculator/README.md b/calculator/README.md new file mode 100644 index 0000000..4036294 --- /dev/null +++ b/calculator/README.md @@ -0,0 +1,11 @@ +# Simple calculator application +This application asks the user to enter an operator and to two operands. Example: +``` +> Enter operator: + +> Enter operand 1: 67 +> Enter operand 2: 8 +``` +It then preceeds to print the result of the calculation: +``` +The Result is: 75 +``` \ No newline at end of file diff --git a/calculator/main.c b/calculator/main.c new file mode 100644 index 0000000..96a2c2a --- /dev/null +++ b/calculator/main.c @@ -0,0 +1,60 @@ +#include +#include +#include + +// ansi escape codes for colors +#define GREEN "\033[32m" +#define RED "\033[31m" +#define RESET "\033[0m" + +int main() { + do { + // allocate memory + double a; + double b; + char op; + + printf(GREEN"Enter operator: "RESET); + scanf(" %c", &op); // read character and ignore whitespace + // read operands + printf(GREEN"Enter operand 1: "RESET); + scanf("%lf", &a); + printf(GREEN"Enter operand 2: "RESET); + scanf("%lf", &b); + + // result is by default not a number + double res = NAN; + + switch (op) + { + case '+': + res = a + b; + break; + case '-': + res = a - b; + break; + case '*': + res = a * b; + break; + case '/': + res = a / b; + break; + + default: + printf(RED"invalid operation specified\n"RESET); + break; + } + + if (!isnan(res)) + { + printf("\nTHE RESULT IS:\n"); + + char buf[64] = {0}; + sprintf(buf, "figlet %lf | lolcat", res); + system(buf); + } + + } while(1); + + return 0; +} \ No newline at end of file