排序 自己选择是从小到小还是从小到大排序。

#include "Head.c"
void main()             /*排序*/
{
        int num[10];
        int i,j;

        void DESC();
        void ASC();

        printf("请输入十个数字。/n");

        for( i = 0 ; i < 10 ; i++)
        {
                printf("请输入第%d个数:", i + 1);
                scanf("%d",&num[i]);
        }//End for;记录刚才输入的十个数。

        printf("请选择倒序(D)、顺序(A)、退出(E)。:");

        char ch='D' ;
        for(;;)
        {
                do{
                        ch = toupper(getchar());                /*获取键盘输入的字符*/
                }while(ch != 'D' && ch != 'A' && ch != 'E');

                switch(ch)
                {
                        case 'D':
                                printf("你输入的是倒序 %c/n",ch);
                                DESC(num);
                                break;
                        case 'A':
                                printf("你输入的是顺序显示 %c/n",ch);
                                ASC(num);
                                break;
                        case 'E':
                                exit(0);
                                break;
                }

        }      
       
}//End main();

void ASC(int num[])
{
        int i,j;
        int temp;       /*中间存取变量*/
        for(i = 0 ; i < 9 ; i++)                /*开始进行排序操作*/
        {
                for( j = i + 1 ; j < 10 ; j++)
                {
                        if( num[i] > num[j] )
                        {
                                temp = num[i] ;
                                num[i] = num[j] ;
                                num[j] = temp ;
                        }//End if;
                }//End for;
        }//End for;
/*        for(i = 0 ; i < 10 ; i++)
        {
                printf("%d/n",num[i]);
        }//End for;从小到大打印出来。a*/

        void PrintfNumber();

        PrintfNumber(num);
}//End ASC;

void DESC(int num[])
{
        int i,j;
        int temp;       /*中间存取变量*/
        for(i = 0 ; i < 9 ; i++)                /*开始进行排序操作*/
        {
                for( j = i + 1 ; j < 10 ; j++)
                {
                        if( num[i] < num[j] )
                        {
                                temp = num[i] ;
                                num[i] = num[j] ;
                                num[j] = temp ;
                        }//End if;
                }//End for;
        }//End for;
/*        for(i = 0 ; i < 10 ; i++)
        {
                printf("%d/n",num[i]);
        }//End for;从小到大打印出来。a*/

        void PrintfNumber();
        PrintfNumber(num);
}//End DESC;

/*打印出数字来*/
void PrintfNumber(int num[])
{
        int i;
        for(i = 0 ; i < 10 ; i++)
        {
                printf("%d/n",num[i]);
        }//End for;
}//End PrintfNumber(); 

posted on 2008-01-31 17:36  小土泥  阅读(472)  评论(0编辑  收藏  举报

导航