From 10780c6e8bfdb8eb7f682b57361a5928e696ba64 Mon Sep 17 00:00:00 2001 From: teridax Date: Wed, 13 Sep 2023 10:27:03 +0200 Subject: [PATCH] added calculator --- calculator/README.md | 11 ++++++++ calculator/main.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 calculator/README.md create mode 100644 calculator/main.c 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