simple calculator in C

logistic_guy

Senior Member
Joined
Apr 17, 2024
Messages
1,490
Using the techniques you learned in Fig. \(\displaystyle 7.28\), create a text-based, menu-driven program that allows the user to choose whether to add, subtract, multiply or divide two numbers. The program should then input two double values from the user, perform the appropriate calculation and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives two double parameters. The corresponding functions should each display messages indicating which calculation was performed, the values of the parameters and the result of the calculation.
 
Using the techniques you learned in Fig. \(\displaystyle 7.28\), create a text-based, menu-driven program that allows the user to choose whether to add, subtract, multiply or divide two numbers. The program should then input two double values from the user, perform the appropriate calculation and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives two double parameters. The corresponding functions should each display messages indicating which calculation was performed, the values of the parameters and the result of the calculation.
Please show us what you have tried and exactly where you are stuck.

Please follow the rules of posting in this forum, as enunciated at:


Please share your work/thoughts about this problem
 
In this calculator game, we will start with \(\displaystyle 4\) prototypes functions.

\(\displaystyle \bold{1.} \ \text{add}\)
\(\displaystyle \bold{2.} \ \text{subtract}\)
\(\displaystyle \bold{3.} \ \text{multiply}\)
\(\displaystyle \bold{4.} \ \text{divide}\)
 
In this calculator game, we will start with \(\displaystyle 4\) prototypes functions.

\(\displaystyle \bold{1.} \ \text{add}\)
\(\displaystyle \bold{2.} \ \text{subtract}\)
\(\displaystyle \bold{3.} \ \text{multiply}\)
\(\displaystyle \bold{4.} \ \text{divide}\)
Each prototype function will take two numbers (could be decimals) and will return nothing.

For example,

void \(\displaystyle \text{add(}\)double\(\displaystyle ,\) double\(\displaystyle )\)
 
Our C program will have three parts.

\(\displaystyle \bold{1.} \ \text{Header}\)
It will include the preprocessor directives and the prototype functions.

\(\displaystyle \bold{2.} \ \text{Body}\)
It is the main function and the actions inside it.

\(\displaystyle \bold{3.} \ \text{The implementation}\) of the prototype functions. Basically we will have \(\displaystyle 4\) implementations as we have \(\displaystyle 4\) functions.
 
Let us start by introducing the \(\displaystyle \text{Header}\).

include stdio.h

void
add(double, double)
void subtract(double, double)
void multiply(double, double)
void divide(double, double)
 
One of the main objectives in the \(\displaystyle \text{Body}\) is to let the user choose a number from \(\displaystyle 1\) to \(\displaystyle 4\).

Say, the user chose \(\displaystyle 4\), then he wants to use division. We can also use the idea that if the user suddenly decided to choose nothing, then we present him the number (digit) \(\displaystyle 0\) to exit or cancel the calculations.
 
Top