输入学生成绩,并能对学生成绩进行升降序操作

  • Post author:
  • Post category:其他


#define _CRT_SECURE_NO_WARNINGS 1
#define N 40
#include<stdio.h>
void Swap(int* x, int* y)
{
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;
}
int Shuru(int shuru[N])
{
	int i = -1;
	do {
		i++;
		printf("请输入成绩");
		scanf("%d", &shuru[i]);
	} while (shuru[i]>0);//当此条件为假时,循环终止且终止时的数字并不会存储到数组中
	return i;
}
void Shuchu(int shuru[], int n)
{
	int i = 0;
	for (i = 0; i < n ; i++)
	{
		printf("%4d", shuru[i]);
	}
	printf("\n");
}
void Shengxv(int a[], int n)
{
	int k,j,i;
	for (i = 0; i < n - 1; i++)
	{
		k = i;//不可写为i=k,因为k为设初始值,此时为随机值
		for (j =i+1; j < n; j++)
		{
			if (a[k] > a[j])
			{
				k = j;
			}
		}
		if (k != i)
		{
			Swap(&a[k], &a[i]);
		}	
	}
}
void Jiangxv(int a[], int n)
{
	int i, j, k;
	for (i = 0; i < n - 1; i++)
	{
		k=i;
		for (j = i+1; j < n; j++)
		{
			if (a[k] < a[j])
			{
				k = j;
			}
		}
		if (i != k)
		{
			Swap(&a[k], &a[i]);
		}
	}
}

int main()
{
	int shuru[N] = { 0 };
	int n = 0;
	int panduan;
	n=Shuru(shuru);
	printf("%d\n", n);
	Shuchu(shuru, n);
	printf("请判断升降序:");
	scanf("%d", &panduan);
	if (panduan == 0)
	{
		Shengxv(shuru, n);
		printf("升序排列后成绩为:");

	}
	else
	{
		Jiangxv(shuru, n);
		printf("降序排列后成绩为:");
	}
	Shuchu(shuru, n);
	return 0;
}



版权声明:本文为shenmouattention原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。