Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.0 kB
4
Indexable
#include <stdio.h>
#include <math.h>

const char  binary_operators[5] = {'+', '-', '*', '/', '^'},
            unaryOperators[4] = {'#', '%', '!', 'q'};

void run_calculator();
void scan_data(char* operator, double* input);
double do_next_op(char operator, double input, double start, int* final);
double runAddition(double x, double y);
double runSubstraction(double x, double y);
double runMultiplication(double x, double y);
double runDivision(double x, double y);
double runPotency(double x, double y);
double runSquareRot(double x);
double runReverse(double x);
double runDivisonWithOne(double x);

int main(void) {
    run_calculator();

    return 0;
}

void run_calculator() {
    char operator;
    double input;
    double start = 0.0;
    int final = 0;

    while (final == 0) {
        printf("\nEnter operator, and an optional operand:\n");
        scanf("%c", &operator);
        scan_data(&operator, &input);
        start = do_next_op(operator, input, start, &final);
        if (operator == unaryOperators[3]) {
            printf("The final calculation is: %lf", start);
        }
        else {
            printf("Result so far is %lf", start);
        }
    }
}

void scan_data(char* operator, double* input) {

    for (int i = 0; i <=4; i++) {
        if (*operator == binary_operators[i]) {
            scanf("%lf", &*input);
            break;
        }
    }
}

double do_next_op(char operator, double input, double start, int* final) {

    if (operator == '+') {
        start = runAddition(start, input);
    }
    else if (operator == '-') {
        start = runSubstraction(start, input);
    }
    else if (operator == '*') {
        start = runMultiplication(start, input);
    }
    else if (operator == '/') {
        start = runDivision(start, input);
    }
    else if (operator == '^') {
        start = runPotency(start, input);
    }
    else if (operator == '#') {
        start = runSquareRot(start);
    }
    else if (operator == '%') {
        start = runReverse(start);
    }
    else if (operator == '!') {
        start = runDivisonWithOne(start);
    }
    else if (operator == 'q') {
        *final = 1;
    }
    return start;
}

double runAddition(double x, double y) {
    double result = x + y;
    return result;
}

double runSubstraction(double x, double y) {
    double result = x - y;
    return result;
}

double runMultiplication(double x, double y) {
    double result = x * y;
    return result;
}

double runDivision(double x, double y) {
    double result = x / y;
    return result;
}

double runPotency(double x, double y) {
    double result = pow(x, y);
    return result;
}

double runSquareRot(double x) {
    double result = sqrt(x);
    return result;
}

double runReverse(double x) {
    double result = (-(x));
    return result;
}

double runDivisonWithOne(double x) {
    double result = (1 / x);
    return result;
}