当前位置:网站首页>[C language practice - exchange the values of two variables]

[C language practice - exchange the values of two variables]

2022-06-09 13:07:00 Beginners of C language


Preface

This article exercises exchanging the values of two variables , There are generally two types of methods , Create temporary variables and do not create temporary variables , It mainly includes :

  • Create temporary variables —— General method
  • Create temporary variables —— The pointer
  • Do not create temporary variables —— Addition and subtraction
  • Do not create temporary variables —— Exclusive or

1、 Create temporary variables —— General method

int main()
{
    
	int a = 10;
	int b = 20;
	int temp = 0;
	printf(" Exchange before :a = %d b = %d\n", a, b);// Before the exchange 

	temp = a;	
	a = b;
	b = temp;
	printf(" After exchanging :a = %d b = %d\n", a, b);// After the exchange 
	return 0;
}

The results are as follows :

 Insert picture description here

2、 Create temporary variables —— The pointer

Both pointers and general methods need to create variables , The difference is that general methods cannot be written in the form of functions , This is the application of formal parameters and arguments , Already in 【C Language foundation 3—— function (1)3、 The parameters of the function 】 I've learned :

// Implemented as a function , But I can't finish the task 
int exchange1(int x, int y)
{
    // When an argument is passed to a formal parameter , A formal parameter is a temporary copy of an argument ,
	// Modification of a parameter does not affect the argument 
	int temp = x;
	x = y;
	y = temp;
}
// The right version 
int exchange2(int* pa, int* pb)// Define pointer , Receiving address 
{
    
	int temp = *pa;
	*pa = *pb;
	*pb = temp;
}
int main()
{
    
	int a = 3;
	int b = 5;
	exchange1(a, b);// The passed parameter is the value 
	printf("exchange1::a = %d b = %d\n", a, b);// Exchange before 

	exchange2(&a, &b);// The transmission parameter is the address 
	printf("exchange2::a = %d b = %d\n", a, b);// After the exchange 
	// Incoming address , Custom formal parameters and arguments are more closely related , Can change the value stored in the address 
	// here , The address of the formal parameter is the same as that of the argument 
	return 0;
}

The results are as follows :

 Insert picture description here

3、 Do not create temporary variables —— Addition and subtraction

int main()
{
    
	int a = 10;
	int b = 20;
	a = a + b;// hold a+b Assign a value to a
	b = a - b;// hold a+b-b, Namely a  Assign a value to b
	a = a - b;// hold a+b-a  Assign a value to a
	printf("a=%d\nb=%d\n", a, b);
	return 0;
}

The results are as follows :

 Insert picture description here

4、 Do not create temporary variables —— Exclusive or

Use of XOR , stay 【 Primary data structure and algorithm 2】 Time complexity and space complexity (2)—— Runner array 、 Left hand character 、 Missing numbers 7.3 Method 3—— Exclusive or I have described in detail in :

int main()
{
    
	int a = 10;
	int b = 20;
	printf(" Exchange before :a = %d b = %d\n", a, b);// Before the exchange 

	a = a ^ b;// hold a^b Assign a value to a
	b = a ^ b;// hold a^b^b, It's the rest a  Assign a value to b
	a = a ^ b;// hold a^b^a,  Assign a value to a
	printf(" After exchanging :a = %d b = %d\n", a, b);// After the exchange 
	return 0;
}

The results are as follows :

 Insert picture description here


summary

Exchange the values of two variables , There are two types of methods , Create temporary variables and do not create temporary variables :

  • Two methods of creating temporary variables are commonly used
  • The method of pointer should be mastered
  • It is not easy to think of two methods without creating temporary variables
  • Pay attention to the special use of addition and subtraction and XOR
原网站

版权声明
本文为[Beginners of C language]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091215198854.html