1179: macro definition with parameters (function topic)
Title Description
Input three characters from the keyboard, separate them with spaces, use SWAP in macro definition 1 with parameters, and sort and output the three characters in descending order.
Macro definition 1: #define SWAP(a, b, t) { t=a; a=b; b=t; }
Please try, if the SWAP in macro definition 2 is used, how should the main function be modified to get the correct result?
Macro definition 2: #define SWAP(a, b, t) t=a; a=b; b=t;
Input
Input three characters, separated by spaces
Output
Output one line, including three characters, separated by spaces
Sample Input Copy
waq
Sample Output Copy
wqa
Source/Classification
Macro definition 1: #define SWAP(a, b, t) { t=a; a=b; b=t; }
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SWAP(a,b,t){t=a;a=b;b=t;}
int main(){
char ch[3];
char t;
scanf("%c",&ch[0]);
for(int i=1;i<3;i++){
scanf(" %c",&ch[i]);
}
for(int i=0;i<2;i++){
for(int j=i+1;j<3;j++){
if(ch[i]<ch[j]) SWAP(ch[i],ch[j],t);
}
}
for(int i=0;i<3;i++) printf("%c ",ch[i]);
printf("\n");
return 0;
}
Macro definition 2: #define SWAP(a, b, t) t=a; a=b; b=t;
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SWAP(a,b,t) t=a;a=b;b=t;
int main(){
char ch[3];
char t;
scanf("%c",&ch[0]);
for(int i=1;i<3;i++){
scanf(" %c",&ch[i]);
}
for(int i=0;i<2;i++){
for(int j=i+1;j<3;j++){
if(ch[i]<ch[j]) {
SWAP(ch[i],ch[j],t);}//加大括号
}
}
for(int i=0;i<3;i++) printf("%c ",ch[i]);
printf("\n");
return 0;
}