I am just wondering if it is possible to store the basic arithmetic operators (+, -, *, /) inside variables in C. The reason that I need to do this is because I am trying to build a basic calculator program that accepts up to 5 numbers and can do any of these operations on them. Right now the only way that I have been able to come up with to do this is to store the operators inside a char array and then iterate through it using for loops and switch statements to decide what operator to use for each step. It's pretty clunky and doesn't actually work right now and I am not sure why. Also even if it did work, it wouldn't follow order of operations which I need it to do as well.
This is an example of the output of my program:
Enter a number: 4
Enter an operator (+, -, *, /, or =): +
Enter a number: 8
Enter an operator (+, -, *, /, or =): -
Enter a number: 7
Enter an operator (+, -, *, /, or =): *
Enter a number: 9
Enter an operator (+, -, *, /, or =): /
Enter a number: 2
4.00 + 8.00 - 7.00 * 9.00 / 2.00
0.500000
-6.500000
-6.500000
-3.250000
-3.250000
Result: -3.25
[Finished in 24.13s]
The first 9 lines are just taking the input for the numbers and operators, the 10th line prints out the numbers and operators entered in order, the 11th - 15th lines are supposed to print out the result after each operation which you can see are all incorrect, and the final line prints out the result. What do you think might be causing the math to be incorrect?
Here is my code at the moment:
#include <stdio.h>
#include <stdlib.h>
int main() {
double nums[5];
char operators[5];
double result;
int i = 0;
while ((i <= 4) && (operators[i - 1] != '=')) {
/* Getting the user's input */
printf("Enter a number: ");
scanf("%lf", &nums[i]);
if (i == 4) {
operators[4] = '=';
} else {
printf("Enter an operator (+, -, *, /, or =): ");
scanf(" %c", &operators[i]);
}
i++;
}
printf("%.2f %c %.2f %c %.2f %c %.2f %c %.2f\n", nums[0], operators[0], nums[1], operators[1], nums[2], operators[2], nums[3], operators[3], nums[4]);
for (i = 0; i <= 4; i++) {
/* Doing the math */
if (i == 0) {
/* Getting the value of the first two numbers */
switch(operators[i]) {
case '+' :
result = nums[0] + nums[1];
case '-' :
result = nums[0] - nums[1];
case '*' :
result = nums[0] * nums[1];
case '/' :
result = nums[0] / nums[1];
}
printf("%f\n", result);
} else {
/* Iterating through the rest of both arrays and
continuing to perform operations on the result */
switch(operators[i]) {
case '+' :
result = result + nums[i + 1];
case '-' :
result = result - nums[i + 1];
case '*' :
result = result * nums[i + 1];
case '/' :
result = result / nums[i + 1];
}
printf("%f\n", result);
}
}
// Printing out the answer rounded to 2 decimal points
printf("Result: %.2f", result);
return 0;
}
So just to re-iterate my question, is there a way that I could store the operators in a variable as operators instead of chars, which would remove the need for all of the loops, if statements, and switch statements, and if not, what changes should be made to make my code work properly?
P.S. I am only just learning C right now so suggestions for how I should be formatting my code are welcome too.