|
|
@ -0,0 +1,335 @@
|
|
|
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
|
|
|
#include<io.h>
|
|
|
|
|
|
|
|
#include <unistd.h> //int access(const char *, int); // 获取文件的权限
|
|
|
|
|
|
|
|
#include<stdlib.h> // system()
|
|
|
|
|
|
|
|
#include<string.h>
|
|
|
|
|
|
|
|
#define MAX 200 //定义MAX表示可录入最大学生数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct student //student结构体名 (成员结构类型)定义了学生的基本信息
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
char no[10]; //成员变量 成员名 学号
|
|
|
|
|
|
|
|
char name[50]; //姓名
|
|
|
|
|
|
|
|
float score; //成绩
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct data //主结构类型
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int count; //学生个数
|
|
|
|
|
|
|
|
struct student stu[MAX]; //结构类型的嵌套
|
|
|
|
|
|
|
|
}dd; //dd是结构体data类型的变量 全局结构体变量名
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void menu();
|
|
|
|
|
|
|
|
void to_menu();
|
|
|
|
|
|
|
|
void add_data();
|
|
|
|
|
|
|
|
void edit_data();
|
|
|
|
|
|
|
|
void fen_shu_duan();
|
|
|
|
|
|
|
|
void query_data_name();
|
|
|
|
|
|
|
|
void query_data_no();
|
|
|
|
|
|
|
|
void sort_data_score();
|
|
|
|
|
|
|
|
void read_data();
|
|
|
|
|
|
|
|
void view_data();
|
|
|
|
|
|
|
|
void save_data();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void menu()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
system("cls"); /*清屏*/
|
|
|
|
|
|
|
|
printf("\n\n\n\n\n");
|
|
|
|
|
|
|
|
printf("\t\t|-----------------------学生C语言成绩记录簿----------------------|\n");
|
|
|
|
|
|
|
|
printf("\t\t|\t 0. 退出 |\n");
|
|
|
|
|
|
|
|
printf("\t\t|\t 1. 查看所有学生成绩 |\n");
|
|
|
|
|
|
|
|
printf("\t\t|\t 2. 输入学生成绩 |\n");
|
|
|
|
|
|
|
|
printf("\t\t|\t 3. 修改学生成绩 |\n");
|
|
|
|
|
|
|
|
printf("\t\t|\t 4. 查询成绩(学号) |\n");
|
|
|
|
|
|
|
|
printf("\t\t|\t 5. 查询成绩(姓名) |\n");
|
|
|
|
|
|
|
|
printf("\t\t|\t 6. 及格率 |\n");
|
|
|
|
|
|
|
|
printf("\t\t|\t 7. 成绩分数段 |\n");
|
|
|
|
|
|
|
|
printf("\t\t|----------------------------------------------------------------|\n");
|
|
|
|
|
|
|
|
printf("\t\t ");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//回到主菜单
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void to_menu()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
char c1,c2;
|
|
|
|
|
|
|
|
printf("\n\n按回车返回主菜单…");
|
|
|
|
|
|
|
|
scanf("%c%c",&c1,&c2); //第一个字符确认上次的确认回车键
|
|
|
|
|
|
|
|
menu();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
对应的颜色码表:
|
|
|
|
|
|
|
|
0 = 黑色 8 = 灰色
|
|
|
|
|
|
|
|
1 = 蓝色 9 = 淡蓝色
|
|
|
|
|
|
|
|
2 = 绿色 A = 淡绿色
|
|
|
|
|
|
|
|
3 = 浅绿色 B = 淡浅绿色
|
|
|
|
|
|
|
|
4 = 红色 C = 淡红色
|
|
|
|
|
|
|
|
5 = 紫色 D = 淡紫色
|
|
|
|
|
|
|
|
6 = 黄色 E = 淡黄色
|
|
|
|
|
|
|
|
7 = 白色 F = 亮白色
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
system("color E9");
|
|
|
|
|
|
|
|
int fun;
|
|
|
|
|
|
|
|
read_data();
|
|
|
|
|
|
|
|
menu();
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\t\t 请输入功能号:",&fun);
|
|
|
|
|
|
|
|
scanf("%d",&fun);
|
|
|
|
|
|
|
|
switch(fun)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
|
|
view_data();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
|
|
add_data();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
|
|
edit_data();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
|
|
query_data_no();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
|
|
|
|
query_data_name();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
|
|
|
|
sort_data_score();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
|
|
|
|
fen_shu_duan();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fun==0)break;
|
|
|
|
|
|
|
|
to_menu();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void view_data() // 查询总表
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("学号\t 姓名\t C语言成绩");
|
|
|
|
|
|
|
|
printf("\n--------------------------------------------------------------------------\n");
|
|
|
|
|
|
|
|
for(i=0;i<dd.count;i++)
|
|
|
|
|
|
|
|
printf("%s\t%s\t%.lf\n",dd.stu[i].no,dd.stu[i].name,dd.stu[i].score);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void save_data()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
int i,k;
|
|
|
|
|
|
|
|
k=dd.count;
|
|
|
|
|
|
|
|
fp=fopen("d/:chen.txt","w"); //打开文件以便写入数据
|
|
|
|
|
|
|
|
fwrite(&k,sizeof(int),1,fp); // k输出数据的地址 将k的数据输入到fp文件中,读写一次 每次输入占sizeof(int)个字节
|
|
|
|
|
|
|
|
for(i=0;i<k;i++) //准备要写入文件的数组
|
|
|
|
|
|
|
|
fwrite(&dd.stu[i].no,sizeof(struct student),1,fp);
|
|
|
|
|
|
|
|
fclose(fp); //写入完毕,关闭文件
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sort_data_score() //及格率
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int a[2]={0,0};
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
int j,k;
|
|
|
|
|
|
|
|
float pass;
|
|
|
|
|
|
|
|
for(i=0;i<dd.count;i++) // for循环每个学号的学生
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(dd.stu[i].score>=60&&dd.stu[i].score<=100)
|
|
|
|
|
|
|
|
a[0]++;
|
|
|
|
|
|
|
|
else if(dd.stu[i].score<60)
|
|
|
|
|
|
|
|
a[1]++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
j=a[0];
|
|
|
|
|
|
|
|
k=a[1];
|
|
|
|
|
|
|
|
pass=j/((j+k)*1.0)*100;
|
|
|
|
|
|
|
|
printf("及格率:%.2f%%",pass);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void add_data() //添加学生成绩
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct student st; // 创建结构体变量st
|
|
|
|
|
|
|
|
int b;
|
|
|
|
|
|
|
|
int k;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\n请输入学生信息:");
|
|
|
|
|
|
|
|
printf("\n学号 \t姓名 \tC语言成绩");
|
|
|
|
|
|
|
|
printf("\n-------------------------------------------------------------------------------\n");
|
|
|
|
|
|
|
|
scanf("%s%s%f",st.no,st.name,&st.score);
|
|
|
|
|
|
|
|
k=dd.count;
|
|
|
|
|
|
|
|
strcpy(dd.stu[k].no,st.no);
|
|
|
|
|
|
|
|
strcpy(dd.stu[k].name,st.name);
|
|
|
|
|
|
|
|
dd.stu[k].score=st.score;
|
|
|
|
|
|
|
|
dd.count++;
|
|
|
|
|
|
|
|
printf("\n\n是否继续添加学生信息[1-Yes 0-No]:");
|
|
|
|
|
|
|
|
scanf("%d",&b);
|
|
|
|
|
|
|
|
if(b==0)break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
save_data();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void read_data() //将数据从文件读到结构体数组中
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
int i,k;
|
|
|
|
|
|
|
|
struct student st;
|
|
|
|
|
|
|
|
k=0;
|
|
|
|
|
|
|
|
if((fp=fopen("d:/chen.txt","r"))==NULL) // 如果文件不存在
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
dd.count=0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fread(&k,sizeof(int),1,fp);
|
|
|
|
|
|
|
|
dd.count=k;
|
|
|
|
|
|
|
|
for(i=0;i<k;i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fread(&dd.stu[i],sizeof(struct student),1,fp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void edit_data() //修改学生成绩
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int i,k;
|
|
|
|
|
|
|
|
char no[10],name[50];
|
|
|
|
|
|
|
|
float score;
|
|
|
|
|
|
|
|
printf("\n请输入要修改学生的学号:");
|
|
|
|
|
|
|
|
scanf("%s",no);
|
|
|
|
|
|
|
|
k=-1;
|
|
|
|
|
|
|
|
for(i=0;i<dd.count;i++) //for循环每个学号的学生
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(strcmp(dd.stu[i].no,no)==0) //若该两个数相同
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
k=i;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(k==-1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("没有找到该同学(学号-%s)",no);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\n学号 \t姓名 \tC语言成绩");
|
|
|
|
|
|
|
|
printf("\n-------------------------------------------------------------------------------\n");
|
|
|
|
|
|
|
|
scanf("%s%s%f",no,name,&score);
|
|
|
|
|
|
|
|
strcpy(dd.stu[k].no,no);
|
|
|
|
|
|
|
|
strcpy(dd.stu[k].name,name);
|
|
|
|
|
|
|
|
dd.stu[k].score=score;
|
|
|
|
|
|
|
|
save_data();
|
|
|
|
|
|
|
|
printf("\n\n编辑学生记录(学号-%s)成功!",no);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void query_data_no() //按学号查找成绩
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int i,k;
|
|
|
|
|
|
|
|
char no[10];
|
|
|
|
|
|
|
|
printf("\n请输入要查询学生的学号:");
|
|
|
|
|
|
|
|
scanf("%s",no);
|
|
|
|
|
|
|
|
k=-1;
|
|
|
|
|
|
|
|
for(i=0;i<dd.count;i++) //for循环每个学号的学生
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(strcmp(dd.stu[i].no,no)==0) //定位结构数组元素 如果学生学号和no的相同证明找到了
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(k==-1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\n\n学号\t姓名\tC语言成绩");
|
|
|
|
|
|
|
|
printf("\n--------------------------------------\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
k=i;
|
|
|
|
|
|
|
|
printf("%s\t%s\t%.1f\n",dd.stu[i].no,dd.stu[i].name,dd.stu[i].score);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(k==-1) //k值没变
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\n\n没有找到该学生学号(学号-%s)!",no);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//查询学号
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void query_data_name() //按姓名查找成绩
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int i,k;
|
|
|
|
|
|
|
|
char name[50];
|
|
|
|
|
|
|
|
printf("\n请输入要查询学生的姓名:");
|
|
|
|
|
|
|
|
scanf("%s",name);
|
|
|
|
|
|
|
|
k=-1;
|
|
|
|
|
|
|
|
for(i=0;i<dd.count;i++) //for循环每个学号的学生
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(strcmp(dd.stu[i].name,name)==0) //定位结构数组元素 如果学生姓名和name的相同证明找到了
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(k==-1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\n\n学号\t姓名\tC语言成绩");
|
|
|
|
|
|
|
|
printf("\n------------------------------\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
k=i;
|
|
|
|
|
|
|
|
printf("%s\t%s\t%.1f\n",dd.stu[i].no,dd.stu[i].name,dd.stu[i].score);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(k==-1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\n\n没有找到该学生姓名(姓名-%s)!",name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void fen_shu_duan() //学生成绩分段
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int a[5]={0,0,0,0,0};
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0;i<dd.count;i++) //for循环每个学号的学生
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(dd.stu[i].score<60)
|
|
|
|
|
|
|
|
a[0]++;
|
|
|
|
|
|
|
|
else if(dd.stu[i].score>=60&&dd.stu[i].score<=69)
|
|
|
|
|
|
|
|
a[1]++;
|
|
|
|
|
|
|
|
else if(dd.stu[i].score>=70&&dd.stu[i].score<=79)
|
|
|
|
|
|
|
|
a[2]++;
|
|
|
|
|
|
|
|
else if(dd.stu[i].score>=80&&dd.stu[i].score<=89)
|
|
|
|
|
|
|
|
a[3]++;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
a[4]++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\t\t60分以下:%d\n",a[0]);
|
|
|
|
|
|
|
|
printf("\t\t60~69分:%d\n",a[1]);
|
|
|
|
|
|
|
|
printf("\t\t70~79分:%d\n",a[2]);
|
|
|
|
|
|
|
|
printf("\t\t80~89分:%d\n",a[3]);
|
|
|
|
|
|
|
|
printf("\t\t90分以下:%d\n",a[4]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|