You must take personal responsibility. You can not change the circumstances, the seasons, or the wind, but you can change yourself. -- Jim Rohn |
Variable exchange is a very common problem. N is used in many places, but each time it is different. Common solutions include the following types of parameters.
There are several solutions described in C language.
"★Use the [pointer type] Parameter
/** Use the pointer type for parameter passing. **/# Include <stdio. h> intSwap (int * parA, int * parB) {int tmp; tmp = * parA; * parA = * parB; * parB = tmp;} intmain (void) {int intA, intB; printf ("Enter two numbers \ n"); printf ("Number one x ="); scanf ("% d", & intA ); printf ("Number two y ="); scanf ("% d", & intB); Swap (& intA, & intB ); printf ("After Swap function \ n"); printf ("Number one x = % d \ n", intA); printf ("Number two y = % d \ n ", intB );}
"★Use the [Integer type] Parameter
/** Replace two swap function parameters with non-Pointers or reference types. In this case, you still need to use pointers to replace the two parameters. First, obtain the corresponding two storage addresses in the main function, and then perform the conversion operation to meet the type requirements of the SWAp function. After entering the swap function, perform semantic conversion to convert the corresponding value to the pointer type. **/# Include <stdio. h> voidswap (int x, int y) {printf ("in SWAp function, and before swap x = % d, y = % d \ n", * (int *) x), * (int *) y); int TMP; int * PX = (int *) x; int * py = (int *) y; TMP = * PX; * PX = * py; * py = TMP; printf ("in SWAp function, and after swap x = % d, y = % d \ n ", * PX, * Py);} intmain (void) {int x = 23, y = 16; // before entering the swap function, the two values are printf ("before swap x = % d, y = % d \ n", x, y); // before entering the swap function, the two numbers of addresses are printf ("before swap & X = % d, & Y = % d \ n", (INT) & X, (INT) & Y ); swap (INT) & X, (INT) & Y); printf ("after swap x = % d, y = % d \ n", x, y ); printf ("after swap & X = % d, & Y = % d \ n", (INT) & X, (INT) & Y );}
The swap function can be implemented without the third variable. The following two solutions give a prompt.
"★Use [XOR] [^] for variable exchange
/** Replace two Swap function parameters with non-Pointers or reference types. In this case, you still need to use pointers to replace the two parameters. First, obtain the corresponding two storage addresses in the main function, and then perform the conversion operation to meet the type requirements of the Swap function. After entering the Swap function, perform semantic conversion to convert the corresponding value to the pointer type. **/# Include <stdio. h> voidSwap (int x, int y) {printf ("In Swap function, and before swap x = % d, y = % d \ n", * (int *) x), * (int *) y); * (int *) x) = * (int *) x) ^ * (int *) y ); * (int *) y) = * (int *) x) ^ * (int *) y); * (int *) x) = * (int *) y) ^ * (int *) x); printf ("In Swap function, and after swap x = % d, y = % d \ n ", * (int *) x), * (int *) y);} intmain (void) {int x = 23, y = 16; // Before entering the Swap function, the two values are printf ("Before swap x = % d, y = % d \ n", x, y ); // Before entering the Swap function, the two addresses are printf ("Before swap & x = % d, & y = % d \ n", (int) & x, (int) & y); Swap (int) & x, (int) & y); printf ("After swap x = % d, y = % d \ n ", x, y); printf ("After swap & x = % d, & y = % d \ n", (int) & x, (int) & y );}
"★Use [+] [-] for variable exchange
/** Replace two Swap function parameters with non-Pointers or reference types. In this case, you still need to use pointers to replace the two parameters. First, obtain the corresponding two storage addresses in the main function, and then perform the conversion operation to meet the type requirements of the Swap function. After entering the Swap function, perform semantic conversion to convert the corresponding value to the pointer type. **/# Include <stdio. h> voidSwap (int x, int y) {printf ("In Swap function, and before swap x = % d, y = % d \ n", * (int *) x), * (int *) y); * (int *) x) = * (int *) x) + * (int *) y ); * (int *) y) = * (int *) x)-* (int *) y); * (int *) x) = * (int *) x)-* (int *) y); printf ("In Swap function, and after swap x = % d, y = % d \ n ", * (int *) x), * (int *) y);} intmain (void) {int x = 23, y = 16; // Before entering the Swap function, the two values are printf ("Before swap x = % d, y = % d \ n", x, y ); // Before entering the Swap function, the two addresses are printf ("Before swap & x = % d, & y = % d \ n", (int) & x, (int) & y); Swap (int) & x, (int) & y); printf ("After swap x = % d, y = % d \ n ", x, y); printf ("After swap & x = % d, & y = % d \ n", (int) & x, (int) & y );}
The solution described in C ++ language.
"★Use the [C ++ reference type] As the form parameter
/** Use the reference parameter **/# include <iostream> using namespace std; voidSwap (int & x, int & y) {cout <"In Swap function, and before swap x = "<x <", "<" y = "<y <endl; x = x ^ y; y = x ^ y; x = y ^ x; cout <"In Swap function, and after swap x =" <x <", "<" y = "<y <endl;} intmain (void) {int x = 23, y = 16; // before entering the Swap function, cout <"Before swap x =" <x <", y =" <y <endl; Swap (x, y ); cout <"After swap x =" <x <", y =" <y <endl ;}