听说最近学生管理系统火热!
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> #define ForMat1 "%d%s%lf%d%d" #define ForMat2 "%-4d%-10s%-12.0lf%-4d%-4d%-4.2f" struct student { int order; char name[10]; double StuNum; int Sub[2]; float Aver; struct student *next; }; struct student *stu,*p,*head; struct student *GetDate()//录入学生的信息、动态分配内存、以0作为结束标志 { stu=(struct student *)malloc(sizeof(struct student)); head=stu; p=stu; p->next=stu; scanf(ForMat1,&stu->order,stu->name,&stu->StuNum,&stu->Sub[0],&stu->Sub[1]); while(p->next!=NULL) { if(p->order!=0)//问题一:此处有点困难就是还要完整输入第三个数据才能结束整个循环想问下怎么样简洁些 { stu=(struct student *)malloc(sizeof(struct student));//动态分配内存 scanf(ForMat1,&stu->order,stu->name,&stu->StuNum,&stu->Sub[0],&stu->Sub[1]); p->next=stu; p=stu; } else p->next=NULL ; } return head; } void Graphic()//输出图像g供选择 { printf("******************************\n"); printf("* ord(0) || nam(1) || StN(2) *\n"); printf("*----------------------------*\n"); printf("* mth(3) || eng(4) || Ave(5) *\n"); printf("******************************\n"); } void Change(struct student *stu)//改变相应的值 { int i,t; char a[10]={0}; float k=0; printf("please input the students' order you want to change i from 0 to the number you put\n"); scanf("%d",&i); printf("please choose the number you want to change\n"); Graphic(); scanf("%d",&t); if(t==0) { printf("the primary date is %d\n",stu[i].order); printf("please input the new date\n"); scanf("%f",&k); stu[i].order=(int)k; k=0; } if(t==1) { printf("the primary date is %s\n",stu[i].name); printf("please input the new name\n"); gets(a); strcpy(stu[i].name,a); stu[i].name[strlen(a)]='\0'; } if(t==2) { printf("the primary date is %f\n",stu[i].StuNum); printf("please input the new student number\n"); scanf("%lf",&k); stu[i].StuNum=k; k=0; } if(t==3) { printf("the primary score of math is %d\n",stu[i].Sub[0]); printf("please input the new score\n"); scanf("%f",&k); stu[i].Sub[0]=(int)k; k=0; } if(t==4) { printf("the primary score of english is %d\n",stu[i].Sub[1]); printf("please input the new score\n"); scanf("%f",&k); stu[i].Sub[1]=(int)k; k=0; } if(t==5) { printf("the date haven't the necessary to change\n"); } } void Ave(struct student *p1)//求每个学生的平均分 { struct student *p2; int i,sum=0; do//问题二:此处倒数第一个数据不能求其平均值求解答 { for(i=0;i<2;i++) sum=sum+p1->Sub[i]; p1->Aver=sum/2; p2=p1; p1=p2->next; }while(p2->next!=NULL); } void Print(struct student *p1)//屏幕输出函数 { struct student *p2; Ave(p1); while(p1->next!=NULL) { printf(ForMat2,p1->order,p1->name,p1->StuNum,p1->Sub[0],p1->Sub[1],p1->Aver); printf("\n"); p2=p1; p1=p2->next; } } void Save(struct student *PS)//建立一个文件名为computerclass105的文件并保存在F盘根目录下 { FILE *PF; struct student *AssitP; if((PF=fopen("F:\ComputerClass105.txt","w")==NULL)) { printf("can't open this file\n"); exit(0); } do { fwrite(PS,sizeof(struct student),1,PF);//问题三:此处文件虽然能够建立但是数据传递不过去 难道结构体里的数据不能传递吗 还是别的 我试过单个数据传递还是不行 求解 fprintf(PF,"\n"); AssitP=PS; PS=AssitP->next; }while(PS!=NULL); fclose(PF); } int main() { struct student *p; printf("plese input the date when meet zero is over\n"); p=GetDate(); Change(p); printf("following is the information\n"); Print(p); Save(p); return 0; }我闲着无聊 做了论坛某个同学问的题目 但是错误到是不少 求指教