- 信息输入
- 总分
- 平均分
- 最高成绩
- 最低成绩
- 成绩排序
- 删除信息
- 查找信息
- 按学号姓名修改成绩
- 写入二进制文件
- 读出二进制文件
- 读写TXT文件
- 退出
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define MAXSIZE 2 //初始数组大小
#define MATHSCORE 1 //数学成绩标识
#define CHINESESCORE 2 //语文成绩标识
#define ASC 3 //升序
#define DESC 4 //降序
#define DELETEDATA 7 //删除
#define SEARCHDATA 8 //查找
#define MODIFYDATA 9 //修改
//定义枚举
enum CHOOSE{
INPUT=1, //信息输入
SUMSCORE, //总分
AVGSCORE, //平均分
MAXSCORE, //最高成绩
MINSCORE, //最低成绩
SORTSCORE, //成绩排序
DELETEMESSAGE, //删除
SEARCHMESSAGE, //查找
MODIFYSCORE, //修改
SAVEFILE, //保存
READFILE, //读取
WRFILE, //读写
EXIT //退出
};
//定义学生成绩结构体
typedef struct Score_tlg
{
float ChineseScore;
float MathScore;
}SCORE;
//定义学生基本信息结构体
typedef struct Student_tlg
{
long num;
char name[20];
SCORE score;
int Size; //当前信息最大个数(用于数组扩容判断)
}STUDENT;
void print(STUDENT stu[], int length); //打印函数声明
void printOne(STUDENT stu); //打印一个学生信息函数声明
void arr_Dilatation(STUDENT* stu, int length); //数组扩容声明
//查询科目选项
int choose_Course()
{
int choice;
printf("请输入您想选择的科目: \n1 --- 数学 2 --- 语文 \n");
scanf("%d", &choice);
return choice;
}
//排序顺序选项
int choose_Sorttype()
{
int choice;
printf("请输入您想升序还是降序: \n3 --- 升序 4 --- 降序 \n");
scanf("%d", &choice);
return choice;
}
//信息输入
void InitMessage(STUDENT *stu,int* length)
{
if (*length == stu->Size)
{
arr_Dilatation(stu, *length);
}
printf("请输入学生的学号,姓名,语文成绩,数学成绩信息: \n");
scanf("%d %s %f %f", &stu[*length].num, stu[*length].name,
&stu[*length].score.ChineseScore,
&stu[*length].score.MathScore);
(*length)++; //注意运算的问题
printf("输入成功!\n");
}
//总分
float agg_Score(STUDENT *stu,int length,int scoretype) //scoretype成绩类型 传1为数学 传2为语文
{
float math_sum = 0;
float chin_sum = 0;
if (scoretype == MATHSCORE) //数学
{
for (int i = 0; i < length; i++)
{
math_sum += stu[i].score.MathScore;
}
return math_sum;
}
if (scoretype == CHINESESCORE)
{
for (int i = 0; i < length; i++)
{
chin_sum += stu[i].score.ChineseScore;
}
return chin_sum;
}
}
//平均分
float avg_Score(STUDENT *stu, int length,int scoretype)
{
float avg;
avg = agg_Score(stu, length, scoretype)/length;
return avg;
}
//交换函数
void swap(STUDENT* a, STUDENT* b)
{
STUDENT temp = *a;
*a = *b;
*b = temp;
}
//成绩高低排序
void sort_Score(STUDENT* stu, int length, int scoretype, int sorttype) //4 降序 3升序
{
for (int i = 0; i < length - 1; i++)
{
for (int j = 0; j < length - i - 1; j++)
{
if (scoretype == MATHSCORE) //数学
{
if (sorttype == ASC) //升序
{
if (stu[j].score.MathScore > stu[j + 1].score.MathScore)
{
swap(&stu[j], &stu[j + 1]);
}
}
else //降序
{
if (stu[j].score.MathScore < stu[j + 1].score.MathScore)
{
swap(&stu[j], &stu[j + 1]);
}
}
}
else //语文
{
if (sorttype == ASC) //升序
{
if (stu[j].score.ChineseScore > stu[j + 1].score.ChineseScore)
{
swap(&stu[j], &stu[j + 1]);
}
}
else //降序
{
if (stu[j].score.ChineseScore < stu[j + 1].score.ChineseScore)
{
swap(&stu[j], &stu[j + 1]);
}
}
}
}
}
print(stu, length);
}
//最高成绩
STUDENT max_Score(STUDENT stu[], int length, int scoretype)
{
STUDENT studentMax = stu[0];
if (scoretype == MATHSCORE) //数学最高
{
for (int i = 1; i < length; i++)
{
if (studentMax.score.MathScore < stu[i].score.MathScore)
{
studentMax = stu[i];
}
}
}
else
{
for (int i = 1; i < length; i++)
{
if (studentMax.score.ChineseScore < stu[i].score.ChineseScore)
{
studentMax = stu[i];
}
}
}
return studentMax;
}
//最低成绩
STUDENT min_Score(STUDENT stu[], int length, int scoretype)
{
STUDENT studentMin = stu[0];
if (scoretype == MATHSCORE) //数学最低
{
for (int i = 1; i < length; i++)
{
if (studentMin.score.MathScore > stu[i].score.MathScore)
{
studentMin = stu[i];
}
}
}
else
{
for (int i = 1; i < length; i++)
{
if (studentMin.score.ChineseScore > stu[i].score.ChineseScore)
{
studentMin = stu[i];
}
}
}
return studentMin;
}
//根据姓名删除信息
void delete_Message(STUDENT* stu,int* length,char* name)
{
for (int i = 0; i < *length; i++)
{
if (strcmp(stu[i].name,name) == 0) //找到学生
{
for (int j = i; j < *length; j++)
{
stu[j] = stu[j + 1]; //元素覆盖
}
//stu[*length - 1] = (STUDENT){ 0 }; //将最后一个元素置为空
(*length)--;
printf("该学生信息已经删除\n");
break;
}
}
}
//按姓名查找
void seek_Message(STUDENT* stu, int* length, char* name)
{
for (int i = 0; i < *length; i++)
{
if (strcmp(stu[i].name, name) == 0) //找到学生
{
printOne(stu[i]);
break;
}
}
}
//数组扩容
void arr_Dilatation(STUDENT* stu,int length)
{
(STUDENT*)realloc(stu,sizeof(STUDENT)*length * 2);
stu->Size = 2 * length;
}
//按照学号姓名修改成绩
void modify_Score(STUDENT* stu, int length, char* name, int scoretype)
{
for (int i = 0; i < length; i++)
{
if (strcmp(stu[i].name, name) == 0) //找到学生
{
float math = 0;
float chinese = 0;
if (scoretype == MATHSCORE) //修改数学
{
printf("请输入您想修改%s数学成绩: ", stu[i].name);
scanf("%f", &math);
stu[i].score.MathScore = math;
}
else if (scoretype == CHINESESCORE) //修改语文
{
printf("请输入您想修改%s语文成绩: ", stu[i].name);
scanf("%f", &chinese);
stu[i].score.ChineseScore = chinese;
}
else //均修改
{
printf("请输入您想修改%s数学和语文成绩: ", stu[i].name);
scanf("%f %f", &math,&chinese);
stu[i].score.MathScore = math;
stu[i].score.ChineseScore = chinese;
}
}
}
}
//打印所有信息
void print(STUDENT stu[], int length)
{
for (int i = 0; i < length; i++)
{
printf("学号: %d\t姓名: %s\t数学成绩: %.2f\t\t语文成绩: %.2f\n",
stu[i].num, stu[i].name, stu[i].score.MathScore, stu[i].score.ChineseScore);
}
}
//打印一个学生的信息
void printOne(STUDENT stu)
{
printf("学号: %d\t姓名: %s\t数学成绩: %.2f\t\t语文成绩: %.2f\n",
stu.num, stu.name, stu.score.MathScore, stu.score.ChineseScore);
}
//写入二进制文件
void saveFile(STUDENT* stu,int length)
{
FILE* fp = fopen("./student.data", "wb"); //二进制写入
if (NULL == fp)
{
printf("文件打开失败!\n");
return;
}
fwrite(stu, sizeof(STUDENT), length, fp);
fclose(fp);
printf("数据写入文件成功!");
}
//从文件中读取二进制文件
void readFile(STUDENT* stu,int length)
{
FILE* fp = fopen("./student.data", "r");
if (NULL == fp)
{
printf("文件打开失败!\n");
return;
}
do
{
fread(stu + length, sizeof(STUDENT), length, fp);
if (feof) //是否到文件末尾
{
break;
}
length++;
} while (1);
fclose(fp);
}
//读写TXT文件
void rwFile(STUDENT* stu, int* length)
{
FILE* fp = fopen("./student.txt", "w+");
if (NULL == fp)
{
printf("文件打开失败!\n");
return;
}
for (int i = 0; i < *length; i++) //写入文件
{
fprintf(fp, "%d\t%s\t%.2f\t%.2f\n", stu[i].num, stu[i].name,
stu[i].score.MathScore,
stu[i].score.ChineseScore);
}
printf("文件写入成功!\n");
//文件读取
rewind(fp); //重置文件指针到文件开头
//开辟缓存空间
STUDENT* pstu = (STUDENT*)malloc(sizeof(STUDENT)*(*length));
for (int i = 0; ; i++)
{
//文件读取
int isok = fscanf(fp, "%d\t%s\t%f\t%f", &stu[i].num, stu[i].name,
&stu[i].score.MathScore,
&stu[i].score.ChineseScore);
if (isok == EOF)
{
break;
}
printf("学号: %d\t姓名: %s\t数学成绩: %.2f\t\t语文成绩: %.2f\n",
stu[i].num, stu[i].name, stu[i].score.MathScore,
stu[i].score.ChineseScore);
}
fclose(fp);
free(pstu);
}
//菜单
void menu(STUDENT *stu, int* length)
{
system("color 02"); //设置控制台背景颜色以及字体颜色
printf("\n\n");
printf("\t\t**************************************\n");
printf("\t\t********** 1--信息输入 *****\n");
printf("\t\t********** 2--总分 *****\n");
printf("\t\t********** 3--平均分 *****\n");
printf("\t\t********** 4--最高成绩 *****\n");
printf("\t\t********** 5--最低成绩 *****\n");
printf("\t\t********** 6--成绩排序 *****\n");
printf("\t\t********** 7--删除信息 *****\n");
printf("\t\t********** 8--查找信息 *****\n");
printf("\t\t********** 9--按学号姓名修改成绩 *****\n");
printf("\t\t********** 10--写入二进制文件 *****\n");
printf("\t\t********** 11--读出二进制文件 *****\n");
printf("\t\t********** 12--读写TXT文件 *****\n");
printf("\t\t********** 13--退出 *****\n");
printf("\t\t**************************************\n");
printf("\n\n");
int choice = 0;
char name[10] = " ";
printf("请输入您想执行的操作: \n");
scanf("%d", &choice);
switch (choice)
{
case INPUT:
InitMessage(stu, &(*length));
break;
case SUMSCORE:
printf("总分为: %.2f\t\n", agg_Score(stu, *length, choose_Course()));
break;
case AVGSCORE:
printf("平均分为: %.2f\t\n", avg_Score(stu, *length, choose_Course()));
break;
case MAXSCORE:
printOne(max_Score(stu, *length, choose_Course()));
break;
case MINSCORE:
printOne(min_Score(stu, *length, choose_Course()));
break;
case SORTSCORE:
sort_Score(stu, *length, choose_Course(), choose_Sorttype());
break;
case DELETEMESSAGE:
printf("请输入您想删除的学生的姓名: \n");
scanf("%s", name);
delete_Message(stu, &(*length), name);
break;
case SEARCHMESSAGE:
printf("请输入您想查找的学生的姓名: \n");
scanf("%s", name);
seek_Message(stu, &(*length), name);
break;
case MODIFYSCORE:
printf("请输入您想修改的学生的姓名: \n");
scanf("%s", name);
modify_Score(stu, *length, name, choose_Course());
break;
case SAVEFILE:
saveFile(stu, *length);
break;
case READFILE:
readFile(stu, *length);
break;
case WRFILE:
rwFile(stu, &(*length));
break;
case EXIT:
exit(1);
default:
printf("输入错误,请重新输入!\n");
break;
}
}
int main()
{
srand((unsigned int)time(NULL));
STUDENT stu[MAXSIZE] = { 0 };
stu->Size = MAXSIZE;
int curSize = 0; //当前元素个数 改变结构体组成时注意插入数据的结构体结构
//循环插入元素
for (int i = 0; i < 10; i++)
{
stu[i] = (STUDENT){ rand() % 100 + 401100, "张三",
rand() % 60 + (float)40.00,
rand() % 60 + (float)40.00 }; //随机生成
curSize++;
}
while (1)
{
system("CLS"); //清屏
menu(stu, &curSize);
system("pause");
}
system("pause");
return 0;
}