标签:++ 指针 链表 头结点 直接插入 signed 常量 child 查询
func.h //头函数
//按姓名排序,冒泡排序
#include <string.h>
#define N 100 //设有名学生
struct student
{
int num;
char name[20];
float math;
float physics;
float english;
float computer;
float sumscore; //总分
float avescore; //平均分
};
struct student student[N];//定义结构体数组
int LENGTH;
typedef struct stu
{
int num1; //学生学号
char name1[20]; //学生姓名
float math1;
float physics1;
float english1;
float computer1;
float sumscore1; //总分
float avescore1; //平均分
struct stu *next;
}linklist_stu;
/***************
函数功能:创建链表
/***************/
linklist_stu *CreateList()
{
int i;
linklist_stu *head,*p,*q;//head指针为链表的头结点,是找到链表的唯一依据,如果head指针丢失,那么整个链表就找不到了;p指针总是指向新申请的结点;q指针总是指向尾节点
linklist_stu temp;//定义结构体别名
FILE *fp;
head=(linklist_stu *)malloc(sizeof(linklist_stu));
head->next=NULL;
p=(linklist_stu *)malloc(sizeof(linklist_stu)); // p指向新开辟的节点内存
//p->next=NULL;
head->next = p; //开辟头结点内存 头结点中没有学生成绩信息
q = p; //开辟尾节点内存 q指针总是指向尾节点
q->next = NULL; // //标志链表的结束尾节点的特点是next成员的值为NULL,它是最后一个节点,作为链表结束的标志,NULL是一个符号常量表示值为的地址
fp=fopen("data\\stuin.txt","rb");
for(i=0;i<LENGTH;i++)
{
p=(struct stu *)malloc(sizeof(struct stu)); // p指向新开辟的节点内存
fscanf(fp,"%d%s%f%f%f%f",&(p->num1),p->name1,&(p->math1),
&(p->physics1),&(p->english1),&(p->computer1)); //将fp内容读入num1--computer1中
q->next=p; //把新节点挂到原尾节点之后
q=q->next; //q指针指向新的尾节点
}
q->next=NULL;//标志链表的结束
p=head->next->next;
fclose(fp);
return head;
}
//表头
void list_head()
{
int i;
printf("+");
for(i=0;i<75;i++)
printf("-");
printf("+\n");
printf("%-6s%-10s%10s%9s%9s%9s%9s%9s","|学号","|姓名","|高等数学","|大学物理","|英语","|计算机","|总分","|平均分|\n");
printf("+");
for(i=0;i<75;i++)
printf("-");
printf("+\n");
printf("\n");
}
//各科目排序函数
void sort_show()
{
int i;
list_head();
for(i=0;i<LENGTH;i++)
printf("%-6d%-10s%10.2f%9.2f%9.2f%9.2f%9.2f%9.2f\n",student[i].num,student[i].name,
student[i].math,student[i].physics,student[i].english,student[i].computer,
student[i].sumscore,student[i].avescore);
}
///////////////////////////////////
//冒泡排序--按姓名
///////////////////////////////////
void name_sort(struct student stu[],int n)
{
int i,j;
struct student temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(stu[j].name,stu[j+1].name)>0)
{
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
}
////////////////////////////////
//选择排序--按总分
////////////////////////////////
void sum_sort(struct student stu[],int n)
{
int i,j,m;
for(i=0;i<n-1;i++)
{
m=i;
for(j=i+1;j<n;j++)
{
if(stu[j].sumscore>stu[m].sumscore)
m=j;
}
if(m!=i)
{
struct student temp=stu[i];
stu[i]=stu[m];
stu[m]=temp;
}
}
}
////////////////////////////////
//快速排序--英语成绩
////////////////////////////////
void english_sort(struct student stu[],int low,int high)
{
if(low<high)
{
int i=low;
int j=high;
struct student x;
x=stu[low];
while(i<j)
{
while(i<j && stu[j].english>x.english)
j--;
if(i<j)
stu[i++]=stu[j];
while(i<j && stu[i].english<=x.english)
i++;
if(i<j)
stu[j--]=stu[i];
}
stu[i]=x;
english_sort(stu,low,i-1);
english_sort(stu,i+1,high);
}
}
///////////////////////////////////
//归并排序--数学成绩
///////////////////////////////////
void merge(struct student stu[], int low, int mid, int high)
{
int i, k;
struct student *tmp = (struct student *)malloc((high-low+1)*sizeof(struct student));
//申请空间,使其大小为两个
int left_low = low;
int left_high = mid;
int right_low = mid + 1;
int right_high = high;
for(k=0; left_low<=left_high && right_low<=right_high; k++){ // 比较两个指针所指向的元素
if(stu[left_low].math>=stu[right_low].math)
{
tmp[k] = stu[left_low++];
}
else
{
tmp[k] = stu[right_low++];
}
}
if(left_low <= left_high)
{ //若第一个序列有剩余,直接复制出来粘到合并序列尾
//memcpy(tmp+k, arr+left_low, (left_high-left_low+l)*sizeof(int));
for(i=left_low;i<=left_high;i++)
tmp[k++] = stu[i];
}
if(right_low <= right_high)
{
//若第二个序列有剩余,直接复制出来粘到合并序列尾
//memcpy(tmp+k, arr+right_low, (right_high-right_low+1)*sizeof(int));
for(i=right_low; i<=right_high; i++)
tmp[k++] = stu[i];
}
for(i=0; i<high-low+1; i++)
stu[low+i] = tmp[i];
free(tmp);
return;
}
void math_sort(struct student stu[],unsigned int first,unsigned int last)
{
int mid = 0;
if(first<last)
{
mid = (first+last)/2; /* 注意防止溢出*/
/*mid = first/2 + last/2;*/
//mid = (first & last) + ((first ^ last) >> 1);
math_sort(stu, first, mid);
math_sort(stu, mid+1,last);
merge(stu,first,mid,last);
}
return;
}
///////////////////////////////////
//直接插入排序--物理成绩
///////////////////////////////////
void physics_sort(struct student *stu,int n) /*直接插入排序*/
{
int i,j;
struct student temp;
for(i=1;i<n;i++)
{
temp = stu[i]; //将要比较的值先绶存起来留出一个空位,方便移动
j = i - 1;
while(j>=0 && stu[j].physics<temp.physics) //比较直到出现比temp大的值,或向前找到头
{
stu[j+1] = stu[j]; //将前面的值往后移
j--;
}
stu[j+1] = temp; //插在a[j]的后面
}
}
///////////////////////////////////
//希尔排序--计算机成绩
///////////////////////////////////
void computer_sort(struct student stu[], int n)
{
int increment;
int i,j;
struct student temp;
for(increment = n/2; increment > 0; increment /= 2) //用来控制步长,最后递减到
{
// i从第step开始排列,应为插入排序的第一个元素
// 可以先不动,从第二个开始排序
for(i = increment; i < n; i++)
{
temp = stu[i];
for(j = i - increment; j >= 0 && temp.computer > stu[j].computer; j -= increment)
{
stu[j + increment] = stu[j];
}
stu[j + increment] = temp; //将第一个位置填上
}
}
}
///////////////////////////////////
//按姓名二分法查找
///////////////////////////////////
void search_name(struct student *stu,int num)
{
int i,j;
struct student temp;
for(i=0;i<num-1;i++)
{
for(j=0;j<num-i-1;j++)
{
if(strcmp(stu[j].name,stu[j+1].name)>0)
{
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
}
int find_name(char *s_name,struct student *st,int num)
{
int low=0;
int high=num-1;
int mid=0;
while(1)
{
mid=(low+high)/2;
if(strcmp(st[mid].name,s_name)==0)
return mid;
else if(strcmp(s_name,st[mid].name)>0)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
}
///////////////////////////////////
//查找前几名的学生
///////////////////////////////////
void top_n(struct student s[],int num)
{
int k,i;
printf("您想看前几名的学生:");
getchar();
scanf("%d",&k);
list_head();
for(i=0;i<k;i++)
{
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n",
s[i].num,
s[i].name,
s[i].math,
s[i].physics,
s[i].english,
s[i].computer,
s[i].sumscore,
s[i].avescore);
}
}
///////////////////////////////////
//查找前几名的学生
///////////////////////////////////
void show_fail(struct student st[],int number)
{
int i;
list_head();
for(i=0;i<number;i++)
{
if(student[i].avescore<60)
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n",
student[i].num,
student[i].name,
student[i].math,
student[i].physics,
student[i].english,
student[i].computer,
student[i].sumscore,
student[i].avescore);
}
}
///////////////////////////////////
//系统运行结束欢迎菜单
///////////////////////////////////
void bye_menu()
{
system("cls");
printf("\n");
printf("\t\t*************************************\n");
printf("\t\t\t 谢谢使用本系统\n");
printf("\n");
printf("\t\t\t\t再见!\n");
printf("\t\t*************************************\n");
}
linklist_student.h //头函数
/////////////////////////////////////////////////////////
//向单链表中插入一个结点
/////////////////////////////////////////////////////////
linklist_stu *insert_node(struct stu *head,int num_)
{
struct stu *p;
struct stu *node;
int n=0;
if(head==NULL)
{
head=node;
node->next=NULL;
n+=1;
return head;
}
node=(struct stu *)malloc(sizeof(struct stu));
scanf("%d%s%f%f%f%f",&(node->num1), (node->name1), &(node->math1), &(node->physics1), &(node->english1), &(node->computer1));
p=head;
while(p->num1!=num_ && p->next!=NULL)
{
p=p->next;
}
if(p->num1==num_)
{
node->next=p->next;
p->next=node;
n+=1;
}
else
{
printf("\t\n没有此结点\n");
}
return head;
}
/////////////////////////////////////////////////////////
//输出单链表的内容
/////////////////////////////////////////////////////////
void Print_List(struct stu *head)
{
struct stu *p;
p=head->next->next; //跳过无数据的头结点
//
int i;
//for(i=0;i<LENGTH;i++)
while(p!=NULL)
{
printf("%-6d%-10s%9.1f%9.1f%9.1f%9.1f", p->num1,p->name1,p->math1,
p->physics1,p->english1,p->computer1);
p->sumscore1=p->math1+p->physics1+p->english1+p->computer1;
p->avescore1=(p->sumscore1)/4;
printf("%9.1f%9.1f\n",p->sumscore1,p->avescore1);
p=p->next;//指向下一个节点
}
}
/////////////////////////////////////////////////////////
//按学号查找某个学生
/////////////////////////////////////////////////////////
int locate_num(linklist_stu *head,int x)
{
linklist_stu *p;
p=head->next->next;
int i=0;
//printf("gggggggggggg");
//printf("%d",p->num1);
//printf("ahhhhhhhhhh");
while(p!=NULL && p->num1!=x)
{
p=p->next;
i++;
}
//printf("aaaaaaaaaaaaaa");
//printf("%d",p->num1);
if(p->num1==x)
{
printf("%-6d%10s%10.2f%10.2f\n",p->num1,p->name1,p->sumscore1,p->avescore1);
return i;
}
else
return 0;
}
chengjiguanli.cpp //主程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "func.h"
#include "linklist_student.h"
//数组中实际的人数,目前有个学生
//函数功能:调用菜用函数,通过输入菜单的序号,控制整个程序的运行
int main()
{
int c; //存放输入的菜单编号
FILE *outfp,*f; //定义文件指针
void menu(); //主菜单
int inputdata();
void sum_average();
void append();
void display();
void count();
void query_num();
void query_score();
void modify_data();
void del_data();
void avg_sort();
void insert_any();
void name_sort(struct student stu[],int);
void sum_sort(struct student stu[],int);
void english_sort(struct student stu[],int,int);
void math_sort(struct student stu[],unsigned int,unsigned int);
void physics_sort(struct student *,int);
void computer_sort(struct student stu[],int);
void search_name(struct student *,int); //按姓名二分法查找某个学生
// void top_three(); //显示前三名的学生
// void show_fail(); //显示不及格的学生
int find_name(char *,struct student *,int);
linklist_stu *CreateList();
void Print_List(struct stu *);
linklist_stu *insert_node(struct stu *,int);
int locate_num(struct stu *,int);
//在磁盘上指定位置创建一个结果数据文件,存放输出的数据
outfp = fopen("data\\stuout.txt", "w");
fclose(outfp);
menu();
while (1)
{
printf("\n\t请选择(0--10):");
scanf("%d", &c);
printf("\n");
if (c>10 || c<0)
printf("------对不起,没有此功能项!------");
switch (c)
{
case 1:
{
int c;
printf("\t***1.磁盘文件输入***\n");
printf("\t***2.追加学生记录***\n\t");
while (1)
{
printf("\n\t请选择(1 or 2):");
scanf("%d", &c);
if (c>2 || c<1)
printf("------对不起,请输入法“”或“”------!");
else
break;
}
switch (c)
{
case 1:
inputdata();
break;
case 2:
inputdata();
append();
break;
}
}break;
case 2:
display();
break;
case 3:
count();
break;
case 4:
{
int c;
printf("\t***1.按学号(顺序)查询***\n");
printf("\t***2.按成绩查询***\n");
printf("\t***3.按姓名(二分)查询***\n");
printf("\t***4.查找前三名***\n");
printf("\t***5.查找平均成绩不及格的学生***\n\t");
while (1)
{
printf("\n\t请选择(1--2):");
scanf("%d", &c);
if (c>5 || c<1)
printf("------对不起,请输入法“”或“”------!");
else
break;
}
switch (c)
{
case 1:
query_num();
break;
case 2:
query_score();
break;
case 3:
search_name(student,LENGTH); //先按姓名排序,二分法查找的是有序表
sort_show();
char sname[20];
int n,nn;
printf("请输入要查找的姓名:");
scanf("%s",sname);
nn=10;
//nn=sizeof(student)/sizeof(student[0]);
n=find_name(sname,student,nn); //按姓名二分法查找
//printf("%d",nn);
//printf("%d",n);
list_head();
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n", student[n].num, student[n].name,
student[n].math, student[n].physics, student[n].english, student[n].computer,
student[n].sumscore, student[n].avescore);
//printf("%s",student[n].name);
break;
case 4:
sum_sort(student,LENGTH);
sort_show();
top_n(student,LENGTH); //查找前几名的学生
break;
case 5:
show_fail(student,LENGTH); //显示所有平均成绩不及格的学生
break;
}
}break;
case 5:
modify_data();
break;
case 6:
del_data();
break;
case 7:
avg_sort();
break;
case 8:
insert_any();
break;
case 9:
{
int c;
printf("\t***1.按姓名冒泡排序***\n");
printf("\t***2.按总分选择排序***\n");
printf("\t***3.按英语成绩快速排序***\n");
printf("\t***4.按数学成绩归并排序***\n");
printf("\t***5.按物理成绩直接插入排序***\n");
printf("\t***6.按计算机成绩希尔排序***\n");
while (1)
{
printf("\n\t请选择(1--6):");
scanf("%d", &c);
if (c>6 || c<1)
printf("------对不起,请输入法“”或“”------!");
else
break;
}
switch (c)
{
case 1:
name_sort(student,LENGTH);
printf("\n\n===按姓名(冒泡)排序===\n\n");
sort_show();
break;
case 2:
sum_sort(student,LENGTH);
printf("\n\n===按总分(选择)排序===\n\n");
sort_show();
//以下是向总分文件sum_sort.txt中保存总分的名次文件
char ch;
printf("\t当前总分排名保存到磁盘文件sum_sort.txt,Y/N:");
getchar();
scanf("%c", &ch);
/*
list_head();
for (i = 0; i<LENGTH; i++)
{
printf("%-6d%-10s%10.2f%9.2f%9.2f%9.2f%9.2f%9.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
*/
if (ch == ‘Y‘ || ch == ‘y‘)
{
//打开文本文件stuin_new.txt
f = fopen("data\\sum_sort.txt", "w");
fprintf(f,"%10s%10s%10s","学号","姓名","总分\n");
fprintf(f,"---------------------------------------------\n");
for (int i = 0; i<LENGTH; i++)
{
fprintf(f,"%10d%10s%10.2f\n", student[i].num, student[i].name,
student[i].sumscore);
}
fclose(f);
printf("\t数据已保存至指定的磁盘文件sum_sort.txt中\n");
}
else
printf("\t您的总分排名未能保存至stuin_new.txt文件中\n");
break;
case 3:
english_sort(student,0,LENGTH-1);
printf("\n\n===按英语(快速)排序===\n\n");
sort_show();
printf("\t当前英语成绩的排名保存到磁盘文件english_sort.txt,Y/N:");
getchar();
scanf("%c", &ch);
if (ch == ‘Y‘ || ch == ‘y‘)
{
//打开文本文件math_sort.txt
f = fopen("data\\english_sort.txt", "w");
fprintf(f,"%10s%10s%10s","学号","姓名","英语\n");
fprintf(f,"---------------------------------------------\n");
for (int i = 0; i<LENGTH; i++)
{
fprintf(f,"%10d%10s%10.2f\n", student[i].num, student[i].name,
student[i].english);
}
fclose(f);
printf("\t数据已保存至指定的磁盘文件math_sort.txt中\n");
}
else
printf("\t您的英语排名未能保存至math_sort.txt文件中\n");
break;
case 4:
math_sort(student,0,LENGTH-1);
printf("\n\n===按数学(归并)排序===\n\n");
sort_show();
printf("\t当前数学成绩的排名保存到磁盘文件math_sort.txt,Y/N:");
getchar();
scanf("%c", &ch);
if (ch == ‘Y‘ || ch == ‘y‘)
{
//打开文本文件math_sort.txt
f = fopen("data\\math_sort.txt", "w");
fprintf(f,"%10s%10s%10s","学号","姓名","数学\n");
fprintf(f,"---------------------------------------------\n");
for (int i = 0; i<LENGTH; i++)
{
fprintf(f,"%10d%10s%10.2f\n", student[i].num, student[i].name,
student[i].math);
}
fclose(f);
printf("\t数据已保存至指定的磁盘文件math_sort.txt中\n");
}
else
printf("\t您的数学排名未能保存至math_sort.txt文件中\n");
break;
case 5:
physics_sort(student,LENGTH);
printf("\n\n===按物理(直接插入)排序===\n\n");
sort_show();
printf("\t当前物理成绩的排名保存到磁盘文件physics_sort.txt,Y/N:");
getchar();
scanf("%c", &ch);
if (ch == ‘Y‘ || ch == ‘y‘)
{
//打开文本文件math_sort.txt
f = fopen("data\\physics_sort.txt", "w");
fprintf(f,"%10s%10s%10s","学号","姓名","物理\n");
fprintf(f,"---------------------------------------------\n");
for (int i = 0; i<LENGTH; i++)
{
fprintf(f,"%10d%10s%10.2f\n", student[i].num, student[i].name,
student[i].physics);
}
fclose(f);
printf("\t数据已保存至指定的磁盘文件physics_sort.txt中\n");
}
else
printf("\t您的物理排名未能保存至physics_sort.txt文件中\n");
break;
case 6:
computer_sort(student,LENGTH);
printf("\n\n===按计算机(希尔)排序===\n\n");
sort_show();
printf("\t当前计算机成绩的排名保存到磁盘文件computer_sort.txt,Y/N:");
getchar();
scanf("%c", &ch);
if (ch == ‘Y‘ || ch == ‘y‘)
{
//打开文本文件math_sort.txt
f = fopen("data\\computer_sort.txt", "w");
fprintf(f,"%10s%10s%10s","学号","姓名","计算机\n");
fprintf(f,"---------------------------------------------\n");
for (int i = 0; i<LENGTH; i++)
{
fprintf(f,"%10d%10s%10.2f\n", student[i].num, student[i].name,
student[i].computer);
}
fclose(f);
printf("\t数据已保存至指定的磁盘文件computer_sort.txt中\n");
}
else
printf("\t您的计算机成绩排名未能保存至computer_sort.txt文件中\n");
break;
}
}
break;
case 10:
{
int c;
printf("\t***1.创建并显示单链表***\n");
printf("\t***2.向单链表中插入一个学生的信息***\n");
printf("\t***3.按学号查询单链表中某个学生的信息***\n");
printf("\t***4.删除单链表中某个学生的信息***\n");
linklist_stu *he;
while (1)
{
printf("\n\t请选择(1--4):");
scanf("%d", &c);
if (c>4 || c<1)
printf("------对不起,请输入法“”或“”------!");
else
break;
}
switch(c)
{
case 1:
he=CreateList();
list_head();
Print_List(he);
break;
case 2:
int number;
printf("\t想要在哪个学号后插入新结点:");
scanf("%d",&number);
insert_node(he,number);
list_head();
Print_List(he);
break;
case 3:
int number1,position;
printf("\t请输入要查找的学号:");
scanf("%d",&number1);
position=locate_num(he,number1);
//printf("aaaaaaaaaaaaaa");
printf("您要查找学生的序号是:%d\n",position+1);
//list_head();
break;
case 4:
break;
}
}
break;
case 0:
bye_menu();
exit(0);
}
}
return 0;
}
//函数功能:主菜单的实现\
void menu()
{
printf("\n\n");
printf("\n***********************************************\n");
printf("\n***********************************************\n");
printf("\n\t\t 欢迎使用本系统\n");
printf("\n");
printf("\t\t\t 学生成绩管理系统\n");
printf("\n");
printf("\t\t\t 1.数据输入\n");
printf("\n");
printf("\t\t\t 2.数据浏览\n");
printf("\n");
printf("\t\t\t 3.数据统计\n");
printf("\n");
printf("\t\t\t 4.数据查询\n");
printf("\n");
printf("\t\t\t 5.数据修改\n");
printf("\n");
printf("\t\t\t 6.数据删除\n");
printf("\n");
printf("\t\t\t 7.按平均成绩排序并存储\n");
printf("\n");
printf("\t\t\t 8.任意位置插入一条记录\n");
printf("\n");
printf("\t\t\t 9.字段的各种排序\n");
printf("\n");
printf("\t\t\t 10.将文本文件转换为单链表\n");
printf("\n");
printf("\t\t\t 0.退出系统\n");
printf("\n");
printf("\n***********************************************\n");
printf("\n***********************************************\n");
}
//函数功能:计算每个学生的总分和平均分
void sum_average()
{
int i;
for (i = 0; i<LENGTH; i++)
{
student[i].sumscore = student[i].math + student[i].physics + student[i].english + student[i].computer;
student[i].avescore = student[i].sumscore / 4;
}
}
//函数功能:从磁盘文件中读取初始学生记录信息送入程序的结构体数组中
int inputdata()
{
int i = 0;
FILE *infp;
if ((infp = fopen("data\\stuin.txt", "r")) == NULL)
{
printf("文件打开失败,不能打开此文本文件!");
return 0;
}
while (!feof(infp)) //文件指针没有指到文件结尾,仍然继续读取学生信息
{
fscanf(infp, "%d%s%f%f%f%f", &student[i].num, student[i].name,
&student[i].math, &student[i].physics, &student[i].english, &student[i].computer);
i++;
}
LENGTH = i;
fclose(infp);
sum_average();
return 0;
}
//函数功能:在数组末尾追回学生记录
void append()
{
char ask;
while (1)
{
//printf(" 请按顺序输入\n 学号\t 姓名\t 高等数学\t 大学物理\t 英语\t 计算机\t ,数据之间用tab键分隔\n");
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
list_head();
scanf("\t%d%s%f%f%f%f", &student[LENGTH].num, student[LENGTH].name,
&student[LENGTH].math, &student[LENGTH].physics, &student[LENGTH].english, &student[LENGTH].computer);
student[LENGTH].sumscore = student[LENGTH].math + student[LENGTH].physics //计算新学生的总分
+ student[LENGTH].english + student[LENGTH].computer;
student[LENGTH].avescore = student[LENGTH].sumscore / 4; //计算新学生的平均分
LENGTH = LENGTH + 1;
printf(" 是否继续追加Y/y?");
getchar(); //////////////缓冲作用
scanf("%c", &ask);
if (!(ask == ‘y‘ || ask == ‘Y‘))
break;
}
}
//函数功能:统计优秀、良好、中等、及格、不及格学生人数
void count()
{
int i;
double excellent, good, medium, pass, fail;
FILE *outfp;
excellent = 0;
good = 0;
medium = 0;
pass = 0;
fail = 0;
for (i = 0; i<LENGTH; i++)
{
if (student[i].avescore >= 90)
excellent++;
else if (student[i].avescore >= 80)
good++;
else if (student[i].avescore >= 70)
medium++;
else if (student[i].avescore >= 60)
pass++;
else
fail++;
}
printf("\t平均成绩各分数段的统计结果如下:\n");
printf("\t平均成绩优秀人数为:%g\t\t,优秀率为:%.1f%%\n", excellent, excellent/LENGTH*100);
printf("\t平均成绩良好人数为:%g\t\t,良好率为:%.1f%%\n", good, good/LENGTH*100);
printf("\t平均成绩中等人数为:%g\t\t,中等率为:%.1f%%\n", medium, medium/LENGTH*100);
printf("\t平均成绩及格人数为:%g\t\t,及格率为:%.1f%%\n", pass, pass/LENGTH*100);
printf("\t平均成绩不及格人数为:%g\t\t,不及格率为:%.1f%%\n", fail, fail/LENGTH*100);
outfp = fopen("data\\stuout.txt", "a");
fprintf(outfp, "\n\t\t平均成绩各分数段的统计结果如下:\n");
fprintf(outfp, "\t平均成绩优秀人数为:%g\t\t,优秀率为:%.1f%%\n", excellent, excellent/LENGTH*100);
fprintf(outfp, "\t平均成绩良好人数为:%g\t\t,良好率为:%.1f%%\n", good, good/LENGTH*100);
fprintf(outfp, "\t平均成绩中等人数为:%g\t\t,中等率为:%.1f%%\n", medium, medium/LENGTH*100);
fprintf(outfp, "\t平均成绩及格人数为:%g\t\t,及格率为:%.1f%%\n", pass, pass/LENGTH*100);
fprintf(outfp, "\t平均成绩不及格人数为:%g\t\t,不及格率为:%.1f%%\n", fail, fail/LENGTH*100);
fclose(outfp);
}
//函数功能:按学号查询学生记录信息
void query_num()
{
int number,i;
printf("请输入要查询的学号,输入-1停止查询\n\t学号:");
scanf("%d",&number);
while(number!=-1)
{
for(i=0;i<LENGTH;i++)
{
if(student[i].num==number)
{
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n",student[i].num,student[i].name,
student[i].math,student[i].physics,student[i].english,student[i].computer,
student[i].sumscore,student[i].avescore);
}
}
printf("\t请输入要查询的学号,输入-1停止查询\n\n\n\n\t学号:");
scanf("%d",&number);
}
}
//函数功能:按成绩查询学生记录信息
void query_score()
{
int i;
FILE *outfp;
outfp = fopen("data\\stuout.txt", "a");
printf("\n\t===各科目平均成绩优秀的学生===\n\n");
for(i=0;i<80;i++)
printf("-");
printf("\n");
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
fprintf(outfp, "\n各科目平均成绩优秀的学生有:");
fprintf(outfp, "\n 学号\t 姓名\t 高等数学\t 大学物理\t 英语\t 计算机\t 总成绩\t 平均成绩\n");
for (i = 0; i<LENGTH; i++)
{
if (student[i].avescore >= 90)
{
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
fprintf(outfp, "%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
}
printf("\n\t各科目平均成绩良好的学生:\n\n");
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
fprintf(outfp, "\n各科目平均成绩良好的学生有:");
fprintf(outfp, "\n 学号\t 姓名\t 高等数学\t 大学物理\t 英语\t 计算机\t 总成绩\t 平均成绩\n");
for (i = 0; i<LENGTH; i++)
{
if (student[i].avescore >= 80 && student[i].avescore < 90)
{
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
fprintf(outfp, "%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
}
printf("\n\t各科目平均成绩中等的学生有:\n");
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
fprintf(outfp, "\n各科目平均成绩中等的学生有:");
fprintf(outfp, "\n 学号\t 姓名\t 高等数学\t 大学物理\t 英语\t 计算机\t 总成绩\t 平均成绩\n");
for (i = 0; i<LENGTH; i++)
{
if (student[i].avescore >= 70 && student[i].avescore < 80)
{
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
fprintf(outfp, "%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
}
printf("\n\t各科目平均成绩及格的学生:\n\n");
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
fprintf(outfp, "\n各科目平均成绩及格的学生有:\n");
fprintf(outfp, "\n 学号\t 姓名\t 高等数学\t 大学物理\t 英语\t 计算机\t 总成绩\t 平均成绩\n");
for (i = 0; i<LENGTH; i++)
{
if (student[i].avescore >= 60 && student[i].avescore < 70)
{
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
fprintf(outfp, "%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
}
printf("\n\t各科目平均成绩不及格的学生:\n\n");
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
fprintf(outfp, "\n各科目平均成绩优秀的学生:\n");
fprintf(outfp, "\n 学号\t 姓名\t 高等数学\t 大学物理\t 英语\t 计算机\t 总成绩\t 平均成绩\n");
for (i = 0; i<LENGTH; i++)
{
if (student[i].avescore<60)
{
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
fprintf(outfp, "%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
}
fclose(outfp);
}
//5.函数功能:按学号修改学生记录信息
void modify_data()
{
int i,number;
printf("\t请输入要修改的学号,按-1停止修改\n\t");
scanf("%d",&number);
while(number!=-1)
{
for(i=0;i<LENGTH;i++)
{
if(student[i].num==number)
{
printf("\t原记录信息如下:\n");
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
printf("%10d%10s%10.2f%10.2f%10.2f%10.2f%10.2f%10.2f\n",student[i].num,student[i].name,
student[i].math,student[i].physics,student[i].english,student[i].computer,
student[i].sumscore,student[i].avescore);
printf("\n\t **请重新输入该学生的信息,数据之间用tab分隔\n\n\t\n");
printf("\t%d",student[i].num);
scanf("%s",student[i].name);
printf("\t");
scanf("%f",&student[i].math);
printf("\t");
scanf("%f",&student[i].physics);
printf("\t");
scanf("%f",&student[i].english);
printf("\t");
scanf("%f",&student[i].computer);
printf("\t");
student[i].sumscore=student[i].math+student[i].physics
+student[i].english+student[i].computer;
printf("\t");
student[i].avescore=student[i].sumscore/4;
}
}
printf("\n\t请输入要修改学生的学号,输入-1停止修改\t\n学号:");
scanf("%d",&number);
}
}
//通过学号返回数组下标
int student_SearchByIndex(int id)
{
int i;
for(i=0;i<LENGTH;i++)
{
if(student[i].num==id)
{
return i;
}
}
return -1;
}
//显示删除的学生信息
void student_DisplaySingle(int show_index)
{
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
printf("%10d%10s%10.2f%10.2f%10.2f%10.2f%10.2f%10.2f\n",
student[show_index].num,
student[show_index].name,
student[show_index].math,
student[show_index].physics,
student[show_index].english,
student[show_index].computer,
student[show_index].sumscore,
student[show_index].avescore);
}
//函数功能:按学号删除文件中的一个学生信息
void del_data()
{
int i;
while(1)
{
FILE *fp_del;
int number;
int index;
printf("请输入要删除的学号:");
scanf("%d",&number);
getchar(); //缓冲
index=student_SearchByIndex(number);
if(index==-1)
{
printf("学号不存在");
}
else
{
printf("\n你要删除的学生信息为:\n");
student_DisplaySingle(index);
printf("真要删除吗?(y/n):");
if(getchar()==‘y‘)
{
//将删除的学生存入del_file
fp_del=fopen("data\\del_file.txt","a");
fprintf(fp_del,"%10d%10s\n",student[index].num,student[index].name);
fclose(fp_del);
for(i=index;i<LENGTH;i++)
{
student[i]=student[i+1];
}
LENGTH--;
}
getchar();
}
printf("是否继续?(y/n):");
if(getchar()==‘n‘)
break;
}
}
//以下是平均成绩堆排序
/*
void swap(struct student *a , struct student *b)
{
struct student temp;
temp=*a;
*a=*b;
*b=temp;
}
*/
void percDown(struct student stu[],int i,int NN)
{
int child;
struct student tmp;
for(;2*i+1<NN;i=child)
{
child=2*i+1;
if(child!=NN-1 && stu[child+1].avescore>stu[child].avescore)
++child;
if(stu[i].avescore<stu[child].avescore)
{
tmp=stu[i];
stu[i]=stu[child];
stu[child]=tmp;
}
else
break;
}
//stu[i]=tmp;
}
//按平均成绩排序并存入文件stuin_new.txt
void avg_sort()
{
FILE *f;
int i;
char ch;
for(i=LENGTH/2;i>=0;--i)
percDown(student,i,LENGTH);
for(i=LENGTH-1;i>0;--i)
{
struct student t;
//swap(&student[0],&student[i]);
t= student[0];
student[0] = student[i];
student[i] = t;
percDown(student,0,i);
}
//以下是冒泡排序
/*
for(i=0;i<LENGTH;i++)
{
for(j=0;j<LENGTH-i-1;j++)
{
if(student[j].avescore>student[j+1].avescore)
{
struct student temp=student[j];
student[j]=student[j+1];
student[j+1]=temp;
}
}
}
*/
printf("\t当前的所有数据信息是否保存到磁盘文件stuin_new.txt,Y/N:");
getchar();
scanf("%c", &ch);
list_head();
for (i = 0; i<LENGTH; i++)
{
printf("%-6d%-10s%10.2f%9.2f%9.2f%9.2f%9.2f%9.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
if (ch == ‘Y‘ || ch == ‘y‘)
{
//打开文本文件stuin_new.txt
f = fopen("data\\stuin_new.txt", "w");
fprintf(f,"%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
fprintf(f,"-----------------------------------------------------------------------------\n");
for (i = 0; i<LENGTH; i++)
{
fprintf(f,"%10d%10s%10.2f%10.2f%10.2f%10.2f%10.2f%10.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
fclose(f);
printf("\t数据已保存至指定的磁盘文件stuin_new.txt中\n");
}
else
printf("\t您的最新数据未能保存至stuin_new.txt文件中\n");
}
//在任意位置插入一个学生的记录
void insert_any()
{
// FILE *ffp;
int i,index,number;
printf("请输入要插入的位置:");
scanf("%d",&number);
for(i=0;i<LENGTH;i++)
{
if(student[i].num==number)
{
index=student_SearchByIndex(student[i].num);
}
}
//printf("%d",index);
for (i=LENGTH-1;i>=index;i--)
{
student[i+1]=student[i];
}
LENGTH++; //增加一个记录,表的总长度加
printf("\n\t请输入要插入的学生信息:\n");
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
//printf("-----------------------------------------------------------------------------\n");
list_head();
scanf("\t%d",&student[index].num);
printf("\t");
scanf("%s",student[index].name);
printf("\t");
scanf("%f",&student[index].math);
printf("\t");
scanf("%f",&student[index].physics);
printf("\t");
scanf("%f",&student[index].english);
printf("\t");
scanf("%f",&student[index].computer);
printf("\t");
student[index].sumscore=student[index].math+student[index].physics
+student[index].english+student[index].computer;
printf("\t");
student[index].avescore=student[index].sumscore/4;
for(i=0;i<LENGTH;i++)
{
printf("\n%10d%10s%10.2f%10.2f%10.2f%10.2f%10.2f%10.2f", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
}
/*
//函数功能:保存最新的学生数据记录到磁盘文件
void save_data()
{
char ch;
int i;
FILE *fp;
printf("\t当前的所有数据信息是否保存到磁盘文件stuin_new.txt,Y/N:");
getchar();
scanf("%c", &ch);
if (ch == ‘Y‘ || ch == ‘y‘)
{
//打开文本文件stuin_new.txt
fp = fopen("data\\stuin_new.txt", "w");
for (i = 0; i<LENGTH; i++)
{
fprintf(fp, "%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
fclose(fp);
printf("\t数据已保存至指定的磁盘文件stuin_new.txt中\n");
}
else
printf("\t您的最新数据未能保存至stuin_new.txt文件中\n");
}
*/
//函数功能:显示所有学生的信息
void display()
{
int i,index;
//printf("%10s%10s%10s%10s%10s%10s%10s%10s","学号","姓名","高等数学","大学物理","英语","计算机","总分","平均分\n");
//for(i=0;i<80;i++)
// printf("-");
//printf("\n");
list_head();
for(i=0;i<LENGTH;i++)
{
if(student[i].num==0)
{
index=student_SearchByIndex(student[i].num);
for (i=index;i<LENGTH-1;i++)
{
student[i]=student[i+1];
}
LENGTH--;
}
}
//int LEN;
for (i = 0; i<LENGTH; i++)
{
printf("%-6d%-10s%10.2f%9.1f%9.1f%9.1f%9.1f%9.1f\n", student[i].num, student[i].name,
student[i].math, student[i].physics, student[i].english, student[i].computer,
student[i].sumscore, student[i].avescore);
}
for(i=0;i<80;i++)
printf("-");
printf("\n");
}
标签:++ 指针 链表 头结点 直接插入 signed 常量 child 查询
原文地址:https://www.cnblogs.com/duanqibo/p/11178105.html