C language: Use the parameters of the main function to implement an Integer Calculator

Source: Internet
Author: User

C language: Use the parameters of the main function to implement an Integer Calculator

/* Use the parameters of the main function to implement an Integer Calculator. The program can accept three parameters. The first parameter "-a" is used to perform addition, and the "-s" option is used to perform subtraction, the "-m" option performs multiplication, and the "-d" option executes division. The next two parameters are operands. */# Include <stdio. h> # include <stdlib. h> int my_calculator (char * p, int num1, int num2) // calculator indicates the calculator {if (p = "-a") return num1 + num2; else if (p = "-s") return num1-num2; else if (p = "-m") return num1 * num2; else if (p = "-d") return num1/num2; else return 0 ;}int main () {char a, B; char * p = & B; int num1 = 0; int num2 = 0; int result = 0; printf ("Enter the computing expression:"); scanf ("% d % c % d", & num1, & a, & num2); // input formula. num1 and num2 are operands. a stores operators. // values p values for parameter pointers, '+ ','-', '*', '/', respectively, corresponding to "-a,-s,-m,-d". If it is another symbol, p is assigned '\ 0' while (1) // while (0) indicates false. As long as it is not 0 in the brackets, the loop is always executed {if (a = '+') {p = "-"; break;} else if (a = '-') {p = "-s"; break;} else if (a = '*') {p = "-m"; break;} else if (a = '/') {p = "-d"; break ;} else * p = '\ 0';} result = my_calculator (p, num1, num2 ); // function call result printf ("% d % c % d = % d \ n", num1, a, num2, result ); // Output system ("pause"); return 0;} respectively ;}

Result: Enter the computing expression: 6/26/2 = 3. Press any key to continue...

Related Article

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.