Exercise 2-9 four operations of integers (10 points)
This problem requires programming to calculate the sum, difference, product, quotient and output of two positive integers. The topic ensures that the input and output are all in the integer range.
Input format:
Input gives 2 positive integers A and B on A single line.
Output format:
Output sum, difference, product and quotient in the order of "A operator B = result" in 4 lines.
Input example:
3 2
Output example:
3 + 2 = 5 3 - 2 = 1 3 * 2 = 6 3 / 2 = 1
Analysis: this problem is the most common four mixed integer operations. The general idea is to write "add, subtract, multiply, divide" four functions respectively, and then call them in turn. However, since you have known "function pointer" before, why not use this opportunity to consolidate this knowledge?
(1) Build an array of function pointers, the parameters are the function names of the four functions, and then use for loop to traverse the array of pointers, so as to realize the sequential call of the four functions
(2) In addition, write a function Val, and take the function pointer as a parameter. According to different situations, take different function names as arguments to call the Val function, and transfer the function entry address to the parameter in the function val (the parameter is the pointer variable to the function). In this way, call the Val function to execute different functions respectively
Code:
Method 1: call function directly
//Method 1: call function directly #define _CRT_SECURE_NO_WARNINGS 1 #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) { return a / b; } int main() { int A = 0; int B = 0; scanf("%d %d", &A, &B); printf("%d + %d = %d\n", A, B, Add(A, B)); printf("%d - %d = %d\n", A, B, Sub(A, B)); printf("%d * %d = %d\n", A, B, Mul(A, B)); printf("%d / %d = %d\n", A, B, Div(A, B)); system("pause"); return 0; }
Method 2: array of function pointers
//Method 2: define an array of function pointers #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> void Add(int a, int b) { printf("%d + %d = %d\n", a, b, a+b); } void Sub(int a, int b) { printf("%d - %d = %d\n", a, b, a - b); } void Mul(int a, int b) { printf("%d * %d = %d\n", a, b, a * b); } void Div(int a, int b) { printf("%d / %d = %d\n", a, b, a / b); } int main() { int A = 0; int B = 0; scanf("%d %d", &A, &B); void(*pf[])(int, int) = { Add, Sub, Mul, Div }; for (int i = 0; i < sizeof(pf) / sizeof(pf[0]); i++) { (*pf[i])(A, B); } system("pause"); return 0; }
Method 3: function pointer as parameter
//Using a pointer to a function as an argument #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> void Add(int a, int b) { printf("%d + %d = %d\n", a, b, a+b); } void Sub(int a, int b) { printf("%d - %d = %d\n", a, b, a - b); } void Mul(int a, int b) { printf("%d * %d = %d\n", a, b, a * b); } void Div(int a, int b) { printf("%d / %d = %d\n", a, b, a / b); } void cal(int a,int b,void(*pf)(int, int)) { (*pf)(a, b); } int main() { int A = 0; int B = 0; scanf("%d %d", &A, &B); cal(A, B, Add); cal(A, B, Sub); cal(A, B, Mul); cal(A, B, Div); system("pause"); return 0; }
Result: