C语言程序输入5学生3门课成绩并输出平均分用结构体指针做
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言程序输入5学生3门课成绩并输出平均分用结构体指针做相关的知识,希望对你有一定的参考价值。
我这儿有一个写好的,改改就可以用.不过个人建议不要这样,我的东西你看看思路就行,主要要自己实现,稍后我给你文件.
切记:尽力自己实现
#ifndef _STULIST_H
#define _STULIST_H
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<string.h>
#include<assert.h>//断言
#include<conio.h>//getch()
#define ADDSTU 1
#define DISPLAYSTU 2
#define UPDATE 3
#define DELSTU 4
#define SORTSCORE 5
#define SORTNUM 6
#define SELECTNAME 7
#define SELECTNUM 8
typedef struct student
long num;
char name[32];
long classnum;
float score_c;
float score_english;
float score_math;
float score_avg;
struct student * next;
nodelist,*pnodelist;
#define LEN sizeof(nodelist)
#define FORMAT "%-7d%-12d%-9s%-8.2f%-11.2f%-8.2f%-7.2f%\\n"
#define DATA p->num,p->classnum,p->name,p->score_c,p->score_english,p->score_math,p->score_avg
void menu(void);
int init_list(pnodelist *p);
void add_node(nodelist* phead,long num,char *s,long classnum,float c,float eng,float math);
void display_list(pnodelist phead);
void add_stu(pnodelist phead);
void destroy_list(pnodelist phead );
void del_node(pnodelist phead, long num );
pnodelist update_stu( pnodelist phead, long num);
void savefile(pnodelist phead);
void loadfile(pnodelist phead);
pnodelist foundmax(pnodelist phead);
pnodelist sortstu_avg(pnodelist phead);
pnodelist foundmin(pnodelist phead );
pnodelist sortstu_num(pnodelist phead);
void select_name(pnodelist phead,pnodelist val);
void select_num(pnodelist phead,pnodelist val);
#endif
//学生管理系统菜单
void menu()/*自定义函数实现菜单功能*/
system("cls");
printf("\\n\\n\\n\\n\\n");
printf("\\t\\t|------------学生管理信息系统------------|\\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|\\t 8. 按学号进行查询 |\\n");
printf("\\t\\t|----------------------------------------|\\n\\n");
printf("请选择您的操作:");
//初始化头指针
int init_list(pnodelist *p)
if (p == NULL)
return 0;
*p = (pnodelist)malloc(LEN);
if( p == NULL )
printf("failed!\\n");
return 0;
(*p)->next = NULL;
return 1;
//添加学生信息
void add_stu(pnodelist phead)//输入学生信息
long num, classnum;
char ch, name[32] = 0;
float c;
float english;
float math;
do
printf("please input: num classnum name c english math\\n");
if( 6 == scanf("%ld %ld %s %f %f %f",&num,&classnum,name,&c,&english,&math) )
add_node(phead,num,name,classnum,c,english,math);
else
printf("error! Input parameters error\\n");
printf("input enter to menu\\n");
while(getchar() != '\\n')continue;//清除错误值
getch();
menu();
return;
while(getchar() != '\\n')continue;
printf("continue? y/n\\n");
ch = getchar();while(ch=='Y '|| ch=='y');
printf("input enter to menu!\\n");
//添加学生信息结点到链表中
void add_node( pnodelist phead, long num, char* s, long classnum, float c, float english, float math )//将学生信息写入文件
pnodelist p = phead;
pnodelist q = NULL;
assert(phead != NULL);
q = (pnodelist)malloc(LEN);
assert(q != NULL);
q->num = num;
q->classnum = classnum;
q->score_c = c;
q->score_english = english;
q->score_math = math;
q->score_avg = (float)( ( q->score_c + q->score_english + q->score_math ) / 3.0 );
strcpy(q->name,s);
q->next = NULL;
while(p->next != NULL)
p = p->next;
p->next = q;
//显示学生信息
void display_list(pnodelist phead)
pnodelist p = NULL;
assert(phead != NULL);
p = phead->next;
if (p == NULL)
printf("no record!\\n");
printf("input enter to menu!\\n");
else
printf("num classnum name c english math avg\\n");
while(p != NULL)
printf(FORMAT,DATA);
p = p->next;
printf("input enter to menu!\\n");
return;
//按学号修改学生信息
pnodelist update_stu( pnodelist phead, long number)
long num, classnum;
char ch, name[32] = 0;
float c;
float english;
float math;
pnodelist p = NULL;
assert(phead != NULL);
p = phead->next;
if (p == NULL)
printf("no record!\\n");
else
while( (p->next != NULL) && (p->num != number) )
p = p->next;
if(p->num != number)
printf("no such num record!\\n");
return 0;
printf("please input: num classnum name c english math\\n");
scanf("%ld %ld %s %f %f %f",&num,&classnum,name,&c,&english,&math);
p->num = num;
p->classnum = classnum;
strcpy(p->name,name);
p->score_c = c;
p->score_english = english;
p->score_math = math;
p->score_avg = (float)( ( p->score_c + p->score_english + p->score_math ) / 3.0 );
return phead;
//清除所有信息
void destroy_list(pnodelist phead )
pnodelist p;
while(phead->next!=NULL)
p = phead->next;
phead->next = phead->next->next;
free(p);
free(phead);
return ;
//删除学号为num的学生信息
void del_node(pnodelist phead, long num )
pnodelist p,q;
assert(phead != NULL);
p=phead;
q=p->next;
//assert( q != NULL );
if ( q == NULL )
printf("no record!\\n");
return ;
while(q!=NULL && q->num!=num )
p=q;
q=q->next;
if(q==NULL)
printf("no such record!\\n");
else
p->next=q->next;
free(q);
printf("delete successfully\\n");
//保存链表信息到文件中
void savefile(pnodelist phead)
FILE *fp = NULL;
pnodelist p = NULL;
assert( phead != NULL);
p = phead->next;
fp = fopen("file.txt","wb");
assert(fp != NULL);
while(p != NULL)
fwrite(p,LEN,1,fp);
p = p->next;
fclose(fp);
//装载文件信息到链表中
void loadfile(pnodelist phead)
FILE* file;
nodelist p;
assert(phead != NULL);
//p = phead;
file = fopen("file.txt","rb");
//assert(file != NULL);
if (file == NULL)
return ;
while(fread(&p,LEN,1,file) == 1)
phead->next = (pnodelist)malloc(LEN);
assert(phead->next != NULL);
*(phead->next) = p;
phead = phead->next;
fclose(file);
//寻找链表中平均成绩最大值
pnodelist foundmax(pnodelist phead )
pnodelist max = NULL;
pnodelist s = NULL;
pnodelist p = NULL;
assert(phead != NULL);
if(phead->next == NULL)
return NULL;
max = phead;
p = phead;
while(p->next != NULL)
if( p->next->score_avg > max->next->score_avg )
max = p;
p = p->next;
s = max->next;
max->next = max->next->next;
return s;
//对平均值进行排序
pnodelist sortstu_avg(pnodelist phead)
pnodelist phead1 = NULL;
pnodelist s = NULL;
pnodelist p = NULL;
assert( init_list(&phead1) != 0 );
p = phead1;
assert(phead != NULL);
while( ( s = foundmax(phead) ) != NULL )
p->next = (pnodelist)malloc(LEN);
*(p->next)= *s;
p = p->next;
p->next = NULL;
savefile(phead1);
printf("sort ok!\\nSuccessfully saved!\\ninput enter to menu!\\n");
return phead1;
//寻找链表中学号最小值
pnodelist foundmin(pnodelist phead )
pnodelist min = NULL;
pnodelist s = NULL;
pnodelist p = NULL;
assert(phead != NULL);
if(phead->next == NULL)
return NULL;
min = phead;
p = phead;
while(p->next != NULL)
if( p->next->num < min->next->num )
min = p;
p = p->next;
s = min->next;
min->next = min->next->next;
return s;
//对学号进行排序
pnodelist sortstu_num(pnodelist phead)
pnodelist phead1 = NULL;
pnodelist s = NULL;
pnodelist p = NULL;
assert( init_list(&phead1) != 0 );
p = phead1;
assert(phead != NULL);
while( ( s = foundmin(phead) ) != NULL )
p->next = (pnodelist)malloc(LEN);
*(p->next)= *s;
p = p->next;
p->next = NULL;
savefile(phead1);
printf("sort ok!\\nSuccessfully saved!\\ninput enter to menu!\\n");
return phead1;
//按姓名进行查询
void select_name(pnodelist phead, pnodelist val )
pnodelist p = NULL;
if(phead->next == NULL)
printf("no record!\\n");
return;
p = phead->next;
while(p != NULL)
if( 0 == strcmp(p->name,val->name) )
printf("num classnum name c english math avg\\n");
printf(FORMAT, DATA);
return;
p = p->next;
if(p == NULL)
printf("no such record!\\n");
return;
//按学号进行查询
void select_num(pnodelist phead, pnodelist val )
pnodelist p = NULL;
if(phead->next == NULL)
printf("no record!\\n");
return;
p = phead->next;
while(p != NULL)
if( p->num == val->num )
printf("num classnum name c english math avg\\n");
printf(FORMAT, DATA);
return;
p = p->next;
if(p == NULL)
printf("no such record!\\n");
return;
追问
好麻烦啊
追答我把基本功能都进行了实现 希望你能好好的理解理解 然后自己动手再实现一次 这样才能真的学到东西
追问好的,谢谢!
参考技术A#include<stdio.h>
typedef struct node
int No;
int c1;
int c2;
int c3;
int sum;
double aver;
node;
int main()
node stu[5],*p=stu;
int i;
for(i=0;i<5;i++)
scanf("%d %d %d",&(p+i)->c1,&(p+i)->c2,&(p+i)->c3);
(p+i)->No=i;
(p+i)->sum=(p+i)->c1+(p+i)->c2+(p+i)->c3;
(p+i)->aver=(p+i)->sum*1.0/3;
printf("学号 课程1 课程2 课程3 平均分\\n");
for(i=0;i<5;i++)
printf("%-6d%-7d%-7d%-7d%.2lf\\n",(p+i)->No,(p+i)->c1,(p+i)->c2,(p+i)->c3,(p+i)->aver);
return 0;
// 解释一个地方:
// #include<stdlib.h>和下面的fflush(stdin);是清除输入缓冲区,因为里面有字符串的输入。
// 正文如下
#include<stdio.h>
#include<stdlib.h>
#define KeNum 3
struct Student
char name[10];
float kecheng[KeNum];
float Ave;
;
void main()
int n=0,stunum=0;
Student stu[5],*p;
for(stunum=0,p=stu;stunum<5;stunum++,p++)
printf("请输入第%d名学生姓名 %d门课成绩,用空格分开,回车结束\n",stunum+1,KeNum);
scanf("%s",p->name);
for(n=0;n<KeNum;n++)
scanf("%f",&(p->kecheng[n]));
fflush(stdin);
for(stunum=0,p=stu;stunum<5;stunum++,p++)
p->Ave=0;
for(n=0;n<KeNum;n++)
p->Ave+=p->kecheng[n];
p->Ave/=KeNum;
for(stunum=0,p=stu;stunum<5;stunum++,p++)
printf("%s:%f %f %f Average:%f\n",p->name,p->kecheng[0],p->kecheng[1],p->kecheng[2],p->Ave);
ZZNUOJ_用C语言编写程序实现1192:删除记录(结构体专题)(附完整源码)
题目描述
有一学生成绩表,包括学号、姓名、3门课程成绩。请实现如下删除功能:输入一个学生的学号,删除该学生的所有信息。
输入
首先输入一个整数n(1<=n<=100),表示学生人数;
然后输入n行,每行包含一个学生的信息:学号(12位)、姓名(不含空格且不超过20位),以及3个整数,表示3门课成绩,数据之间用空格隔开。
最后一行输入一个学号num。
输出
若要删除的学号不存在,则输出“error!”;否则,输出删除该学生后的所有记录。
样例输入
3
541207010188 Zhangling 78 95 55
541207010189 Wangli 87 99 88
541207010190 Fangfang 68 76 75
541207010188
样例输出
541207010189 Wangli 87 99 88
541207010190 Fangfang 68 76 75
完整源码:
以上是关于C语言程序输入5学生3门课成绩并输出平均分用结构体指针做的主要内容,如果未能解决你的问题,请参考以下文章
C语言 建立一个结构体类型,包括了学生姓名和两门课的成绩,用这个类型定义结构体数组,保存多个学生信息
C语言编程,输入10个学生5门课的成绩,分别用函数实现以下功能:
ZZNUOJ_用C语言编写程序实现1180:成绩统计(结构体专题)(附完整源码)
某班有5个学生, 每名学生的数据包括学号,姓名,3门课成绩,从键盘上输入5名学生数据,要求打印出3门课的总平均成绩以及最高分的学生数据(包括学号,姓名,3门课成绩)
C语言试题练习:输入5个学生5门课的成绩,分别用函数求:1.每个学生平均分;2.每门课的平均分;3.找出所有分数中最高分