代码可以实现的功能:
(1)直接从终端键盘输入信息
(2)从磁盘文件录入学生信息
(3)可以查询学生消费信息
(4)可以删除学生信息
(5)可以添加学生的消费信息,并显示添加后学生的人数
(6)显示录入的学生信息
(7) 把录入的学生信息保存到指定的文件中
(喜欢的小伙伴记得分享给身边的小伙伴哦!点点赞,谢谢啦!)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct scorenode)
#define DEBUG
struct scorenode //定义结构体
{
int number;/*学号*/
char name[10];/*姓名*/
int xiaofei;/*消费情况*/
struct scorenode *next;
};
typedef struct scorenode score;
int n,k;/*n,k为全局变量,本程序中的函数均可*p3以使用它*/
void menu();
score *creat(void);
score *load(score *head);
score *search(score *head);
score *del(score *head);
score *add(score *head,score *stu);
void print(score *head);
save(score *p1);
/*==============================================================================================*/
/*=========================创建链表,此函数带回一个指向链表头的指针=============================*/
score *creat(void)
{
score *head;
score *p1,*p2,*p3,*max;
int i,j;
char t[10];
n=0;
p1=p2=p3=(score *)malloc(LEN);/*head=p3; 开辟一个新单元*/
printf("请输入学生信息!(以0结束)\n");
repeat1: printf("请输入学生编号(编号>0):");/*输入学号,学号应大于0*/
scanf("%d",&p1->number);
while(p1->number<0)
{
getchar();
printf("错误,请重新输入号码:");
scanf("%d",&p1->number);
}
/*输入学号为字符或小于0时,程序报错,提示重新输入学号*/
if(p1->number==0)
goto end;/*当输入的学号为0时,转到末尾,结束创建链表*/
else
{
p3=head;
if(n>0)
{
for(i=0;i<n;i++)
{
if(p1->number!=p3->number)
p3=p3->next;
else
{
printf("number repeate,please input again!\n");
goto repeat1;
/*当输入的学号已经存在,程序报错,返回前面重新输入*/
}
}
}
}
printf("请输入学生姓名");
scanf("%s",&p1->name);/*输入学生姓名*/
printf("请输入消费情况");/*输入消费情况;*/
scanf("%d",&p1->xiaofei);
while(p1->number!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(score *)malloc(LEN);
printf("请输入学生信息(以0结束)\n");
repeat2:printf("请输入学号(学号应大于0):");
scanf("%d",&p1->number);/*输入学号,学号应大于0*/
while(p1->number<0)
{
getchar();
printf("请重新输入学号:");
scanf("%d",&p1->number);}
/*输入学号为字符或小于0时,程序报错,提示重新输入学号*/
if(p1->number==0)
goto end;/*当输入的学号为0时,转到末尾,结束创建链表*/
else
{
p3=head;
if(n>0)
{
for(i=0;i<n