📄 studentmis_0.80.040502.1.cpp
字号:
/*------------------------------------------------------------*\
* 功能增强说明 *
* *1. 科目名称处理方式有所变化,变为全局字符串数组处理 *
* *2. 结构体变量不再使用静态内存空间,而是使用动态内存空间 *
* 3. 增加了增、删、改、查、排序等功能 *
* *4. 增加了学员信息管理功能 *
\*------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OBJNUM 4 /* 科目数量,需要改动全局字符串数组 pstzObjectName[OBJNUM]
默认四科为“语文,数学,英语,政治” */
#define STUNUM 30 /* 学生最大人数 */
typedef struct
{
char * pstzName;
char * pstzID;
float Score[OBJNUM];
/* 新增学员信息 */
int iAge;
char cSex;
char * pstzAddress;
} Student; /* 直接定义 struct 在标准C 下不能编译 */
void welcomeInfo();
void printMenu();
char choiseMenu();
void inputInfo(Student * []);
void outputInfo(const Student * []);
void inputScore(Student * []);
void outputScore(const Student * []);
int isScore(const float);
void saveFile(const Student * []);
void loadFile(Student * []);
void unload(Student * []);
void order(Student * []); /* 选择法排序,对原参数有副作用 */
void find(const Student * []); /* 按学号查找并输出个人信息及个人成绩单 */
char buff[256]; /* 输入缓冲区,所有文件公用 */
char * pstzObjectName[OBJNUM] = {"语文", "数学", "英语", "政治"};
void main()
{
char cChoise;
Student * stu[STUNUM] = {NULL};
welcomeInfo();
do{
printMenu();
cChoise = choiseMenu();
switch (cChoise)
{
case '1':
inputInfo(stu);
inputScore(stu);
break;
case '2':
outputInfo(stu);
outputScore(stu);
break;
case '3':
order(stu);
break;
case '4':
find(stu);
break;
/*
case '5':
//新加
break;
case '6':
//删除
break;
case '7':
//更改
break;
*/
case '8':
saveFile(stu); break;
case '9':
loadFile(stu); break;
case '0':
unload(stu); break;
default:
printf("\a\n该功能未实现\n菜单无效选择,请与程序设计者联系。\n\n\n");
break;
}
} while (cChoise != '0');
printf("..........\n");
printf("欢迎使用!\n");
printf("有任何意见或建议欢迎与 sunny_tang_1983@163.com 联系\n");
}
void welcomeInfo()
{
printf("Windows 2000 (TM) 版本 5.00 (内部版本号043212) \n");
printf("欢迎使用 SOPHIA 学生信息管理系统 \n");
printf("学生信息管理系统(内部测试标准C版) 内部版本号 0.80.051002.1 \n\n");
}
void printMenu()
{
printf("\n1.输入 2.输出 3.排序 4.查找 5.新加 6.删除 7.更改 8.保存 9.加载 0.退出\n");
}
char choiseMenu()
{
char cTemp;
printf("\n请选择操作:");
do{
scanf("%s", buff);
sscanf(buff, " %c", &cTemp);
} while ((cTemp > '9' || cTemp < '0')
&& printf("\n无效选择,请重选:"));
return cTemp;
}
void inputInfo(Student * stu[])
{
int i = 0, j = 0;
char flag = '\0';
unload(stu);
do {
stu[i] = (Student *)malloc(sizeof(Student));
printf("学号:");
scanf("%s", buff);
stu[i]->pstzID = (char *)malloc(strlen(buff) + 1);
strcpy(stu[i]->pstzID, buff);
printf("姓名:");
scanf("%s", buff);
stu[i]->pstzName = (char *)malloc(strlen(buff) + 1);
strcpy(stu[i]->pstzName, buff);
printf("年龄:");
do {
scanf("%s", buff);
sscanf(buff, " %d", &stu[i]->iAge);
} while ((stu[i]->iAge > 100 || stu[i]->iAge < 0)
&& printf("不合法年龄,请重新输入年龄:"));
printf("性别(F/M):");
do {
scanf("%s", buff);
sscanf(buff, " %c", &stu[i]->cSex);
} while ((stu[i]->cSex != 'f' && stu[i]->cSex != 'F'
&& stu[i]->cSex != 'm' && stu[i]->cSex != 'M')
&& printf("性别输入错误,请重新输入性别:"));
printf("地址:");
scanf("%s", buff);
stu[i]->pstzAddress = (char *)malloc(strlen(buff) + 1);
strcpy(stu[i]->pstzAddress, buff);
i++;
printf("是否结束 (Y/N)?");
scanf(" %c", &flag);
} while (i < STUNUM && flag != 'Y' && flag != 'y');
printf("一共录入%d个人信息\n", i);
}
void outputInfo(const Student * stu[])
{
int i = 0;
/* 结构信息 */
puts("\t\t\t学员情况表单");
puts("__________________________________________________________________");
printf("学号\t姓名\t\t年龄\t性别\t地址\n");
/* 输出成绩单数据 */
for (i = 0; i < STUNUM && stu[i] != NULL; i++)
{
printf("%s\t%-16s%d\t", stu[i]->pstzID, stu[i]->pstzName, stu[i]->iAge);
if (stu[i]->cSex == 'f' || stu[i]->cSex == 'F')
printf("男\t");
else if (stu[i]->cSex == 'm' || stu[i]->cSex == 'M')
printf("女\t");
else
printf("\n\a\a\a错误,无效性别,检查数据文件\n");
printf("%s\n", stu[i]->pstzAddress);
}
printf(" 共计%d人\n", i);
puts("__________________________________________________________________");
}
void inputScore(Student * stu[])
{
int i = 0, j = 0;
char flag = '\0';
do {
for (j = 0; j < OBJNUM; j++)
{
printf("输入%s同学的%s成绩:", stu[i]->pstzName, pstzObjectName[j]);
do {
scanf("%s", buff);
sscanf(buff, " %f", &stu[i]->Score[j]);
} while ( !isScore(stu[i]->Score[j])
&& printf("输入无效成绩,请重新输入"));
}
i++;
printf("是否结束 (Y/N)?");
scanf(" %c", &flag);
} while (i < STUNUM && stu[i] != NULL /* 下一个人还没有存在 */
&& flag != 'Y' && flag != 'y');
printf("一共录入%d个人成绩\n", i);
}
void outputScore(const Student * stu[])
{
int i = 0, j = 0;
float sum;
float CountScore[OBJNUM];
for (j = 0; j < OBJNUM; j++)
{
CountScore[j] = 0;
}
/* 输出成绩单结构信息 */
puts("\t\t\t成绩单");
puts("__________________________________________________________________");
printf("学号\t姓名\t");
for (j = 0; j < OBJNUM; j++)
{
printf("%s\t", pstzObjectName[j]);
}
printf("总分\t平均分\n");
/* 输出成绩单数据 */
for (i = 0; i < STUNUM && stu[i] != NULL; i++)
{
printf("%s\t%s\t", stu[i]->pstzID, stu[i]->pstzName);
sum = 0;
for (j = 0; j < OBJNUM; j++)
{
sum += stu[i]->Score[j];
CountScore[j] += stu[i]->Score[j];
printf("%.2f\t", stu[i]->Score[j]);
}
printf("%.2f\t%.2f\n", sum, sum / OBJNUM); /* 输出每个人的统计信息 */
}
/* 输出每科的统计信息 */
printf("\n总分\t\t");
for (j = 0; j < OBJNUM; j++)
{
printf("%.2f\t", CountScore[j]);
}
printf("\n平均分\t\t");
for (j = 0; j < OBJNUM; j++)
{
printf("%.2f\t", CountScore[j] / i);
}
printf(" 共计%d人\n", i);
puts("__________________________________________________________________");
}
int isScore(const float score)
{
if (score <= 100 && score >= 0)
return 1;
return 0;
}
void saveFile(const Student * stu[])
{
int i, j;
FILE * fp;
if (NULL == (fp = fopen("c:\\temp.gmy", "w")))
{
printf("\n\a\a\a磁盘错误!\n");
exit(0);
}
for (i = 0; i < STUNUM && stu[i] != NULL; i++)
{
fprintf(fp, "%s\t%s\t%d\t%c\t%s\t", stu[i]->pstzID, stu[i]->pstzName, stu[i]->iAge,
stu[i]->cSex, stu[i]->pstzAddress);
for (j = 0; j < OBJNUM; j++)
{
fprintf(fp, "%.2f\t", stu[i]->Score[j]);
}
fprintf(fp, "\n");
printf(">");
}
fclose(fp);
printf("\n信息保存成功!共保存%d条信息。\n", i);
}
void loadFile(Student * stu[])
{
int i, j;
FILE * fp;
if (NULL == (fp = fopen("c:\\temp.gmy", "r")))
{
printf("\n\a\a\a磁盘错误!\n");
exit(0);
}
/* 释放原有动态内存区空间 */
unload(stu);
for (i = 0; i < STUNUM && !feof(fp); i++)
{
stu[i] = (Student *)malloc(sizeof(Student));
fscanf(fp, "%s\t", buff);
stu[i]->pstzID = (char *)malloc(strlen(buff) + 1);
strcpy(stu[i]->pstzID, buff);
fscanf(fp, "%s\t", buff);
stu[i]->pstzName = (char *)malloc(strlen(buff) + 1);
strcpy(stu[i]->pstzName, buff);
fscanf(fp, "%d\t%c\t", &stu[i]->iAge, &stu[i]->cSex);
fscanf(fp, "%s\t", buff);
stu[i]->pstzAddress = (char *)malloc(strlen(buff) + 1);
strcpy(stu[i]->pstzAddress, buff);
for (j = 0; j < OBJNUM; j++)
{
fscanf(fp, "%f\t", &stu[i]->Score[j]);
}
printf(">");
}
fclose(fp);
printf("\n信息加载成功!共加载%d条信息。\n", i);
}
void unload(Student * stu[])
{
int i = 0;
for (i = 0; i < STUNUM && stu[i] != NULL; i++)
{
free(stu[i]->pstzAddress);
free(stu[i]->pstzID);
free(stu[i]->pstzName);
free(stu[i]);
}
}
void order(Student * stu[])
{
int i = 0, j = 0;
int iCount = 0; /* 学生总数 */
int iTemp = 0;
float score[STUNUM] = {0};
float fTemp = 0;
Student * stuTemp = NULL;
for (i = 0; i < STUNUM && stu[i] != NULL; i++);
iCount = i; /* 得到实际人数 */
/* 计算统计信息作为排序依据 */
for (i = 0; i < iCount; i++)
{
for (j = 0; j < OBJNUM; j++)
{
score[i] += stu[i]->Score[j];
}
}
/* 选择法排序 */
for (i = 0; i < iCount; i++)
{
iTemp = i; /* 做标记 */
for (j = i; j < iCount; j++)
{
if (score[j] >= score[iTemp]) /* 降序排列 */
{
iTemp = j; /* 记录极值 */
}
}
fTemp = score[i];
score[i] = score[iTemp];
score[iTemp] = fTemp;
stuTemp = stu[i];
stu[i] = stu[iTemp];
stu[iTemp] = stuTemp;
}
}
void find(const Student * stu[])
{
int i = 0, j = 0;
float sum = 0;
printf("请输入学号:");
scanf("%s", buff); /* 不太好, 应该另存 */
for (i = 0; i < STUNUM && stu[i] != NULL; i++)
{
if (strcmp(stu[i]->pstzID, buff) == 0)
{
puts("\n\n\t\t个人成绩单");
puts("__________________________________________________");
/* 输出个人信息 */
printf("学号:%-10s 年龄:%d\n", stu[i]->pstzID, stu[i]->iAge);
printf("姓名:%-10s 性别:", stu[i]->pstzName);
if (stu[i]->cSex == 'f' || stu[i]->cSex == 'F')
printf("男\n");
else if (stu[i]->cSex == 'm' || stu[i]->cSex == 'M')
printf("女\n");
else
printf("\n\a\a\a错误,无效性别,检查数据文件\n");
printf("地址:%s\n\n", stu[i]->pstzAddress);
/* 输出成绩 */
for (j = 0; j < OBJNUM; j++)
{
printf("%s\t", pstzObjectName[j]);
}
printf("总分\t平均分\n");
for (j = 0; j < OBJNUM; j++)
{
sum += stu[i]->Score[j];
printf("%.2f\t", stu[i]->Score[j]);
}
printf("%.2f\t%.2f\n", sum, sum / OBJNUM);
puts("__________________________________________________");
return;
}
}
printf("\n\a查无此人,请重新确认后再查!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -