Skip to content

Instantly share code, notes, and snippets.

@stephyswe
Created April 20, 2023 12:43
Show Gist options
  • Save stephyswe/8c807c09fa8742c9f51347de68c0c931 to your computer and use it in GitHub Desktop.
Save stephyswe/8c807c09fa8742c9f51347de68c0c931 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "input.h"
typedef enum {
Calculator_Status_Ok = 0,
Calculator_Status_InvalidInput = 1,
Calculator_Status_DivideByZero = 2
} Calculator_Status;
#define OPERATOR_ADD '+'
#define OPERATOR_SUB '-'
#define OPERATOR_MUL '*'
#define OPERATOR_DIV '/'
#define OPERATOR_MOD '%'
typedef double (*OperatorFunction)(double, double);
double add(double num1, double num2) { return num1 + num2; }
double subtract(double num1, double num2) { return num1 - num2; }
double multiply(double num1, double num2) { return num1 * num2; }
double divide(double num1, double num2) { return num1 / num2; }
double modulus(double num1, double num2) { return fmod(num1, num2); }
#define GET_INPUT(prompt, var) \
while ((status = getInputValue(prompt, &var)) != Calculator_Status_Ok) { \
printf("Invalid input for " #var ". Please try again.\n"); \
}
char getValidOperator() {
const char* VALID_OPERATORS = "+-*/%";
char op;
while (1) {
printf("Enter operator (%s): ", VALID_OPERATORS);
scanf(" %c", &op);
if (strchr(VALID_OPERATORS, op) != NULL) {
return op;
} else {
printf("Invalid operator. Please try again.\n");
}
}
}
OperatorFunction opFunction = NULL;
typedef struct {
const char* operatorString;
OperatorFunction operatorFunction;
} OperatorMapping;
OperatorMapping operatorMappings[] = {{"+", add},
{"-", subtract},
{"*", multiply},
{"/", divide},
{"%", modulus}};
OperatorFunction getOperatorFunction(char op) {
for (int i = 0; i < sizeof(operatorMappings) / sizeof(OperatorMapping);
i++) {
if (op == *(operatorMappings[i].operatorString)) {
return operatorMappings[i].operatorFunction;
}
}
return NULL;
}
void calculatorMenu() {
printf("Calculator\n");
double num1, num2, result;
char op;
Calculator_Status status = Calculator_Status_Ok;
GET_INPUT("num1: ", num1);
GET_INPUT("num2: ", num2);
op = getValidOperator();
// Perform the operation
result = getOperatorFunction(op)(num1, num2);
if (op == '/' || op == '%') {
if (num2 == 0) {
printf("Error: Division by zero.\n");
return;
}
}
printf("%.2lf %c %.2lf = %.2lf\n", num1, op, num2, result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment