求各路大神指教,文件数据出现乱码该如何解决
#include "stdio.h" #include "string.h"
#include"stdlib.h"
#include"conio.h"
/*定义学生结构体*/
struct Student
{
char ID[20]; //学号
char Name[20]; //姓名
float C_Mark; //C语言成绩
float M_Mark; //高数成绩
float E_Mark; //英语成绩
float Average; //平均分
};
/*声明学生数组及学生数量*/
struct Student students[1000];
int num=0;
/*平均值*/
float Avg(struct Student stu)
{
return (stu.C_Mark+stu.M_Mark+stu.E_Mark)/3;
}
/*通过学号返回数组下标*/
int Student_SearchByIndex(char id[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].ID,id)==0)
{
return i;
}
}
return -1;
}
/*通过姓名返回数组下标*/
int Student_SearchByName(char name[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].Name,name)==0)
{
return i;
}
}
return -1;
}
/*显示单条学生记录*/
void Student_DisplaySingle(int index)
{
printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","C语言成绩","高数成绩","英语成绩","平均分");
printf("-------------------------------------------------------------\n");
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[index].ID,students[index].Name,students[index].C_Mark,students[index].M_Mark,students[index].E_Mark,students[index].Average);
}
/****1、增加学生记录*****/
void Student_Insert()
{
while(1)
{
printf("请输入学号:");
scanf("%s",&students[num].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[num].Name);
getchar();
printf("请输入C语言成绩:");
scanf("%f",&students[num].C_Mark);
getchar();
printf("请输入高数成绩:");
scanf("%f",&students[num].M_Mark);
getchar();
printf("请输入英语成绩:");
scanf("%f",&students[num].E_Mark);
getchar();
students[num].Average=Avg(students[num]);
num++;
printf("是否继续?(Y/N),继续请按任意键,否则输入N:");
if (getchar()=='N')
{
break;
}
}
}
/******2、修改学生信息**********/
void Student_Modify()
{
float C_Mark,M_Mark,E_Mark;
while(1)
{
char id[20];
int index;
printf("请输入要修改的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("该学生不存在!\n");
}
else
{
printf("你要修改的学生信息为:\n");
Student_DisplaySingle(index);
printf("-- 请输入新值--\n");
printf("请输入学号:");
scanf("%s",&students[index].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[index].Name);
getchar();
printf("请输入C语言成绩:");
scanf("%f",&students[index].C_Mark);
getchar();
printf("请输入高数成绩:");
scanf("%f",&students[index].M_Mark);
getchar();
printf("请输入英语成绩:");
scanf("%f",&students[index].E_Mark);
getchar();
students[index].Average=Avg(students[index]);
}
printf("是否继续?(Y/N),继续请按任意键,否则输入N:");
if (getchar()=='N')
{
break;
}
}
}
/****3、删除学生信息****/
void Student_Delete()
{
int i;
while(1)
{
char id[20];
int index;
printf("请输入要删除的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要删除的学生信息为:\n");
Student_DisplaySingle(index);
printf("是否真的要删除?(Y/N)");
if (getchar()=='Y')
{
for (i=index;i<num-1;i++)
{
students[i]=students[i+1];//把后边的对象都向前移动
}
num--;
}
getchar();
printf("已删除\n");
}
printf("是否继续删除?(Y/N),继续请按任意键,否则输入N:");
if (getchar()=='N')
{
break;
}
}
}
/****4、按姓名查询******/
void Student_Select()
{
while(1)
{
char name[20];
int index;
printf("请输入要查询的学生的姓名:");
scanf("%s",&name);
getchar();
index=Student_SearchByName(name);
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要查询的学生信息为:\n");
Student_DisplaySingle(index);
}
printf("是否继续?(Y/N),继续请按任意键,否则输入N:");
if (getchar()=='N')
{
break;
}
}
}
/*统计每个等级的人数以及所占的百分比*/
void Student_Showgrade()
{
int x1 = 0;
int x2 = 0;
int x3 = 0;
int x4 = 0;
int x5 = 0;
for(int i = 0; i < num; i++)
{
float temp = students[i].C_Mark;
if(temp >= 90.00 ) x1++;
else if(temp >= 80.00 ) x2++;
else if(temp >= 70.00 ) x3++;
else if(temp >= 60.00 ) x4++;
else x5++;
}
printf("优秀:%.2lf\n",(double)(x1) / (double)(num));
printf("良好:%.2lf\n",(double)(x2) / (double)(num));
printf("中等:%.2lf\n",(double)(x3) / (double)(num));
printf("及格:%.2lf\n",(double)(x4) / (double)(num));
printf("不及格:%.2lf\n",(double)(x5) / (double)(num));
}
/*按平均值排序*/
void Student_SortByAverage()
{
int i,j;
struct Student tmp;
for(i=0;i<num;i++)
{
for(j=0;j<num-1;j++)
{
if(students[j-1].Average<students[j].Average)
{
tmp=students[j-1];
students[j-1]=students[j];
students[j]=tmp;
}
}
}
}
/*显示学生信息*/
void Student_Display()
{
int i;
printf("%10s%10s%8s%8s%8s%810s\n","学号","姓名","C语言成绩","高数成绩","英语成绩","平均分");
printf("-------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[i].ID,students[i].Name,students[i].C_Mark,students[i].M_Mark,students[i].E_Mark,students[i].Average);
}
}
/*将学生信息从文件(Database.txt)中读出*/
void IO_ReadInfo()
{
FILE *fp;
int i;
if ((fp=fopen("Database.txt","rb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fread(&num,sizeof(int),1,fp)!=1)
{
num=-1;
}
else
{
for(i=0;i<num;i++)
{
fread(&students[i],sizeof(struct Student),1,fp);
}
}
fclose(fp);
}
/*将学生信息写入文件(Database.txt)*/
void IO_WriteInfo()
{
FILE *fp;
int i;
if ((fp=fopen("Database.txt","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("写入文件错误!\n");
}
for (i=0;i<num;i++)
{
if (fwrite(&students[i],sizeof(struct Student),1,fp)!=1)
{
printf("写入文件错误!\n");
}
}
fclose(fp);
}
/***********主程序*********/
int main()
{printf("学生成绩管理系统\n");
printf("2019年7月12日\n");
printf("学号:1831050059\n");
printf("班级:计本1802\n");
printf("制作人:\n");
int choice;
IO_ReadInfo(); //读取文件
while(1)
{
/*主菜单*/
printf("\n------ 学生成绩管理系统--------\n");
printf("-------1. 增加学生记录---------\n");
printf("-------2. 修改学生记录---------\n");
printf("-------3. 删除学生记录---------\n");
printf("-------4. 按姓名查询学生记录---\n");
printf("-------5. 统计等级所占百分比---\n");
printf("-------6. 按平均分排序---------\n");
printf("-------7. 退出-----------------\n");
printf("-------请选择(1-7):");
scanf("%d",&choice);
getchar();
switch(choice)
{
case 1:
Student_Insert();
break;
case 2:
Student_Modify();
break;
case 3:
Student_Delete();
break;
case 4:
Student_Select();
break;
case 5:
Student_Showgrade();
break;
case 6:
Student_SortByAverage();
Student_Display();
break;
case 7:
exit(0);
break;
}
IO_WriteInfo();
}
}
[此贴子已经被作者于2019-8-7 15:49编辑过]