Vorkus-Programmieren-2023/calculator/main.c

60 lines
1.2 KiB
C

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// 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;
}