Implement a simple calculator

Source: Internet
Author: User
Tags mul

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.