89 lines
No EOL
2.6 KiB
C
89 lines
No EOL
2.6 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <float.h>
|
|
#include <stdbool.h>
|
|
#define FIRST_OPTION 1
|
|
#define SECOND_OPTION 2
|
|
#define THIRD_OPTION 3
|
|
#define FOURTH_OPTION 4
|
|
#define FIFTH_OPTION -1
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
int user_choice;
|
|
while (true)
|
|
{
|
|
bool dont_pass = true;
|
|
printf("Choose one of the following operations:\n");
|
|
printf("\tAdd (%d)\n", FIRST_OPTION);
|
|
printf("\tSubtract (%d)\n", SECOND_OPTION);
|
|
printf("\tMultiply (%d)\n", THIRD_OPTION);
|
|
printf("\tDivide (%d)\n", FOURTH_OPTION);
|
|
printf("\tStop program (%d)\n", FIFTH_OPTION);
|
|
do
|
|
{
|
|
printf("Enter your choice: ");
|
|
scanf("%d", &user_choice);
|
|
dont_pass = user_choice < FIFTH_OPTION || user_choice > FOURTH_OPTION || user_choice == 0;
|
|
if (dont_pass)
|
|
{
|
|
printf("Input not allowed, please try again\n");
|
|
}
|
|
|
|
} while (dont_pass);
|
|
|
|
if(user_choice == -1) return 0;
|
|
|
|
double first_operand;
|
|
double second_operand;
|
|
dont_pass = true;
|
|
do
|
|
{
|
|
printf("Please enter the first operand: ");
|
|
scanf("%lf", &first_operand);
|
|
printf("Please enter the second operand: ");
|
|
scanf("%lf", &second_operand);
|
|
if(user_choice == FOURTH_OPTION && second_operand == 0)
|
|
{
|
|
printf("Division by zero\n");
|
|
}
|
|
else if (first_operand < DBL_MIN || second_operand < DBL_MIN)
|
|
{
|
|
printf("Number underflow\n");
|
|
}
|
|
else if(first_operand > DBL_MAX || second_operand > DBL_MAX)
|
|
{
|
|
printf("Number overflow\n");
|
|
}
|
|
else
|
|
{
|
|
dont_pass = false;
|
|
}
|
|
}
|
|
while (dont_pass);
|
|
|
|
double result;
|
|
switch(user_choice)
|
|
{
|
|
case FIRST_OPTION:
|
|
result = first_operand + second_operand;
|
|
printf("The result is %.3f\n", result);
|
|
break;
|
|
case SECOND_OPTION:
|
|
result = first_operand - second_operand;
|
|
printf("The result is %0.3f\n", result);
|
|
break;
|
|
case THIRD_OPTION:
|
|
result = first_operand * second_operand;
|
|
printf("The result is %0.3f\n", result);
|
|
break;
|
|
case FOURTH_OPTION:
|
|
result = first_operand / second_operand;
|
|
printf("The result is %2.3f\n", result);
|
|
break;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
return 0;
|
|
} |