我直接单独给你写一篇文章:
【C语言】C语言程序设计期末/课程设计/综合实验,学生成绩管理系统/教务管理系统_a辰龙a的博客-CSDN博客
用结构体的可读性多强啊,用数组能搞清楚就不容易了。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<math.h>
#define LEN sizeof(struct student)
struct student {
struct student *next;
long num;
char name[8];
char sex[4];
int year;
int month;
int day;
float c;
float math;
float eng;
float sum;
float average;
};
int n;
//建立基础的学生信息库
struct student * creat() {
struct student * head,*p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("输入学生的学号为0时,停止录入.\n");
printf("请输入学生学号:");
scanf("%ld",&p1->num);
if(p1->num!=0) {
printf("请输入学生姓名:");
scanf("%s",p1->name);
printf("请输入学生性别:");
scanf("%s",p1->sex);
printf("请输入学生生日:\n");
printf("年:");
scanf("%d",&p1->year);
printf("月:");
scanf("%d",&p1->month);
printf("日:");
scanf("%d",&p1->day);
printf("c语言:");
scanf("%f",&p1->c);
printf("高数:");
scanf("%f",&p1->math);
printf("英语:");
scanf("%f",&p1->eng);
p1->sum=p1->c+p1->eng+p1->math;
p1->average=p1->sum/3;
printf("\n");
}
while(p1->num!=0) {
n=n+1;
if(n==1) {
head=p1;
} else {
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("请输入学生学号:");
scanf("%ld",&p1->num);
if(p1->num!=0) {
printf("请输入学生姓名:");
scanf("%s",p1->name);
printf("请输入学生性别:");
scanf("%s",p1->sex);
printf("请输入学生生日:\n");
printf("年:");
scanf("%d",&p1->year);
printf("月:");
scanf("%d",&p1->month);
printf("日:");
scanf("%d",&p1->day);
printf("c语言:");
scanf("%f",&p1->c);
printf("高数:");
scanf("%f",&p1->math);
printf("英语:");
scanf("%f",&p1->eng);
p1->sum=p1->c+p1->eng+p1->math;
p1->average=p1->sum/3;
printf("\n");
}
}
p2->next=NULL;
return head;
}
//删除学生信息
struct student * del(struct student *head,long num) {
struct student *p1,*p2;
if(head==NULL) {
printf("\nlist null!\n");
return head;
}
p1=head;
while(num!=p1->num&&p1->next!=NULL) {
p2=p1;
p1=p1->next;
}
if(num==p1->num) {
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("你删除的学生信息为:\n");
printf("学号:%ld\n",p1->num);
printf("姓名:%s\n",p1->name);
printf("性别:%s\n",p1->sex);
printf("生日:\n");
printf("年:%d\n",p1->year);
printf("月:%d\n",p1->month);
printf("日:%d\n",p1->day);
printf("c语言:%5.2f\n",p1->c);
printf("高数:%5.2f\n",p1->math);
printf("英语:%5.2f\n",p1->eng);
p1->sum=p1->c+p1->math+p1->eng;
printf("总分:%5.2f\n",p1->sum);
printf("平均分:%5.2f\n",p1->average);
printf("\n");
n=n-1;
} else
printf("输入有误!\n");
return head;
}
//添加学生信息
struct student * insert (struct student *head,struct student *stud) {
struct student * p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL) {
head=p0;
p0->next=NULL;
} else
while((p0->num>p1->num)&&(p1->next!=NULL)) {
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num) {
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
} else {
p1->next=p0;
p0->next=NULL;
}
n=n+1;
return head;
}
//总分排序
int sortsum(struct student *head) {
struct student *p, *q, *max;
long temp1;
char temp2[4],temp9[4];
int temp3,temp4,temp5;
float temp,temp6,temp7,temp8,temp10;
p = head;
while (p != NULL) {
max = p;
q = p->next;
while (q != NULL) {
if (max->sum < q->sum)
max = q;
q = q->next;
}
// 交换其他值
if (max != p) {
temp = max->sum;
max->sum = p->sum;
p->sum = temp;
temp1=max->num;
max->num=p->num;
p->num=temp1;
strcpy(temp2,max->name);
strcpy(max->name,p->name);
strcpy(p->name,temp2);
temp3=max->year;
max->year=p->year;
p->year=temp3;
temp4=max->month;
max->month=p->month;
p->month=temp4;
temp5=max->day;
max->day=p->day;
p->day=temp5;
temp6=max->c;
max->c=p->c;
p->c=temp6;
temp7=max->math;
max->math=p->math;
p->math=temp7;
temp8=max->eng;
max->eng=p->eng;
p->eng=temp8;
strcpy(temp9,max->sex);
strcpy(max->sex,p->sex);
strcpy(p->sex,temp9);
temp10=max->average;
max->average=p->average;
p->average=temp10;
}
p = p->next;
}
printf("排序以后的学生信息为:\n");
struct student *p1;
p1 = head;
int count=1;
while(p1!=NULL) {
printf("第%d名:\n",count);
printf("学号:%ld ",p1->num);
printf("姓名:%s ",p1->name);
printf("生日:");
printf("%d.",p1->year);
printf("%d.",p1->month);
printf("%d ",p1->day);
printf("c语言:%5.3f ",p1->c);
printf("高数:%5.3f ",p1->math);
printf("英语:%5.3f ",p1->eng);
printf("总分:%4.2f ",p1->sum);
printf("\n");
count++;
p1=p1->next;
}
return 0;
}
//平均分排序
int sortaverage(struct student *head) {
struct student *p, *q, *max;
long temp1;
char temp2[4],temp9[4];
int temp3,temp4,temp5;
float temp,temp6,temp7,temp8;
p = head;
while (p != NULL) {
max = p;
q = p->next;
while (q != NULL) {
if (max->average < q->average)
max = q;
q = q->next;
}
// 交换值
if (max != p) {
temp = max->average;
max->average = p->average;
p->average = temp;
temp1=max->num;
max->num=p->num;
p->num=temp1;
strcpy(temp2,max->name);
strcpy(max->name,p->name);
strcpy(p->name,temp2);
temp3=max->year;
max->year=p->year;
p->year=temp3;
temp4=max->month;
max->month=p->month;
p->month=temp4;
temp5=max->day;
max->day=p->day;
p->day=temp5;
temp6=max->c;
max->c=p->c;
p->c=temp6;
temp7=max->math;
max->math=p->math;
p->math=temp7;
temp8=max->eng;
max->eng=p->eng;
p->eng=temp8;
strcpy(temp9,max->sex);
strcpy(max->sex,p->sex);
strcpy(p->sex,temp9);
}
p = p->next;
}
printf("排序以后的学生信息为:\n");
struct student *p1;
p1 = head;
int count=1;
while(p1!=NULL) {
printf("第%d名:\n",count);
printf("学号:%ld ",p1->num);
printf("姓名:%s ",p1->name);
printf("生日:");
printf("%d.",p1->year);
printf("%d.",p1->month);
printf("%d ",p1->day);
printf("c语言:%5.3f ",p1->c);
printf("高数:%5.3f ",p1->math);
printf("英语:%5.3f ",p1->eng);
printf("总分:%4.2f ",p1->sum);
printf("平均分:%4.2f ",p1->average);
printf("\n");
count++;
p1=p1->next;
}
return 0;
}
//求班级平均数
float ave(struct student * head) {
int i;
float ave,sum=0;
struct student *p;
p=head;
for(i=0; p!=NULL; i++) {
sum=sum+p->sum;
p=p->next;
}
ave=sum/i;
return ave;
}
//修改学生数据
int change(struct student *head,long num) {
struct student *p;
p=head;
for(; p!=NULL;) {
if(p->num==num) {
printf("请输入学生姓名:");
scanf("%s",p->name);
printf("请输入学生性别:");
scanf("%s",p->sex);
printf("请输入学生生日:\n");
printf("年:");
scanf("%d",&p->year);
printf("月:");
scanf("%d",&p->month);
printf("日:");
scanf("%d",&p->day);
printf("c语言:");
scanf("%f",&p->c);
printf("高数:");
scanf("%f",&p->math);
printf("英语:");
scanf("%f",&p->eng);
p->sum=p->c+p->eng+p->math;
p->average=p->sum/3;
printf("\n");
break;
} else {
p=p->next;
}
}
return 0;
}
//保存到文件中
int filein(struct student *head) {
FILE *fp;
struct student *p;
//打开文件
if((fp=fopen("student.dat","w"))==NULL) {
printf("can't open.\n");
exit(0);
}
p=head;
//将链表的内容存储到文本文件中
while(p!=NULL) {
fwrite(p,LEN,1,fp);
printf("\n");
p=p->next;
}
fclose(fp);
printf("成功保存\n");
return 0;
}
struct student *fileout(struct student *head) {
FILE *fp;
struct student *p,*s;
if((fp=fopen("student.dat","r"))==NULL)
return NULL;
else if(fgetc(fp)==EOF)
return NULL;
rewind(fp);
head=(struct student *)malloc(LEN);
fread(head,LEN,1,fp);
p=head;
while(!feof(fp)) {
s=(struct student *)malloc(LEN);
if(fread(s,LEN,1,fp)==0)
break;
p->next=s;
p=s;
p->next=NULL;
}
return head;
fclose(fp);
}
//按学号查找
struct student * locate(struct student *head,long num1) {
struct student *p1,*p2;
p1=head;
if(head==NULL) { //空链表时返回
printf("/n链表为空!/n");
return(head);
} else {
while(num1!=p1->num && p1->next!=NULL) {
p2=p1;
p1=p1->next;
}
if(p1->num==num1) { //比较输入学号是否与链表中学生学号匹配
printf("查找的学生信息为:\n");
printf("学号:%ld\n",p1->num);
printf("姓名:%s\n",p1->name);
printf("性别:%s\n",p1->sex);
printf("生日:\n");
printf("年:%d\n",p1->year);
printf("月:%d\n",p1->month);
printf("日:%d\n",p1->day);
printf("c语言:%5.3f\n",p1->c);
printf("高数:%5.3f\n",p1->math);
printf("英语:%5.3f\n",p1->eng);
p1->sum=p1->c+p1->eng+p1->math;
printf("总分:%5.2f\n",p1->sum);
printf("平均分:%5.2f\n",p1->average);
printf("\n");
return head;
} else {
printf("无该学生数据\n");
return head;
}
}
}
//按姓名查找
struct student * locate1(struct student *head) {
char name[10];
printf("请输入要查询学生的姓名:");
scanf("%s",name);
struct student *p1,*p2;
p1=head;
if(head==NULL) { //空链表时返回
printf("/n链表为空!/n");
return(head);
} else {
while(strcmp(name,p1->name)!=0 && p1->next!=NULL) {
p2=p1;
p1=p1->next;
}
if(strcmp(name,p1->name)==0) { //比较输入姓名与链表中学生姓名是否匹配
printf("查找的学生信息为:\n");
printf("学号:%ld\n",p1->num);
printf("姓名:%s\n",p1->name);
printf("性别:%s\n",p1->sex);
printf("生日:\n");
printf("年:%d\n",p1->year);
printf("月:%d\n",p1->month);
printf("日:%d\n",p1->day);
printf("c语言:%5.3f\n",p1->c);
printf("高数:%5.3f\n",p1->math);
printf("英语:%5.3f\n",p1->eng);
p1->sum=p1->c+p1->eng+p1->math;
printf("总分:%5.2f\n",p1->sum);
printf("平均分:%5.2f\n",p1->average);
printf("\n");
return head;
} else {
printf("无该学生数据\n");
return head;
}
}
}
//输出学生信息
int print(struct student *head) {
struct student *p;
p=head;
printf("当前录入的所有学生的信息为:\n");
while(p!=NULL) {
printf("学号:%ld ",p->num);
printf("姓名:%s ",p->name);
printf("性别:%s ",p->sex);
printf("生日:%d.",p->year);
printf("%d.",p->month);
printf("%d ",p->day);
printf("c语言:%5.2f ",p->c);
printf("高数:%5.2f ",p->math);
printf("英语:%5.2f ",p->eng);
printf("总分:%5.2f ",p->sum);
printf("平均分:%5.2f",p->average);
printf("\n");
p=p->next;
}
printf("\n");
return 0;
}
//显示输出某门课程60分以下、60~79、80~89、90分以上各分数段的学生信息
int C(struct student *head) {
int i=0,j=0,k=0,l=0,a;
struct student *p,*p1;
p=head;
while(p!=NULL) {
if(p->c<60) {
i++;
}
if(p->c>=60&&p->c<80) {
j++;
}
if(p->c>=80&&p->c<90) {
k++;
}
if(p->c>=90) {
l++;
}
p=p->next;
}
p1=head;
if(i>0) {
printf("C语言60分以下有%d人\n",i);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->c<60) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
p1=head;
if(j>0) {
printf("C语言60~79分有%d人\n",j);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->c>=60&&p1->c<80) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
p1=head;
if(k>0) {
printf("C语言80~89分有%d人\n",k);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->c>=80&&p1->c<90) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
p1=head;
if(l>0) {
printf("C语言90分以上有%d人\n",l);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->c>=90) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
}
printf("\n");
return 0;
}
int MATH(struct student *head) {
int i=0,j=0,k=0,l=0,a;
struct student *p,*p1;
p=head;
while(p!=NULL) {
if(p->math<60) {
i++;
}
if(p->math>=60&&p->math<80) {
j++;
}
if(p->math>=80&&p->math<90) {
k++;
}
if(p->math>=90) {
l++;
}
p=p->next;
}
p1=head;
if(i>0) {
printf("数学60分以下有%d人\n",i);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->math<60) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
p1=head;
if(j>0) {
printf("数学60~79分有%d人\n",j);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->math>=60&&p1->math<80) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
p1=head;
if(k>0) {
printf("数学80~89分有%d人\n",k);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->math>=80&&p1->math<90) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
p1=head;
if(l>0) {
printf("数学90分以上有%d人\n",l);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->math>=90) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
}
printf("\n");
return 0;
}
int ENGLISH(struct student *head) {
int i=0,j=0,k=0,l=0,a;
struct student *p,*p1;
p=head;
while(p!=NULL) {
if(p->eng<60) {
i++;
}
if(p->eng>=60&&p->eng<80) {
j++;
}
if(p->eng>=80&&p->eng<90) {
k++;
}
if(p->eng>=90) {
l++;
}
p=p->next;
}
p1=head;
if(i>0) {
printf("英语60分以下有%d人\n",i);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->eng<60) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
p1=head;
if(j>0) {
printf("英语60~79分有%d人\n",j);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->eng>=60&&p1->eng<80) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
p1=head;
if(k>0) {
printf("英语80~89分有%d人\n",k);
printf("学号,姓名如下\n");
while(p1!=NULL) {
if(p1->eng>=80&&p1->eng<90) {
printf("学号:%ld ",p1->num);
printf("姓名:%s\n",p1->name);
p1=p1->next;
}
else p1=p1->next;
}
printf("\n");
}
https://www.bilibili.com/read/cv11853066/
可以借鉴下
#include<stdio.h>
void input(int a[10][6], int n);
float avg1(int a[10][6], int n);
float avg2(int a[10][6], int n);
void max(int a[10][6]);
int main()
{
int a[10][6];
int i;
for (i = 0; i < 10; i++)
{
printf("请输入第%d个学生的6门成绩:", i + 1);
input(a, i);
}
for (i = 0; i < 10; i++)
{
printf("第%d个学生的平均分为%f\n", i + 1, avg1(a, i));
}
for (i = 0; i < 6; i++)
{
printf("第%d门课的平均分为%f\n", i + 1, avg2(a, i));
}
max(a);
return 0;
}
void input(int a[10][6], int n)
{
int i;
for (i = 0; i < 6; i++)
{
scanf("%d", &a[n][i]);
}
}
float avg1(int a[10][6], int n)
{
int i, sum = 0;
for (i = 0; i < 6; i++)
{
sum = sum + a[n][i];
}
return sum * 1.0 / 6;
}
float avg2(int a[10][6], int n)
{
int i, sum = 0;
for (i = 0; i < 10; i++)
{
sum = sum + a[i][n];
}
return sum * 1.0 / 10;
}
void max(int a[10][6])
{
int i, j, m, n;
int max = a[0][0];
for (i = 0; i < 10; i++)
{
for (j = 0; j < 3; j++)
{
if (max < a[i][j])
{
max = a[i][j];
m = i;
n = j;
}
}
}
printf("60个分数中最高分为%d,为第%d个学生的第%d门课\n", max, m + 1, n + 1);
}
不知道你这个问题是否已经解决, 如果还没有解决的话:#include<stdio.h>
//x + 1;//不带副作用
//x++;//带有副作用
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main()
{
int x = 5;
int y = 8;
int z = MAX(x++, y++);
printf("x=%d y=%d z=%d\n", x, y, z);//输出的结果是什么?
return 0;
}
需要设计函数的类型操作包括但不限于:数据处理、排序、查找、文件读写、网络通信等等。
在用归并排序做排序时,可能遇到的问题包括但不限于:代码实现复杂、代码执行效率低下、内存占用大等等。解决上述问题的方案包括但不限于:借鉴优秀的归并排序实现代码、通过多线程/并行计算优化代码执行效率、优化代码实现逻辑,减少内存占用等等。
要进行优化的操作包括但不限于:代码执行效率低下、代码占用内存过大、功能实现不完整、交互不够友好等等。优化方案包括但不限于:通过调整代码逻辑、优化算法/数据结构、使用更高效的编程语言、减少内存占用等等。
替换结构体为数组的原因可能包括但不限于:使用数组可以更高效地存储结构体数据、数组更容易进行增删改查操作、数组占用的内存更小等等。在代码中,结构体可能扮演着存储重要数据的角色,通过数组替换结构体可以更高效地存储和操作这些数据。实现代码可能如下:
旧代码(使用结构体):
typedef struct {
int id;
char name[256];
} Person;
Person people[100];
// 其他操作
替换为新代码(使用数组):
int personIds[100];
char personNames[100][256];
// 其他操作
结构体主要用于组织和存储复杂的数据对象,而数组则主要用于处理相同类型的数据的集合,所以不建议直接用数组替代结构体
结构体和数组两者的功能并不完全相同,所以最好不要替代