今天在电脑上偶然看到自己大一上学期写的学生管理系统,当时写是因为C与看课本的课后习题有这个,老师没有让写,我当时为了挑战一下自己,就开始搞,前前后后修改了很多次,最后终于完成了。嘿嘿,特地来分享一下。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 30
#define MAX 150
void InputRecord(int num[], char name[][MAX], int score[], int n);
int CaculateTotal(int score[], int n);
float CaculateAverage(int n, int sum);
void DescendingSort(int num[], char name[][MAX], int score[], int n);
void AscendingSort(int num[], char name[][MAX], int score[], int n);
void AscendingNumSort(int num[], char name[][MAX], int score[], int n);
void AscendingNameSort(int num[], char name[][MAX], int score[], int n);
void LinNumSearch(int num[], char name[][MAX], int score[], int n);
void LinNameSearch(int num[], char name[][MAX], int score[], int n);
void StatisticAnalysis(int score[], int n);
void ListRecord(int num[], char name[][MAX], int score[], int n, int sum, int aver);
void Swap(int *x, int *y);
int main()
{
int num[N], n, op, score[N], sum;
char name[N][MAX];
float aver;
printf("How many students:\n");
scanf("%d", &n);
printf("1.Input record\n2.Caculate total and average score of course\n3.Sort in descending order by score\n4.Sort in ascending order by score\n5.Sort in ascending order by number\n6.Sort in dictionary order by name\n7.Search by number\n8.Search by name\n9.Statistic analysis\n10.List record\n0.Exit\nPlease enter your choice\n");
do
{
printf("Input option:\n");
scanf("%d", &op);
switch (op)
{
case 1:
InputRecord(num, name, score, n);
break;
case 2:
aver = CaculateAverage(n, CaculateTotal(score, n));
break;
case 3:
DescendingSort(num, name, score, n);
break;
case 4:
AscendingSort(num, name, score, n);
break;
case 5:
AscendingNumSort(num, name, score, n);
break;
case 6:
AscendingNameSort(num, name, score, n);
break;
case 7:
LinNumSearch(num, name, score, n);
break;
case 8:
LinNameSearch(num, name, score, n);
break;
case 9:
StatisticAnalysis(score, n);
break;
case 10:
sum = CaculateTotal(score, n);
aver = CaculateAverage(n, sum);
ListRecord(num, name, score, n, sum, aver);
break;
case 0:
exit(0);
}
} while (op >= 0);
return 0;
}
void InputRecord(int num[], char name[][MAX], int score[], int n)
{
int i;
for (i = 0; i < n; i++)
{
scanf("%d%s%d", &num[i], &name[i], &score[i]);
}
}
int CaculateTotal(int score[], int n)
{
int i;
int total = 0;
for (i = 0; i < n; i++)
{
total += score[i];
}
return total;
}
float CaculateAverage(int n, int sum)
{
return (float)sum / n;
}
void DescendingSort(int num[], char name[][MAX], int score[], int n)
{
int i, j;
char temp[MAX];
for (i = 0; i < n - 1; i++)
{
for (j = 1; j < n; j++)
{
if (score[j] > score[i])
{
Swap(&score[j], &score[i]);
Swap(&num[j], &num[i]);
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
for (i = 0; i < n; i++)
{
printf("num:%d name:%s score:%d\n", num[i], name[i], score[i]);
}
}
void Swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void AscendingSort(int num[], char name[][MAX], int score[], int n)
{
int i, j;
char temp[MAX];
for (i = 0; i < n - 1; i++)
{
for (j = 1; j < n; j++)
{
if (score[j] < score[i])
{
Swap(&score[j], &score[i]);
Swap(&num[j], &num[i]);
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
for (i = 0; i < n; i++)
{
printf("num:%d name:%s score:%d\n", num[i], name[i], score[i]);
}
}
void AscendingNumSort(int num[], char name[][MAX], int score[], int n)
{
int i, j;
char temp[MAX];
for (i = 0; i < n - 1; i++)
{
for (j = 1; j < n; j++)
{
if (num[j] < num[i])
{
Swap(&score[j], &score[i]);
Swap(&num[j], &num[i]);
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
for (i = 0; i < n; i++)
{
printf("num:%d name:%s score:%d\n", num[i], name[i], score[i]);
}
}
void AscendingNameSort(int num[], char name[][MAX], int score[], int n)
{
int i, j;
char temp[MAX];
for (i = 0; i < n - 1; i++)
{
for (j = 1; j < n; j++)
{
if ((strcmp(name[j], name[i])) < 0)
{
Swap(&score[j], &score[i]);
Swap(&num[j], &num[i]);
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
for (i = 0; i < n; i++)
{
printf("num:%d name:%s score:%d\n", num[i], name[i], score[i]);
}
}
void LinNameSearch(int num[], char name[][MAX], int score[], int n)
{
int i;
char na[MAX];
printf("Input the searching name:");
scanf("%s", na);
for (i = 0; i < n; i++)
{
if ((strcmp(name[i], na)) == 0)
printf("no=%d\tscore=%d\n", i + 1, score[i]);
}
}
void LinNumSearch(int num[], char name[][MAX], int score[], int n)
{
int x, i;
printf("Input the searching ID:");
scanf("%d", &x);
for (i = 0; i < n; i++)
{
if (num[i] == x)
{
printf("no:%d\tscore=%d\n", i + 1, score[i]);
}
}
}
void StatisticAnalysis(int score[], int n)
{
int i, A = 0, B = 0, C = 0, D = 0, E = 0;
for (i = 0; i < n; i++)
{
if (score[i] >= 0 && score[i] <= 59)
{
A++;
}
else if (score[i] >= 60 && score[i] <= 69)
{
B++;
}
else if (score[i] >= 70 && score[i] <= 79)
{
C++;
}
else if (score[i] >= 80 && score[i] <= 89)
{
D++;
}
else if (score[i] >= 90 && score[i] <= 100)
{
E++;
}
}
printf("90~100:%d\trate:%f %\n", E, (float)E / n * 100.0);
printf("80~89:%d\trate:%f %\n", D, (float)D / n * 100.0);
printf("70~79:%d\trate:%f %\n", C, (float)C / n * 100.0);
printf("60~69:%d\trate:%f %\n", B, (float)B / n * 100.0);
printf("0~59:%d\trate:%f %\n", A, (float)A / n * 100.0);
}
void ListRecord(int num[], char name[][MAX], int score[], int n, int sum, int aver)
{
int i;
for (i = 0; i < n; i++)
{
printf("num=%d\tname=%s\tscore=%d\n", num[i], name[i], score[i]);
}
printf("sum=%d\taverage=%d\n", sum, aver);
}