[C Language] analog Calculator

Source: Internet
Author: User

[C Language] analog Calculator

`# Include <stdio. h> # include <stdlib. h> int _ add (int a, int B) {return a + B;} int _ sub (int a, int B) {return a-B ;} int _ mul (int a, int B) {return a * B;} int _ div (int a, int B) {if (B = 0) {printf ("except digit 0 \ n");} return a/B;} void menu () {printf ("************************* \ n "); printf ("***** 1.add 2.sub ***** \ n"); printf ("***** 3.mul 4.div ***** \ n "); printf ("********* 0. exit ********* \ n "); printf ("************************** \ n");} int main () {int input = 1; int num1 = 0; int num2 = 0; int ret = 0; while (input) {menu (); printf ("SELECT> :"); scanf ("% d", & input); printf ("Please input two integer operands>:"); scanf ("% d", & num1, & num2 ); 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 ("incorrect selection. Please reselect it. \ n "); break;} printf (" ret = % d \ n ", ret);} return 0 ;}in addition, you can also use the enumeration structure (enum) to implement: # include <stdio. h> # include <stdlib. h> enum OP {EXIT, ADD, SUB, MUL, DIV}; int _ add (int a, int B) {return a + B;} int _ sub (int, int B) {return a-B;} int _ mul (int a, int B) {return a * B;} int _ div (int a, int B) {if (B = 0) {printf ("except digit 0 \ n") ;}return a/B;} void menu () {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}; int main () {int input = 1; int num1 = 0; int num2 = 0; int ret = 0; while (input) {menu (); printf ("SELECT> :"); scanf ("% d", & input); printf ("Please input two integer operands>:"); scanf ("% d", & num1, & num2 ); if (input> = 1 & input <= 4) {ret = pfun [input] (num1, num2);} else if (input = 0) {exit (EXIT_FAILURE);} else {printf ("select error \ n");} printf ("ret = % d \ n", ret);} return 0 ;}`

Related Keywords:
Related Article

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.