C++ experiment 02 (05) character exchange-the use of quoted variables

Title description
Write a program, input two characters at will from the keyboard, and make them output in descending order. The program is required to have a function that exchanges two characters, and the formal parameters are references to variables.
Input description
Two characters
Output description Two characters
after exchange
Input sample
ab
Output sample
The result after exchange is: ba (Chinese colon)

#include <iostream>
using namespace std;
void exchange(char &x,char &y)//使用引用
{
    
    
	char k;
	if(x<y)
	{
    
    
		k=x;
		x=y;
		y=k;
	}
}

int main()
{
    
    
	char j=0,h=0;
	cin>>j>>h;
	exchange(j,h);
	cout<<"交换后的结果为:"<<j<<h;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44179485/article/details/105890650