This calculator can be used for "subtraction" operations:
The code is as follows:
#include <stdio.h>
#include <stdlib.h>
Enum OP//enumeration of individual functions
{
EXIT,
ADD,
SUB,
MUL,
Div
};
int _add (int a, int b)//Implement addition function
{
return a + B;
}
int _sub (int a, int b)//Implement subtraction function
{
return a-B;
}
int _mul (int a, int b)//Implement multiply rows
{
return a*b;
}
int _div (int a, int b)//Implement Division function
{
if (b = = 0)
{
printf ("except digital 0\n");
}
return a/b;
}
void menu ()//Menus function for implementing the menu bar
{
printf ("*************************\n");
printf ("* * * 1.add 2.sub ****\n");
printf ("* * * 3.mul 4.div ****\n");
printf ("******** 0.exit *********\n");
printf ("*************************\n");
}
Int (*pfun[5]) (int, int) = {0, _add, _sub, _mul, _div}; Defines an array pointer type function
int main ()
{
int input = 1;
int NUM1 = 0;
int num2 = 0;
int ret = 0;
while (input)//Select the action to perform
{
menu (); Print Menu
printf ("Please select";: ");
scanf ("%d", &input);
printf ("Please enter two integer operands;:");
scanf ("%d%d", &num1, &num2);
if (input >= 1 && input <= 4)
{
ret = Pfun[input] (NUM1, num2); Call the array pointer function and perform the corresponding operation
}
else if (input = = 0)//exit Program
{
Exit (Exit_failure);
}
Else
{
printf ("Select Error \ n"); The selection is wrong
}
printf ("ret =%d\n", ret); Results of the output calculation
}
return 0;
}
In the main function while loop, you can also use the switch statement to do the corresponding operation
The code is as follows:
switch (input)
{
Case 1:
ret = _add (NUM1, num2);
Break
Case 2:
ret = _sub (NUM1, num2);
Break
Case 3:
ret = _mul (NUM1, num2);
Break
Case 4:
ret = _div (NUM1, num2);
Break
Case 0:
Exit (Exit_failure);
Break
Default
printf ("Select Error, please re-select. \ n");
Break
}
This article is from the "0-point Time" blog, please make sure to keep this source http://10741764.blog.51cto.com/10731764/1716870
Implement a simple calculator