[C] 纯文本查看 复制代码
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");
}