筱海 发表于 2021-12-1 10:10:54

用指针作返回值编写函数int *sort(int a, int b, int c)求最大值中间值最小值

本帖最后由 筱海 于 2021-12-1 10:11 编辑

用指针作返回值编写函数int *sort(int a, int b, int c),实现求三个数中的最大值、中间值和最小值。
在main函数中进行数据输入和输出,数据之间用”,”隔开。
输入:三个整数,数据之间用”,”隔开。
输出:最大值、中间值和最小值,数据之间用”,”隔开。

jackz007 发表于 2021-12-1 10:59:20

本帖最后由 jackz007 于 2021-12-1 12:30 编辑

#include <stdio.h>
#include <stdlib.h>

int * sort(int a , int b , int c)
{
      int * d = NULL                                                 ;
      if(d = (int *) malloc(sizeof(int) * 3)) {
                d = (a > b && a > c) ? a : (b > a && b > c) ? b : c ;
                d = (a < b && a < c) ? a : (b < a && b < c) ? b : c ;
                d = a + b + c - d - d                         ;
      } else {
                fprintf(stderr , "Failure of memory allocate .\n")   ;
      }
      return d                                                       ;
}

int main(void)
{
      int a , b , c , * d                                          ;
      scanf("%d,%d,%d" , & a , & b , & c)                            ;
      if(d = sort(a , b , c)) {
                printf("%d,%d,%d\n" , d , d , d)            ;
                free(d)                                                ;
      }
}
      编译、运行实况:
D:\0002.Exercise\C>g++ -o x x.c

D:\0002.Exercise\C>x
5,7,8
8,7,5

D:\0002.Exercise\C>x
8,7,5
8,7,5

D:\0002.Exercise\C>x
7,5,8
8,7,5

D:\0002.Exercise\C>x
5,8,7
8,7,5

D:\0002.Exercise\C>

阿萨德按时 发表于 2021-12-2 09:30:36

{:10_254:}

心驰神往 发表于 2021-12-3 08:06:43

阿萨德按时 发表于 2021-12-3 08:31:18

{:10_254:}

tomok 发表于 2021-12-3 08:48:47

也想知道 ,答案

tianlai7266 发表于 2021-12-3 12:36:14

{:10_254:}
页: [1]
查看完整版本: 用指针作返回值编写函数int *sort(int a, int b, int c)求最大值中间值最小值