要求:实现成绩的录入与浏览,并保存为二进制文件
实现算法:采用结构体链表实现数据的插入、遍历,利用fopen()、fwrite()、fread()等函数来实现文件保存。
main.c
#include <stdio.h>
#include "interface.h"
#include "insert_point.h"
#include <stdlib.h>
#include "list.h"
#include <unistd.h>
#include "file.h"
int main(void)
{
STU head=NULL;
//1,从保存指针的二进制文件中读出链表,并返回头节点
head = read_from_file(head);
int num = 0;
while(1)
{
//2,界面显示,简单的printf实现
/*
Student Manage System
1,...
2,...
3,...
*/
main_face();
while(scanf("%d",&num) != 1)
{
printf("请输入数字选项\n");
//3,输入的不是数据,就清空缓存区
while(getchar()!='\n');
}
if(num>=1 && num<=3)
{
switch(num)
{
//4, 1选项是录入成绩
case 1:
head = insert_points(head);
write_to_file(head);
break;
//5, 2选项是浏览成绩
case 2:
show_all_student(head);
break;
//6, 3选项是退出系统
case 3:
fprintf(stderr,"\t拜拜\n");
exit(0);
}
}
else
printf("请输入下面三个选项\n");
}
return 0;
}
interface.c 实现主界面功能
#include "interface.h"
void main_face(void)
{
printf("\t\t\tStudent Manage System\n");
printf("\n");
printf("\t\t\t 1,录入成绩\n");
printf("\t\t\t 2,浏览成绩\n");
printf("\t\t\t 3,退出系统\n");
printf("\n");
printf("\t请输入选项 : ");
}
interface.h
#include <stdio.h>
#ifndef __INTERFACE_H__
#define __INTERFACE_H__
void main_face(void);
#endif
insert_point.c
#include "insert_point.h"
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
//1, 封装一个分配堆内存的函数
static STU get_memory(void)
{
STU student=malloc(sizeof(struct point));
return student;
}
//2, 写入数据到节点内
static void input_point(STU student)
{
printf("请输入学生姓名\n");
scanf("%s",student->name);
printf("请输入其成绩\n");
scanf("%f",&student->point);
}
//3, 把新节点采用头插法插入链表
STU add_to_head(STU head,STU student)
{
student->next = head;
head = student;
return head;
}
STU add_student_point(STU head)
{
STU student = NULL;
student = get_memory();
input_point(student);
head=add_to_head(head,student);
return head;
}
// 遍历整个链表并依次打印各节点数据
void show_all_student(STU head)
{
STU p = head;
while(p != NULL)
{
printf("%s %g\n",p->name,p->point);
p = p->next;
}
}
//4, 供主函数调用,并返回插入节点后的头结点
STU insert_points(STU head)
{
//printf("请输入学生姓名和成绩\n");
while(1)
{
head = add_student_point(head);
printf("是否继续录入成绩\n");
int i=0;
scanf("%d",&i);
if(i==0)
{
break;
}
}
return head;
}
insert_point.h
#include "list.h"
#ifndef __INSERT_H__
#define __INSERT_H__
void show_all_student(STU head);
STU insert_points(STU head);
STU add_to_head(STU head,STU p);
#endif
file.c
#include "file.h"
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include "insert_point.h"
//1, 把结构体地址的写到二进制文件
void write_to_file(STU head)
{
//2, 打开文件并返回文件指针
FILE *str = fopen("./student_file","wb");
STU p = head;
//3, 把链表的各个节点循环写入文件
while(p != NULL)
{
fwrite(p,sizeof(struct point),1,str);
p = p->next;
}
fclose(str);
}
//1, 从文件中读取二进制数据写入各个节点内存内
STU read_from_file(STU head)
{
//2, 打开文件
FILE *str = fopen("./student_file","rb");
//3, 分配堆内存用来存储二进制数据
STU p = malloc(sizeof(struct point));
while((fread(p,sizeof(struct point),1,str))>0)
{
//4, 读取的数据写入节点内存后,把该节点插入链表,并返回头节点
head=add_to_head(head,p);
p = malloc(sizeof(struct point));
}
fclose(str);
free(p);
return head;
}
file.h
#include "list.h"
#ifndef __FILE_H__
#define __FILE_H__
STU read_from_file(STU head);
void write_to_file(STU head);
#endif
list.h
#ifndef __LIST_H__
#define __LIST_H__
//定义一个结构体指针,放在头文件内,供所有.c文件调用
typedef struct point
{
char name[10];
float point;
struct point *next;
}*STU;
#endif
这是我在Ubuntu环境下编辑的代码,同时要在当前文件夹中创建一个student_file文件用来保存数据,为了方便编译可以创建一个Makefile文件如下
vim Makefile然后在文件里编辑如下代码
main:main.c insert_point.c interface.c file.c
gcc main.c insert_point.c interface.c file.c -o main
下面是Ubuntu下的执行结果
还可以自己加入别的功能,比如排序插入节点,删除节点之类的
之前的版本源码连接(可以参考):https://download.csdn.net/download/xzhksd123/10845790