输入三个字符串,按由小到大的顺序输出,利用指针实现。

输入三个字符串,按由小到大的顺序输出,利用指针实现。

//输入三个字符串,按由小到大的 顺序输出,用指针来实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 5
int main()
{
    
    
    char a[M], b[M], c[M];
    char *p, *q, *t;
    char x[M];
    printf("请输入三个字符串:\n");
    gets(a);
    gets(b);
    gets(c);
    p = a; // abc
    q = b; // agf
    t = c; // ccc
    // printf("输出%d\n", strcmp(p, q)); // 输出-1 说明 p比q小
    // printf("strcmp(q,p) 输出%d\n", strcmp(q,p)); // 输出1 说明 q比p 大
    if ((strcmp(p, q)) > 0) // p 比 q 大,交换 pq的值,让小的放前面
    {
    
    
        strcpy(x, p); // 将p的值覆盖到x里面,
        strcpy(p, q); // 将q的值覆盖到p
        strcpy(q, x); // 将x的值覆盖到q
    }
    if ((strcmp(p, t)) > 0) // p > t ? 同上,x相当于 temp 辅助交换
    {
    
    
        strcpy(x, p);
        strcpy(p, t);
        strcpy(t, x);
    }
    if ((strcmp(q, t)) > 0) // q > t
    {
    
    
        strcpy(x, q);
        strcpy(q, t);
        strcpy(t, x);
    }

    printf("输出由小到大排序好的三个字符串:%s\t%s\t%s\n", p, q, t);
    return 0;
}

result:

请输入三个字符串:
ccc
abf
acb
输出由小到大排序好的三个字符串:abf      acb     ccc

2020/12/21 更新变量名以及更明确的注释,新步骤

相信考察的是程序员的逻辑而不是数学。

// 输入三个字符串,按由小到大的顺序输出,利用指针实现。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define array_size 10
int main(int argc, char const *argv[])
{
    
    
    char char_array_first[array_size], char_array_second[array_size], char_array_third[array_size];
    char temp_swap[array_size];
    char *char_pointer_first, *char_pointer_second, *char_pointer_third;
    
    puts("please type three strings to order!");
    gets(char_array_first);
    gets(char_array_second);
    gets(char_array_third);
    printf("before order: %s\t%s\t%s\n",char_array_first,char_array_second,char_array_third);

    char_pointer_first = char_array_first;
    char_pointer_second = char_array_second;
    char_pointer_third = char_array_third;

    if (strcmp(char_array_first, char_array_second) > 0) // make sure the fist < second
    {
    
    
        strcpy(temp_swap, char_array_first); // first arg is Destination,second arg is source
        strcpy(char_array_first, char_array_second);
        strcpy(char_array_second, temp_swap);
    }

    if (strcmp(char_array_second, char_array_third) > 0) // make sure the second < third
    {
    
    
        strcpy(temp_swap, char_array_second);
        strcpy(char_array_second, char_array_third);
        strcpy(char_array_third, temp_swap);
    }

    if (strcmp(char_array_first, char_array_second) > 0) // make sure the fist < second
    {
    
    
        strcpy(temp_swap, char_array_first); // first arg is Destination,second arg is source
        strcpy(char_array_first, char_array_second);
        strcpy(char_array_second, temp_swap);
    }
    printf("after order: %s\t%s\t%s\n",char_array_first,char_array_second,char_array_third);

    return 0;
}

result:

please type three strings to order!
ccc
abf
acb
before order: ccc       abf     acb
after order: abf        acb     ccc

3.21
指针,只使用strcmp

//输入三个字符串,按由小到大的 顺序输出,用指针来实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 5
int main()
{
    
    

    char str_1[SIZE];
    char str_2[SIZE];
    char str_3[SIZE];
    char *p1, *p2, *p3;

    scanf("%s%s%s", str_1, str_2, str_3);

    p1 = str_1;
    p2 = str_2;
    p3 = str_3;

    if (strcmp(p1, p2))
    {
    
    
        char *tmp = p1;
        p1 = p2;
        p2 = tmp;
    }
    if (strcmp(p2, p3))
    {
    
    
        char *tmp = p2;
        p2 = p3;
        p3 = tmp;
    }
    if (strcmp(p1, p2))
    {
    
    
        char *tmp = p1;
        p1 = p2;
        p2 = tmp;
    }
    // printf("%s,%s,%s\n", str_1, str_2, str_3);
    printf("%s,%s,%s", p1, p2, p3);
    return 0;
}

result:

zzz bbb aaa
aaa,bbb,zzz

猜你喜欢

转载自blog.csdn.net/qq_44880154/article/details/110475449
今日推荐