#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 35
char name;
int inth;
struct date
{
int year;
char month;
char day;
};
struct student
{
char no[9];
char name[9];
char sex[3];
struct date birthday;
int score[4];
};
int menu()
{
int code;
printf(" 菜单\n");
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃ 0.退出 ┃\n");
printf("┃ 1.显示学生信息 ┃\n");
printf("┃ 2.显示按姓名排序后的学生信息 ┃\n");
printf("┃ 3.显示按总分排序后的学生信息 ┃\n");
printf("┃ 4.按学号查学生分数和平均分数 ┃\n");
printf("┃ 5.按姓名查学生分数和平均分数 ┃\n");
printf("┃ 6.查各课程平均分数 ┃\n");
printf("┃ 7.查男女学生的人数和总人数 ┃\n");
printf("┃ 8.显示学生信息表 ┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("选择代码(0,1,2,3,4,5,6,7,8): ");
scanf("%d", &code);
return code;
}
/* 建立学生信息数组 */
void readsi(struct student stud[], int *n)
{
FILE*fp;
int i;
if ((fp = fopen("D:\\studf.txt", "rb")) == NULL)
{
printf("不能打开studf.txt文件!\n");
exit(1);
}
for (i = 0; !feof(fp); i++)
{
(*n)++;
if (*n>N)
{
printf("最多处理35位学生信息!\n");
return;
}
fscanf(fp, "%s %s %s %d %d %d %d %d %d", stud[i].no, stud[i].name, stud[i].sex, &stud[i].birthday.year, &stud[i].birthday.month, &stud[i].birthday.day, &stud[i].score[0], &stud[i].score[1], &stud[i].score[2]);
stud[i].score[3] = stud[i].score[0] + stud[i].score[1] + stud[i].score[2];/*总分计算*/
}
fclose(fp);
}
/* 显示学生信息 */
void printsi(struct student *pstud, int n)
{
int i;
printf(" 学号 姓名 性别 年 月 日 数学 英语 C 总分\n");
printf("┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉\n");
printf("========================================================\n");
for (i = 0; i<n; i++)
{
printf(" %-8s%-8s%-2s %4d %2d %2d %4d %4d %4d %5d\n", pstud[i].no, pstud[i].name, pstud[i].sex, pstud[i].birthday.year, pstud[i].birthday.month, pstud[i].birthday.day, pstud[i].score[0], pstud[i].score[1], pstud[i].score[2], pstud[i].score[3]);
}
}
/* 按学号排序-简单比较排序法*/
void csort_no(struct student *pstud, int n)
{
struct student temp;
int i, j;
for (i = 0; i<n - 1; i++)
for (j = i + 1; j<n; j++)
if (strcmp(pstud[i].no, pstud[j].no)>0)
{
temp = pstud[i];
pstud[i] = pstud[j];
pstud[j] = temp;
}
}
/* 显示学生信息表*/
void printtable(struct student *pstud, int n)
{
int i;
struct student studA[N];
for (i = 0; i<n; i++)
studA[i] = pstud[i];
csort_no(studA, n);
printf("\n\n");
printf(" 学生信息表\n");
printf(" ==========================================================================\n");
printf("┏━━━━┳━━━━┳━━━┳━━━━━━┳━━━┳━━━┳━━━┳━━━┓\n");
printf("┃学号 ┃姓名 ┃ 性别 ┃ 年 月 日 ┃ 数学 ┃ 英语 ┃ C ┃ 总分 ┃\n");
printf("┣━━━━╋━━━━╋━━━╋━━━━━━╋━━━╋━━━╋━━━╋━━━┫\n");
printf("┣━━━━╋━━━━╋━━━╋━━━━━━╋━━━╋━━━╋━━━╋━━━┫\n");
for (i = 0; i<n; i++)
{
printf("┃%-8s┃%-8s┃ %-2s ┃ %4d %2d %2d ┃ %4d ┃ %4d ┃ %4d ┃ %4d ┃\n", studA[i].no, studA[i].name, studA[i].sex, studA[i].birthday.year, studA[i].birthday.month, studA[i].birthday.day, studA[i].score[0], studA[i].score[1], studA[i].score[2], studA[i].score[3]);
if (i == n - 1)
printf("┗━━━━┻━━━━┻━━━┻━━━━━━┻━━━┻━━━┻━━━┻━━━┛\n");
else
printf("┣━━━━╋━━━━╋━━━╋━━━━━━╋━━━╋━━━╋━━━╋━━━┫\n");
}
}
/* 查各课程平均分数 */
void find_ave(struct student *pstud, int n)
{
static int summath = 0;
static int sumenglish = 0;
static int sumC = 0;
static int score = 0;
int i;
struct student studA[N];
for (i = 0; i<n; i++)
studA[i] = pstud[i];
csort_no(studA, n);
printf(" 各课程平均分数 \n ");
printf("----------------------\n");
for (i = 0; i<n; i++)
{
summath += pstud[i].score[0];
sumenglish += pstud[i].score[1];
sumC +=pstud[i].score[2];
score +=pstud[i].score[3];
}
printf(" 数学平均分数: %4d\n", summath / n);
printf(" 英语平均分数: %4d\n", sumenglish / n);
printf(" C 平均分数: %4d\n", sumC / n);
printf(" 总分平均分数: %5d\n", score / n);
}
/* 查男女学生人数 */
void find_pnum(struct student *pstud, int n)
{
int i, j = 0, k = 0, m = 0;
for (i = 0; i<n; i++)
{
if (strcmp(pstud[i].sex, "男") == 0)
{
j++;
m++;
}
else
{
k++;
m++;
}
}
printf("\n男学生人数为%d人\n", j);
printf("\n女学生人数为%d人\n", k);
printf("\n学生总人数为%d人\n", m);
}
/* 按总分递减选择排序(应用指针数组)*/
void ssort(struct student *ptscore[], int n)
{
struct student *temp; int i, j;
for (i = 0; i<n - 1; i++)
for (j = 0; j<n - i - 1; j++)
if (ptscore[j]->score[3]<ptscore[j + 1]->score[3])
{
temp = ptscore[j];
ptscore[j] = ptscore[j + 1];
ptscore[j + 1] = temp;
}
}
/* 按学号查学生信息和平均分数(顺序查找)*/
void ssrch_no(struct student *pstud[], int n)
{
int i = 0, j = 0;
char no[9];
printf("请输入要查找的学生学号:");
scanf("%s", &no); printf("\n");
for (i = 0; i<n; i++)
{
if (strcmp(no, pstud[i]->no) == 0)
{
printf("已找到学号为%-8s的学生\n学号:%-8s\n姓名:%-8s\n性别:%-2s\n%4d年%2d月%2d日\n数学: %4d\n英语: %4d\n C : %4d\n总分: %4d\n平均分数:%4d\n", pstud[i]->no, pstud[i]->no, pstud[i]->name, pstud[i]->sex, pstud[i]->birthday.year, pstud[i]->birthday.month, pstud[i]->birthday.day, pstud[i]->score[0], pstud[i]->score[1], pstud[i]->score[2], pstud[i]->score[3], pstud[i]->score[3] / 3);
}
else
j++;
}
if (j >= n)
printf("未找到学号为%-8s的学生\n", no);
}
/* 按姓名查找的学生信息和平均分数(折半查找)*/
void bsrch_name(struct student *pname[], int n)
{
void bsrch(struct student *pname[], int n, char *name);
char name[9];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
bsrch(pname, n, name);
}
void bsrch(struct student *pname[], int n, char *name)
{
int lower = 0, upper = n - 1;
int i;
while (lower <= upper)
{
int mid = (lower + upper) / 2;
if (strcmp(name, pname[mid]->name) == 0)
{
for (i = mid; i<n; i++)
{
if (strcmp(name, pname[i]->name) == 0)
printf("已找到该学生\n学生信息:\n\n学号:%-8s\n姓名:%-8s\n性别:%-2s\n%4d年%2d月%2d日\n数学: %4d\n英语: %4d\n C : %4d\n总分: %4d\n平均分数:%4d\n", pname[i]->no, pname[i]->name, pname[i]->sex, pname[i]->birthday.year, pname[i]->birthday.month, pname[i]->birthday.day, pname[i]->score[0], pname[i]->score[1], pname[i]->score[2], pname[i]->score[3], pname[i]->score[3] / 3);
return;
}
for (i = mid - 1; i>0; i--)
{
if (strcmp(name, pname[i]->name) == 0)
printf("已找到该学生\n学生信息:\n\n学号:%-8s\n姓名:%-8s\n性别:%-2s\n%4d年%2d月%2d日\n数学: %4d\n英语: %4d\n C : %4d\n总分: %4d\n平均分数:%4d\n", pname[i]->no, pname[i]->name, pname[i]->sex, pname[i]->birthday.year, pname[i]->birthday.month, pname[i]->birthday.day, pname[i]->score[0], pname[i]->score[1], pname[i]->score[2], pname[i]->score[3], pname[i]->score[3] / 3);
return;
}
}
if (strcmp(name, pname[mid]->name)>0)
lower = mid + 1;
else
upper = mid - 1;
}
printf("找不到学生信息!");
}
/* 按姓名递增冒泡排序(应用指针数组) */
void bsort(struct student *pname[], int n)
{
struct student *temp;
int i, j;
for (i = 0; i<n - 1; i++)
for (j = 0; j<n - i - 1; j++)
if (strcmp(pname[j]->name, pname[j + 1]->name)>0)
{
temp = pname[j];
pname[j] = pname[j + 1];
pname[j + 1] = temp;
}
}
/* 按总分名次查找的学生信息和平均分数 */
void printf_ptscore(struct student *ptscore[], int n, int inth)
{
printf("请输入要查找总分名次:");
scanf("%d", &inth); inth--; if (inth>n)
{
printf("未找到总分名次为%5d的学生", inth);
}
else
printf("已找到总分名次为%5d的学生\n学号:%-8s\n姓名:%-8s\n性别:%-2s\n%4d年%2d月%2d日\n数学:%4d\n英语:%4d\nC:%4d\n总分%5d\n平均分数为:%4d", inth + 1, ptscore[inth]->no, ptscore[inth]->name, ptscore[inth]->sex, ptscore[inth]->birthday.year, ptscore[inth]->birthday.month, ptscore[inth]->birthday.day, ptscore[inth]->score[0], ptscore[inth]->score[1], ptscore[inth]->score[2], ptscore[inth]->score[3], ptscore[inth]->score[3] / 3);
}
/*显示姓名排序后的学生信息*/
void printsi_p1(struct student *parray[], int n)
{
int i, k = 0;
printf(" 按姓名排序后的学生信息\n\n");
printf(" 学号 姓名 性别 年 月 日 数学 英语 C 总分\n");
printf("┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉\n");
printf("========================================================\n");
for (i = 0; i<n; i++)
{
printf(" %-8s%-8s%-2s %4d %2d %2d %4d %4d %4d %5d\n", parray[i]->no, parray[i]->name, parray[i]->sex, parray[i]->birthday.year, parray[i]->birthday.month, parray[i]->birthday.day, parray[i]->score[0], parray[i]->score[1], parray[i]->score[2], parray[i]->score[3]);
k++;
if (k % 20 == 0)
scanf("%*c");
}
}
/* 显示按总分排序后的学生信息 */
void printsi_p2(struct student *ptscore[], int n)
{
int i, k = 0;
printf("\n\n");
printf(" 按总分排序后的学生信息表\n");
printf(" ==================\n\n");
printf("┏━━━━┳━━━━┳━━━┳━━━━━━┳━━━┳━━━┳━━━┳━━━┓\n");
printf("┃学号 ┃姓名 ┃ 性别 ┃ 年 月 日 ┃ 数学 ┃ 英语 ┃ C ┃ 总分 ┃\n");
printf("┣━━━━╋━━━━╋━━━╋━━━━━━╋━━━╋━━━╋━━━╋━━━┫\n");
printf("┣━━━━╋━━━━╋━━━╋━━━━━━╋━━━╋━━━╋━━━╋━━━┫\n");
for (i = 0; i<n; i++)
{
printf("┃%-8s┃%-8s┃ %-2s ┃ %4d %2d %2d ┃ %4d ┃ %4d ┃ %4d ┃ %4d ┃\n", ptscore[i]->no, ptscore[i]->name, ptscore[i]->sex, ptscore[i]->birthday.year, ptscore[i]->birthday.month, ptscore[i]->birthday.day, ptscore[i]->score[0], ptscore[i]->score[1], ptscore[i]->score[2], ptscore[i]->score[3]);
if (i == n - 1)
printf("┗━━━━┻━━━━┻━━━┻━━━━━━┻━━━┻━━━┻━━━┻━━━┛\n");
else
printf("┣━━━━╋━━━━╋━━━╋━━━━━━╋━━━╋━━━╋━━━╋━━━┫\n");
}
}
void main()
{
static struct student stud[N];
struct student *pstud[N];
struct student *pname[N];
struct student *ptscore[N];
int code, inth = 0;
int n = 0, i;
readsi(stud, &n);
for (i = 0; i<N; i++)
pstud[i] = &stud[i];
for (i = 0; i<N; i++)
pname[i] = &stud[i];
for (i = 0; i<N; i++)
ptscore[i] = &stud[i];
bsort(pname, n);
ssort(ptscore, n);
printf("\n 按<Enter>,进入菜单:\n");
scanf("%*c");
while (1)
{
code = menu();
switch (code)
{
case 0: /* 退出 */
exit(1);
case 1: /* 显示学生信息 */
printsi(stud, n);
scanf("%*2c");
break;
case 2: /* 显示按姓名排序后的学生信息 */
printsi_p1(pname, n);
scanf("%*2c");
break;
case 3: /* 显示按总分排序后的学生信息 */
printsi_p2(ptscore, n);
scanf("%*2c");
break;
case 4: /* 按学号查找学生信息和平均分数 */
ssrch_no(pstud, n);
scanf("%*2c");
break;
case 5: /* 按姓名查找学生信息和平均分数 */
bsrch_name(pname, n);
scanf("%*2c");
break;
case 6: /* 查各门课的平均分数 */
find_ave(stud, n);
scanf("%*2c");
break;
case 7: /* 查男女人数 */
find_pnum(stud, n);
scanf("%*2c");
break;
case 8: /* 显示学生信息表 */
printtable(stud, n);
scanf("%*2c");
break;
}
}
}
一个简单的学生管理系统
最新推荐文章于 2024-01-15 19:46:15 发布