展示
文档文件
功能实现和通讯录小系统项目相似,实现序号、名字、性别、年龄以及语数英3课的成绩。录入成绩会不断执行,当序号输入为0时结束录入学生成绩并返回到初始选择的页面。
代码
主函数main.c
#include "student.h"
int menu_select();
int main(int argc, const char *argv[])
{
link l;
l = (link)malloc(sizeof(NODE));
if(!l)
{
printf("\n malloc memory failure!\n");
return -1;
}
l->next = NULL;
while(1)
{
int tmp = menu_select();
switch(tmp)
{
case 1:
input(l);
break;
case 2:
del(l);
break;
case 3:
list(l);
break;
case 4:
search(l);
break;
case 5:
save(l);
break;
case 6:
load(l);
break;
case 7:
exit(0);
//defult :
// break;
}
}
return 0;
}
int menu_select()
{
int i;
printf("\n\n\t************student list***********\n");
printf("\t|* 1、input record *|\n");
printf("\t|* 2、delete record *|\n");
printf("\t|* 3、list record *|\n");
printf("\t|* 4、search record *|\n");
printf("\t|* 5、save record *|\n");
printf("\t|* 6、load record *|\n");
printf("\t|* 7、quit *|\n");
printf("\t***********************************\n\n\n");
do
{
printf("\n\t input your choice:");
scanf("%d",&i);
}while(i <= 0 || i > 7);
return i;
}
student.h文件
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <stdio.h>
#include <stdlib.h>
struct student
{
int num; /*学号*/
char name[15]; /*姓名*/
char sex[2]; /*性别*/
int age; /*年龄*/
double score[3]; /*三门课的分数*/
double sum; /*总分*/
double ave; /*平均分*/
};
typedef struct node
{
struct student data;
struct node *next;
}NODE,*link;
void input(link l);
void save(link l);
void load(link l);
void del(link l);
void list(link l);
void display(NODE *p);
void search(link l);
#endif
student.c文件
#include "student.h"
void input(link l)
{
//int i;
link p,q = l;
while(1)
{
p = (NODE *)malloc(sizeof(NODE));
if(!p)
{
printf("memory malloc failure\n");
return ;
}
printf("\tinput number:");
scanf("%d",&p->data.num);
if(p->data.num == 0)
break;
for(;q->next!= NULL;q = q->next)
{
if(q->data.num == p->data.num)
{
printf("\tthe number has exited,please input again:");
scanf("%d",&p->data.num);
}
}
printf("\tinput name:");
scanf("%s",p->data.name);
printf("\tinput sex w/m:");
scanf("%s",p->data.sex);
printf("\tinput age:");
scanf("%d",&p->data.age);
printf("\tplease input Chinese Math English score:\n");
int i;
for(i = 0;i < 3; i++)
{
printf("\t");
scanf("%lf",&p->data.score[i]);
}
p->data.sum = p->data.score[0] + p->data.score[1] + p->data.score[2];
p->data.ave = p->data.sum / 3;
q->next = NULL;
q->next = p;
p = q;
}
return;
}
void del(link l)
{
int num;
NODE *p,*q = l;
p = q->next;
printf("\tplease input the student number you want to delete:");
scanf("%d",&num);
while(p)
{
if(num == p->data.num)
{
q->next = p->next;
free(p);
printf("\tdelete sucessfuly!\n");
break;
}
else
{
q = p;
p = q->next;
}
}
if(NULL == p)
printf("can not find the student you want to delete!");
return;
}
void list(link l)
{
NODE *p = l->next;
if(NULL == p)
{
printf("no student record\n");
}
while(p != NULL)
{
display(p);
p = p->next;
}
return;
}
void display(NODE *p)
{
printf("\tstudent information\n");
printf("\tnumber: %d\n",p->data.num);
printf("\tname: %s\n",p->data.name);
printf("\tsex: %s\n",p->data.sex);
printf("\tChinese: %lf\n",p->data.score[0]);
printf("\tMath: %lf\n",p->data.score[1]);
printf("\tEnglist: %lf\n",p->data.score[2]);
printf("\tsum: %lf\n",p->data.sum);
printf("\tave: %lf\n",p->data.ave);
}
void search(link l)
{
int num;
NODE *p = l->next;
printf("\tinput number of the student:");
scanf("%d",&num);
while(p)
{
if(p->data.num == num)
{
display(p);
break;
}
p = p->next;
}
if(NULL == p)
{
printf("\tnot search\n");
}
return;
}
void save(link l)
{
NODE *p = l->next;
FILE *fp;
fp = fopen("studentlist.txt","w");
if(!fp)
{
perror("can not open file");
exit(1);
}
printf("\n\tsaving file\n");
while(p)
{
fwrite(p,sizeof(NODE),1,fp);
p = p->next;
}
fclose(fp);
return;
}
void load(link l)
{
NODE *p,*r = l;
FILE *fp = NULL;
fp = fopen("studentlist.txt","r");
if(!fp)
{
perror("can not open file");
exit(1);
}
printf("\n\tloading file\n");
while(!feof(fp))
{
p = (NODE *)malloc(sizeof(NODE));
if(!p)
{
printf("memory malloc failure\n");
return ;
}
if(fread(p,sizeof(NODE),1,fp) != 1)
break;
else
{
p->next = NULL;
r->next = p;
r = p;
}
}
fclose(fp);
return;
}