當前位置:編程學習大全網 - 編程軟體 - 如何用函數實現三個數的排序(用的是C語言)

如何用函數實現三個數的排序(用的是C語言)

壹:全局變量

#include?<stdio.h>

int?a,b,c;?//定義三個全局變量

void?sort()?//不需要參數

{

int?t;

if(a>b)

{

t=a;a=b;b=t;

}

if(b>c)

{

t=b;b=c;c=t;

}

if(a>b)

{

t=a;a=b;b=t;

}

}

int?main()

{

printf("輸入:");

scanf("%d%d%d",&a,&b,&c);

sort();

printf("排序:%d?<?%d?<?%d\n",a,b,c);

return?0;

}

二:指針

#include?<stdio.h>

void?sort(int?*a,?int?*b,?int?*c)?//參數傳遞方式:地址傳遞

{?

int?t;

if(*a>*b)?

{

t=*a;*a=*b;*b=t;

}

if(*b>*c)

{

t=*b;*b=*c;*c=t;

}

if(*a>*b)?

{

t=*a;*a=*b;*b=t;

}

}

int?main()

{

int?a,b,c;

printf("輸入:");

scanf("%d%d%d",&a,&b,&c);

sort(&a,&b,&c);//把地址作為參數

printf("排序:%d?<?%d?<?%d\n",a,b,c);

return?0;

}

  • 上一篇:手機軟件編程和電腦編程的異同
  • 下一篇:數控加工中心編程中的g15g16g17怎麽使用?
  • copyright 2024編程學習大全網