C语言小程序-基于链表的学生信息管理

程序支持增加、查询、删除、存盘和读取操作

一 程序定义和函数声明

  头文件studentsys.h定义如下

/*  student management system by list  */
#ifndef _StudentSys_H
#define _StudentSys_H #define MAX 25 /* lenth of name string */
typedef struct _Grade Grade;;
typedef struct StdNode Std;;
typedef struct ListNode *List;

  全局结构体定义

/* 6 courses */
struct _Grade{
int Chinese;
int Math;
int English;
int Programming;
int Physic;
int Geology;
}; /* student info */
struct StdNode {
int Id; /* student id */
char name[MAX];/* student name */
Grade grades; /* student grades */
}; struct ListNode {
Std info;
List next;
};

  函数声明

  /* 1->Add   2->Find   3->Del   4->Brows   5->Store
     6->Read    7->DelAll   8->Cls    9->Quit  */    功能选择键

int AddStd( List L, int Id, char *name, Grade grades );
/* flag:0 by id, 1 by name */
int FindStd(List L, int flag, int Id ,char *name);
int DeleteStd( List L, int Id );
void PrintInfo( List L );
void PrintAll( List L );
int StoreInfo( List L );
List ReadInfo( );
void DestoryInfo( List L );
int IsRepeat( List L, int Id );
void selectInfo();

二 源文件studentsys.c

  主体部分,由while循环下的switch分支进行功能选择

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "studentsys.h" int main(void)
{
List L; /* Head Node */
L = (List)malloc(sizeof( struct ListNode ));
L->next = NULL; int i, flag; /* flag做为查找方式标志, 0由Id, 1由姓名进行搜索 */
int option, Id = ; /* option选择功能, init info */
char name[MAX]= " ";
Grade grades; printf(" Starting \n");
printf(" Student Management system, ver:0.101 \n");
printf("******************************************************\n");
printf(" 学生管理系统\n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
selectInfo();
while ( ) {
scanf("%d", &option);
switch ( option ) {
case : printf("Please in put id, name, six grades:\n");
scanf("%d %s", &Id, name);
scanf("%d %d %d %d %d %d", &grades.Chinese, &grades.Math,
&grades.English, &grades.Programming,&grades.Physic,
&grades.Geology );
if ( AddStd( L, Id, name, grades ) == ) {
printf("Added student.\n");
} else {
printf("Sorry, can't add student\n");
}
break; case : printf("select method: 0 by Id, 1 by name\n");
scanf("%d", &flag);
printf("Please in put id, name\n");
scanf("%d %s", &Id, name); /* notice & */
if ( FindStd( L, flag, Id, name ) == ) {
printf("found student\n");
} else {
printf("sorry, can't found student\n");
}
break; case : printf("Which id you want to del: ");
scanf("%d", &Id);
if ( DeleteStd( L, Id ) ) {
printf("Successfully Delete!\n");
} else {
printf(" Not found!\n");
}
break; case : PrintAll( L );
break; case : if ( StoreInfo( L ) ) {
printf("Write successfulluy!\n");
} else {
printf("Failed to read!\n");
}
break; case : L = ReadInfo( );
break;
case : DestoryInfo( L );
break;
case : system("cls");
selectInfo();
break;
case : exit();
break;
default: printf("error!请重新输入!\n");
}
}
return ;
}

  

  函数主体,由简单的链表的插入、删除和遍历

int AddStd( List L, int Id, char *name, Grade grades )
{
List Head = L;
List Cur;
if( IsRepeat( L, Id) == ) {
printf(" Id repeat!!!\n");
return ;
}
/* 链表的插入 */
Cur = (List)malloc(sizeof( struct ListNode ));
if ( Cur != NULL ) {
Cur->info.Id = Id;
strcpy( Cur->info.name, name);
Cur->info.grades.Chinese = grades.Chinese;
Cur->info.grades.Math = grades.Math;
Cur->info.grades.English = grades.English;
Cur->info.grades.Programming = grades.Programming;
Cur->info.grades.Physic = grades.Physic;
Cur->info.grades.Geology = grades.Geology; Cur->next = Head->next;
Head->next = Cur; return ;
} else {
printf("Failed to malloc!\n");
return ;
}
} int FindStd( List L, int flag, int Id , char *name )
{
List Cur = L->next;
while ( Cur != NULL ) {
if( flag == ) {
if ( strcmp( name, Cur->info.name ) == ) {
PrintInfo(Cur);
break;
}
} else if ( flag == ) {
if ( Cur->info.Id == Id ) {
PrintInfo(Cur);
break;
}
}
Cur = Cur->next;
}
if ( Cur == NULL ){
return ;
} else {
return ;
}
} int DeleteStd( List L, int Id )
{
List Pre = L;
List Cur = Pre->next; if( L->next == NULL ) {
printf("Empty Node!\n");
return ;
} else {
while ( Cur != NULL ) {
if ( Cur->info.Id == Id ) {
/* 找到对应编号 */
Pre->next = Cur->next;
free(Cur);
break;
}
Pre = Cur;
Cur = Cur->next;
}
if ( Cur == NULL ) {
return ;
} else {
return ;
}
} /* 1 else */
} void PrintInfo( List L )
{
List Cur = L;
if ( Cur != NULL ) {
printf("ID: %d ", Cur->info.Id);
printf("Name: %s\n", Cur->info.name);
printf("Chinese :%d ", Cur->info.grades.Chinese);
printf("Math :%d ", Cur->info.grades.Math);
printf("English :%d\n", Cur->info.grades.English);
printf("Programming:%d ", Cur->info.grades.Programming);
printf("Physic :%d ", Cur->info.grades.Physic);
printf("Geology :%d\n", Cur->info.grades.Geology);
}
} void PrintAll( List L )
{
List Cur = L->next;
int i = ;
while ( Cur != NULL ) {
i++;
printf("%d-->Student ", i);
PrintInfo( Cur );
Cur = Cur->next;
}
}
void DestoryInfo( List L )
{
List Cur, Tmp;
Cur = L->next;
while ( Cur != NULL ) {
Tmp = Cur->next;
free( Cur );
Cur = Tmp;
}
L->next = NULL;
printf("删除所有学生信息成功!\n");
} int IsRepeat( List L, int Id )
{
List Cur = L->next;
int flag = ;
while ( Cur != NULL ) {
if ( Cur->info.Id == Id ) {
       flag = ;
       break;    
     }
     Cur = Cur->next;
}
  return flag;
} void selectInfo()
{
printf("Stumansys 请选择:\n");
printf("(1)---增加学生 (2)---查询学生 (3)---删除学生 \n");
printf("(4)---查看全部 (5)---记录存盘 (6)---读取信息\n");
printf("(7)---删除所有 (8)---清除屏幕 (9)---退出系统\n");
}

  文件读写操作

    1. 链表写入文件,每次写一个结构体,直到链表尾。

    2. 文件读取,使用feof(fp)函数判断是否达到文件流尾,否则读取一个结构体,并尾插入链表。

int StoreInfo( List L )
{
FILE *fp = fopen("student.data", "w");
List H = L->next;
const int size = ;
int ret = -; /* default */ if ( fp == NULL ) {
printf("Failed to open file!\n");
} else if( H == NULL ) {
printf("Empty student info!\n");
} else {
/* write list into fp */
while ( H != NULL ) {
ret = fwrite( H, sizeof(H->info), size, fp );
H = H->next;
}
ret = ;
}
fclose( fp );
return ret;
} List ReadInfo( )
{
int i = ; /* count */
List L, P, tmp;
P = L = (List)malloc( sizeof(struct ListNode) );
FILE *fp = fopen("student.data", "r"); if ( fp == NULL ) {
printf("Failed to read file!\n");
return NULL;
}
/* 假如文件为空,检查文件结束符 feof在到达文件流尾返回非零值 */
if ( feof(fp) != ) {
tmp = (List)malloc( sizeof(struct ListNode) );
fread( tmp, sizeof(tmp->info), , fp);
if( feof(fp) ) {
printf("读取失败!文件为空!\n");
free(tmp);
}
return(NULL);
} while ( feof(fp) == ) {
tmp = (List)malloc( sizeof(struct ListNode) );
fread( tmp, sizeof(tmp->info), , fp);
printf("正在读取第%d个数据\n", ++i);
if ( feof(fp) != ) {
/* reached end */
free(tmp); break;
}
/* List tail insert */
P->next = tmp;
P = tmp;
P->next = NULL;
}
fclose(fp);
printf("Read successfulluy!\n");
return L;
}

C语言小程序-基于链表的学生信息管理

上一篇:2008---2009学年(A)B)第1学期《中国近代史纲要》课程考核试卷


下一篇:三代PacBio reads纠错 - 专题