Added division by zero detection
This commit is contained in:
11
src/ee.c
11
src/ee.c
@@ -75,6 +75,11 @@ arrity(const char query)
|
||||
0;
|
||||
}
|
||||
|
||||
bool
|
||||
isDivByZero(double *_operands)
|
||||
{
|
||||
return _operands[0] == 0;
|
||||
}
|
||||
|
||||
double
|
||||
evaluate(STACK* target_stack, char operator)
|
||||
@@ -107,6 +112,9 @@ evaluate(STACK* target_stack, char operator)
|
||||
break;
|
||||
|
||||
case '/' :
|
||||
if (isDivByZero(_operands))
|
||||
error |= ERR_DIV_BY_ZERO;
|
||||
else
|
||||
value = _operands[1] / _operands[0];
|
||||
break;
|
||||
|
||||
@@ -187,6 +195,9 @@ report(STACK* expr, char* fmt)
|
||||
else if (error & ERR_INVALID_FACTORIAL)
|
||||
printf("Error: Factorials are only supported for positive integers.\n");
|
||||
|
||||
else if (error & ERR_DIV_BY_ZERO)
|
||||
printf("Undefined\n");
|
||||
|
||||
else
|
||||
printf(fmt, peek(expr));
|
||||
}
|
||||
|
||||
2
src/ee.h
2
src/ee.h
@@ -1,5 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
@@ -9,6 +10,7 @@ const int ERR_NS_OPERANDS = 1;
|
||||
const int ERR_NS_OPERATORS = 2;
|
||||
const int ERR_INVALID_INPUT = 4;
|
||||
const int ERR_INVALID_FACTORIAL = 8;
|
||||
const int ERR_DIV_BY_ZERO = 16;
|
||||
|
||||
|
||||
struct EE_STACK_NODE {
|
||||
|
||||
Reference in New Issue
Block a user