Write a function in C to implement an integer computer, the program accepts three parameters, the first parameter is an action item, and the last two parameters are operands.

Source: Internet
Author: User

function function:

Using the parameters of the main function, implement an integer computer, the program can accept three parameters, the first parameter "-a" option to perform the addition, "-S" option to perform the subtraction, "-M" option to perform the multiplication, "-d" option to perform the division, the following two parameters are the operands.


Thinking:

Three parameters in the two operands do not have to process, direct incoming can, and for the first operand needs to be processed, to ensure that the incoming parameter is "'-a ', '-s ', '-M ', '-d '", in order to deal with the calculation, the first step to convert the operation symbol to the above, and then the parameters and

The procedure is as follows:

/***1. Using the parameters of the main function to implement an integer computer, the program can accept three parameters, * * The first parameter "-a" option to perform the addition, "-S" option to perform the subtraction, * * "-m" option to perform the multiplication, "-d" option to perform the division, the following two parameters are operands. * * #include <stdio.h> #include <stdlib.h> #include <assert.h>int my_math (char *p,  INT&NBSP;NUM1,&NBSP;INT&NBSP;NUM2) {assert (P);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; printf ("Please enter the calculation:"), scanf ("%d%c%d", &num1,&a,&num2),   //input calculation, num1,num2 as operand, a holds operator while   (1)       //respectively to the parameter pointer p assignment, ' + ', '-', ' * ', '/', respectively, corresponding to "-a,-s,-m,-d", if the other symbol team p   (a ==  ' + ') {p=  "-A"  ;break;} else if  (a ==  '-') {p =  "-S"; Else if  (a ==  ' * ') {p =  "-M"; else if  (a ==  '/') {p =  "-D"; else *p =  ' + ';} Int result = my_math (p, num1, num2);     //function call to get the result. printf ("%d%c%d=%d\n", Num1,a,num2,result),   //output system ("pause"); return 0;}

After verification, the results are correct, note: input arithmetic type when do not enter the equal sign, if you want to calculate 5 plus 3, enter "5+3" Can!


Welcome the Great God to criticize you!

This article is from the "Sharing Progress" blog, be sure to keep this source http://xmwen1.blog.51cto.com/10730069/1712714

Write a function in C to implement an integer computer, the program accepts three parameters, the first parameter is an action item, and the last two parameters are operands.

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.