用指针实现:(1) 输入 3 个整数,按由大到小的顺序输出;(2) 然后将程 序改为:输入 3 个字符串,按由小到大顺序输出
时间: 2023-09-09 09:08:02 浏览: 82
(1) 输入 3 个整数,按由大到小的顺序输出
```c
#include <stdio.h>
void sort(int *a, int *b, int *c);
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
sort(&a, &b, &c);
printf("由大到小排序结果为:%d %d %d\n", a, b, c);
return 0;
}
void sort(int *a, int *b, int *c) {
int temp;
if (*a < *b) {
temp = *a;
*a = *b;
*b = temp;
}
if (*a < *c) {
temp = *a;
*a = *c;
*c = temp;
}
if (*b < *c) {
temp = *b;
*b = *c;
*c = temp;
}
}
```
(2) 输入 3 个字符串,按由小到大顺序输出
```c
#include <stdio.h>
#include <string.h>
void sort(char *a, char *b, char *c);
int main() {
char a[100], b[100], c[100];
printf("请输入三个字符串:\n");
scanf("%s %s %s", a, b, c);
sort(a, b, c);
printf("由小到大排序结果为:%s %s %s\n", a, b, c);
return 0;
}
void sort(char *a, char *b, char *c) {
char temp[100];
if (strcmp(a, b) > 0) {
strcpy(temp, a);
strcpy(a, b);
strcpy(b, temp);
}
if (strcmp(a, c) > 0) {
strcpy(temp, a);
strcpy(a, c);
strcpy(c, temp);
}
if (strcmp(b, c) > 0) {
strcpy(temp, b);
strcpy(b, c);
strcpy(c, temp);
}
}
```
阅读全文
相关推荐

















