Files
Laboratori-TDP/Laboratorio 1/calcolatrice.c
2024-03-22 17:14:57 +01:00

43 lines
710 B
C

#include <stdio.h>
int main()
{
char operation;
float op1, op2, res;
operation = getchar();
if (scanf("%f %f", &op1, &op2) != 2) {
printf("Error: Invalid input\n");
return 2;
}
switch (operation)
{
case '+':
res = op1 + op2;
break;
case '-':
res = op1 - op2;
break;
case '*':
res = op1 * op2;
break;
case '/':
if (op2 == 0) {
printf("Error: divide by zero\n");
return 2;
}
res = op1 / op2;
break;
default:
printf("Error: invalid operation\n");
return 1;
}
printf("%c %f\n", operation, res);
return 0;
}