wai1216 发表于 2016-5-31 10:43:55

第十九课作业

本帖最后由 wai1216 于 2016-5-31 23:38 编辑


int ltos(int a, int b);
int stol(int a, int b);
void _numchange(int *a, int *b);
void enternumlist(int *p, int n)
{
        int i = 0;
        for (i = 0; i < n; i++)
        {
                scanf("%d", p + i);
        }
}

void shownumlist(int *p, int n)
{
        int i = 0;
        for (i = 0; i < n; i++)
        {
                printf("%d ", *(p + i));
        }
        printf("\n");
}


void changenumlistltos(int *p, int n)
{
        int i = 0, j = 0;
        for (i = 0; i < n; i++)
        {
                for (j = 0; j < n - i - 1; j++)
                {
                        if (ltos(*(p + j), *(p + j + 1)))
                        {
                                _numchange(p + j, p + j + 1);
                        }
                }
        }
}

void changenumliststol(int *p, int n)
{
        int i = 0, j = 0;
        for (i = 0; i < n; i++)
        {
                for (j = 0; j < n - i - 1; j++)
                {
                        if (stol(*(p + j), *(p + j + 1)))
                        {
                                _numchange(p + j, p + j + 1);
                        }
                }
        }
}

void changenumlistlx(int *p, int n)
{
        int i = 0, j = 0;
        for (i = 0; i < n; i++)
        {
                for (j = i; j < n - 1; j++)
                {
                        if (!(i % 2))
                        {
                                if (ltos(*(p + i), *(p + j + 1)))
                                {
                                        _numchange(p + i, p + j + 1);
                                }
                        }
                        else
                        {
                                if (stol(*(p + i), *(p + j + 1)))
                                {
                                        _numchange(p + i, p + j + 1);
                                }
                        }
                }
        }
}

void _numchange(int *a, int *b)
{
        int temp = 0;
        temp = *a;
        *a = *b;
        *b = temp;
}

int ltos(int a, int b)/*从小到大*/
{
        return a > b ? 1 : 0;
}

int stol(int a, int b)/*从大到小*/
{
        return a < b ? 1 : 0;
}

int _strcmp(char *str1, char *str2)
{
        int j = 0;
        for (; *str1&&*str2; str1++, str2++)
        {
                if (*str1 == *str2)
                {
                        ;
                }
                else
                {
                        j++;
                }
        }
        return j;
}

void showltos(int *p, int n)
{
        changenumlistltos(p, n);

        printf("The l to s numlist is :\n");
        shownumlist(p, n);
}

void showstol(int *p, int n)
{
        changenumliststol(p, n);

        printf("The s to l numlist is :\n");
        shownumlist(p, n);
}

void showlx(int *p, int n)
{
        changenumlistlx(p, n);

        printf("The lx numlist is :\n");
        shownumlist(p, n);
}

void Sortlist(int *p, int n, char *type)
{
        char *type1 = "ltos";
        char *type2 = "stol";
        char *type3 = "lx";
        if (_strcmp(type, type1) == 0)
        {
                showltos(p, n);
        }
        else if (_strcmp(type, type2) == 0)
        {
                showstol(p, n);
        }
        else if (_strcmp(type, type3) == 0)
        {
                showlx(p, n);
        }
}



void main()
{
        int *p = (int *)malloc(10 * sizeof(int));
        printf("Please enter the num: \n");
        enternumlist(p, 10);

        printf("The numlist is :\n");
        shownumlist(p, 10);

        Sortlist(p, 10, "ltos");
        Sortlist(p, 10, "stol");
        Sortlist(p, 10, "lx");
}
页: [1]
查看完整版本: 第十九课作业