#include <stdio.h>
#include <stdlib.h>
#define SIZE 30 //成绩数组总长度
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30 //成绩数组总长度
#define SEGMENTSIZE 11 //分数段总共有0-10,总计11个段
#define FAILURE -1 //返回失败结果
#define SUCCESS 1 //返回成功结果
#define ASCEND 1 //升序
#define DECEND -1 //降序
int countStuNum(int array[], int length)
{
int i, stuNumber = 0;
for (i = 0; i < length; i++)
if (array[i] != -1)
stuNumber++; //因为数组的结尾是-1,统计非-1的个数
else break;
return stuNumber;
}
int countMaxScore(int array[], int len)
{
int max = 0;
for(int i = 0; i < len; i++)
{
if(array[i] >= max)
max = array[i];
}
printf("MAX:%d\n", max);
return max;
}
int countMinScore(int array[], int len)
{
int min = 101;
for(int i = 0; i < len; i++)
{
if(array[i] <= min)
min = array[i];
}
printf("MIN:%d\n", min);
return min;
}
float countAverageScore(int array[], int len)
{
float ave, sum = 0;
for(int i = 0; i < len; i++)
{
sum += array[i];
}
ave = sum / len;
printf("AVE:%f\n", ave);
return ave;
}
void swap(int *a, int *b){
int t;
t = *a;
*a = *b;
*b = t;
}
void bubbleSort(int array[], int length, int sortOrder)
{
int i, j, k, stuNum;
stuNum = countStuNum(array, length);//得到有效成绩的学生人数
for (i = stuNum; i>0; i--) //i指第i轮排序
{
k = i; //k指一轮中要比较交换的元素个数
if (sortOrder == ASCEND) //升序排列
{
for (j = 0; j<k - 1; j++)
if (array[j]>array[j + 1])
swap(&array[j], &array[j + 1]);
}
else //降序排列
{
for (j = 0; j<k - 1; j++)
if (array[j]<array[j + 1])
swap(&array[j], &array[j + 1]);
}
} //end for
}//end bubbleSort(int array[],int length,int sortOrder)
int main()
{
//注:数组中有25个学生分数,其中最一个-1表示该元素之前的数组元素值为有效分数。
int score[SIZE] = { 78, 65, 23, 34, 56, 87, 90, 99, 45, 88, 54, 95, 63, 66, 89, 54, 64, 77, 63, 56, 5, 86, 97, 94, 30, -1 }; //给score[1]到score[25]赋初值。
int i;
int stuNumber, maxScore, minScore, searchArrayPosition;
int searchScore, appendScoreNum, insertScoreNum, result, arrayPossition;
int order;//排序方向
float averageScore, medianScore;
enum Status { GAMEOVER, CONTINUE };
int choiceNum;
enum Status gameStatus;
int len = countStuNum(score , sizeof(score)/sizeof(int));
do
{
gameStatus = CONTINUE;
printf("\n\t\t学 生 成 绩 管 理 系 统 1.3 \n\t\t\tDesigned by HuangX\n");
printf("\n\t0.退出程序\t\t\t1.显示所有成绩\n\n");
printf("\t2.统计最高分\t\t\t3.统计最低分\n\n");
printf("\t4.统计平均分\t\t\t5.冒泡排序\n\n");
printf("请选择功能:");
scanf("%d", &choiceNum);
fflush(stdin);//清除键盘缓冲区的数,否则当输入字符时会出现死循环
switch (choiceNum)
{
case 0:
gameStatus = GAMEOVER;
break;
case 1:
for(i=0;i<=24;i++){
printf("%d\t",score[i]);
if(i%5==0)
puts("");
}
break;
case 2:
countMaxScore( score, len);
break;
case 3:
countMinScore( score, len);
break;
case 4:
countAverageScore(score, len);
break;
case 5:
bubbleSort(score,len,sortOrder);
break;
default:
printf("输入错误,重新输入!\n");
break;
}
}
while(1);
return 0;
}