- 1. 项目概述
- 1.1 项目需求
- 1.2 项目文件结构概述
- 2. 项目函数介绍
- 2.1 项目关键字介绍
- 2.2 manage文件中函数介绍
- 2.3 list文件中函数介绍
- 3. 代码
- 3.1 main 和student文件
- 3.2 manage 文件
- 3.3 list 文件
项目是在linux中通过C语言进行编写的,代码量不大只有300行左右,写这个项目的目的是为了练习C语言的一些基础知识。如果读者想要代码直接跳转到第三章按照文件名拷贝代码即可。
1.1 项目需求项目需要实现以下目的:
- 具有管理员与学生登录两种方式;
- 管理员登入的账号和密码是确定的,而学生登入是根据已经录入的学生姓名进行核实登入的,账号输入3次失败后退出。
- 管理员有输入新学生、显示所有学生信息、修改学生信息、删除学生信息的权限,学生登录只有显示权限
- 录入的信息保存到本地文件
main.c文件中调用list.c文件中的list_creathead() 函数创建头结点,创建完成后调用 fileTonode() 函数将录入的学生信息文件打开,将信息写入链表中。同时该文件还调用 manage.c文件中的login()函数进行学习成绩系统登入。
项目文件主要由四部分组成,main.c文件作为程序的入口函数开始,student.h 定义了学习信息的数据结构,manage.c文件定义用户操作功能,这些功能需要调用list.c文件中的一些函数实现
定义头文件时会使用#ifndef #define #endif关键字。在编译预处理过程中会将一些头文件加入到当前文件中,如这里的list.c文件中包含了list.h文件,在预处理时会将list.h整个文件拷贝到list.c文件中,而manage.c文件中同样也包含list.h文件,这时如果预处理再将list.h文件加入进去就会出现重复定义,就像是在一个文件中定义了 int x;然后又定义一次 int x;这就会出现了重复定义会报错。
为了避免这种重复包含的问题就出现了#ifndef #define #endif关键字,意思是如果没有定义 xxx(if not define缩写 ifndef)那么就定义(define) xxx,如果是定义过xxx,那么就跳到endif后,也就是不操作。
extern关键字:在头文件中 extern + 变量和extern + 函数名的作用是声明函数或全局变量的作用范围,其声明的函数和变量可以在本模块和其他模块中使用,记住它是一个声明不是定义!也就是说B模块(编译单元)要是引用模块(编译单元)A中定义的全局变量或函数时,它只要包含A模块的头文件即可,在编译阶段,模块B虽然找不到该函数或变量,但它不会报错,它会在链接时从模块A生成的目标代码中找到此函数。如这里的manage.c中添加list.h文件后可以调用list.c中定义的函数。
2.2 manage文件中函数介绍void login()函数:用户选择是管理员账号登入还是学生用户登入
int adminor()函数:管理员用户登入,登入后进行操作选择
int students()函数:学生用户登入
void quit()函数:退出
void input()函数:输入新增学生
void delet( )函数:根据id删除某个学生信息
void update()函数:根据id信息修改某个学生信息
lnode* list_creathead()函数:通过malloc动态分配内存方式创建头节点
void list_insert(st msg)函数:通过manage.c中的输入信息插入学生信息节点
void list_show()函数:通过链表显示信息
int list_delete(int id)函数:通过id信息先查找,然后再删除查找到的节点信息
int list_update(int id)函数:修改信息
int list_find(char* str)函数:查找信息
int fileTonode()函数:将文件中的信息通过读取写入到节点
int nodeTofile()函数:将节点信息写入文件
这里涉及到很多链表操作,详细的介绍可以参见我写的另一篇文章:《单链表操作介绍》
这里还有一点需要注意,项目中采用学生信息放一个结构体,然后将其做一个一个变量放入另一个结构体中,这样的好处可以参见:结构体中含有结构体成员变量
main.c文件
#include#include #include "manage.h" #include "list.h" int main() { head = list_creathead(); fileTonode(); login(); }
学生数据文件 student.h文件
#ifndef STUDENT_H #define STUDENT_H #include3.2 manage 文件struct student { int id; char name[20]; int age; int classid; char sex[10]; int math; int chinese; }; typedef struct student st; st s1; #endif
manage.h文件
#ifndef MANAGE_H #define MANAGE_H #include#include extern void login(); extern int adminor(); extern void input(); extern void show(); extern void update(); extern void del(); extern void quit(); extern void delet(); #endif
manage.c文件
#include3.3 list 文件#include "manage.h" #include "student.h" void login() { printf("Please input your IDn"); printf("1:admin 2:studentn"); int temp = 0; scanf("%d", &temp); switch(temp) { case 1: printf("Welcom Admin !n"); adminor(); break; case 2: printf("Welcom student !n"); students(); break; } } int adminor() { printf("Please input your name and passwordn"); char name[20]; int num = 0,times = 0; scanf("%s %d", name, &num); while(times < 2) { times = times + 1; if(!(strcmp(name,"admin")) && (num == 1234)) { printf("Welcom to student score systemn"); while(1) { printf("please choice your operaten"); printf("1. input 2. shown"); printf("3. update 4. deln"); printf("5. quiten"); int temp = 0; scanf("%d",&temp); switch(temp) { case 1: printf("chose 1 inputn"); input(); break; case 2: printf("chose 2 shown"); list_show(); break; case 3: printf("chose 3 updaten"); update(); break; case 4: printf("chose 4 deleten"); delet(); break; default: printf("chose 5 quitn"); nodeTofile(); quit(); break; } } } else { printf("name or password wrongn"); sleep(1); printf("please input your name and passwordn"); scanf("%s %d",name, &num); } } printf("Username or password error 3 times and Quitn"); } int students() { printf("Please input your namen"); char name[20]; int times = 0; scanf("%s", name); while(times < 2) { times = times + 1; if(!list_find(name)) { printf("Welcom to student score systemn"); while(1) { printf("please choice your operaten"); printf("1. show 2. quiten"); int temp = 0; scanf("%d",&temp); switch(temp) { case 1: printf("chose 1 inputn"); list_show(); break; default: printf("chose 2 quiten"); quit(); break; } } } else { printf("name wrongn"); sleep(1); printf("please input your name n"); scanf("%s",name); } } printf("input 3 times error and will quiten"); sleep(1); return 1; } void quit() { exit(0); } void input() { st s1; printf("please inputn"); printf("id name age classid sex math chinesen"); scanf("%d %s %d %d %s %d %d",&s1.id,s1.name,&s1.age,&s1.classid,s1.sex,&s1.math,&s1.chinese); printf("input message id=%d,name=%s,age=%d,classid=%d,sex=%s,math=%d,chinese=%dn",s1.id,s1.name,s1.age,s1.classid,s1.sex,s1.math,s1.chinese); list_insert(s1); } void delet( ) { int temp = 0; printf("please input id which node will be deleten"); scanf("%d",&temp); list_delete(temp); } void update() { int temp = 0; printf("please input id which node will be updaten"); scanf("%d", &temp); list_update(temp); }
list.h文件
#ifndef LIST_H #define LIST_H #include#include #include "student.h" struct node { st data; struct node* next; }; typedef struct node lnode; extern lnode* head; extern lnode* list_creathead(); extern void list_insert(st msg); extern void list_show(); extern int list_delete(); extern int list_update(); extern int list_find(char* str); extern int fileTonode(); extern int nodeTofile(); #endif
list.c文件
#include "list.h"
lnode* head = NULL;
lnode* list_creathead()
{
lnode* p = (lnode* ) malloc(sizeof(lnode));
p->next = NULL;
return p;
}
void list_insert(st msg)
{
lnode* p = (lnode* ) malloc(sizeof(lnode));
p->data = msg;
p->next = NULL;
lnode* current = head;
while(current->next != NULL)
{
current = current->next;
}
current->next = p;
}
void list_show()
{
int node = 0;
lnode* current = head;
while(current->next != NULL)
{
node = node + 1;
current = current->next;
printf("node %d: id = %d,name = %s, age = %d, classid = %d, sex = %s, math = %d, chinese = %dn",node ,current->data.id, current->data.name, current->data.age, current->data.classid, current->data.sex, current->data.math, current->data.chinese);
}
printf("printf finishn");
}
int list_delete(int id)
{
lnode* current = head;
lnode* p = NULL;
while(current->next != NULL)
{
if(current->next->data.id == id)
{
printf("find node which id = %d and delete this noden", id);
p = current->next;
current->next = p->next;
return 1;
}
current = current->next;
}
printf("not find node which id = %dn",id);
return 0;
}
int list_update(int id)
{
lnode* current = head->next;
while(current != NULL)
{
if(current->data.id == id)
{
printf("find node which id = %d and begin update n",id);
printf("please input name age classid sex math chinesen");
scanf("%s %d %d %s %d %d",current->data.name, &(current->data.age), &(current->data.classid), current->data.sex, &(current->data.math), &(current->data.chinese));
printf("now the node change to:n id = %d name = %s age = %d classid = %d sex = %s math = %d chinese = %dn",current->data.id, current->data.name, current->data.age, current->data.classid, current->data.sex, current->data.math, current->data.chinese);
}
current = current->next;
}
printf("not find node which id = %dn",id);
}
int list_find(char* str)
{
lnode* current = head->next;
while(current != NULL)
{
if(!strcmp(current->data.name,str))
{
return 0;
}
current = current->next;
}
return 1;
}
int fileTonode()
{
FILE* fp = NULL;
fp = fopen("student.txt","rb");
if(fp == NULL)
{
printf("open file errorn");
return 1;
}
st temp;
while(fread(&temp,sizeof(st),1,fp))
{
list_insert(temp);
}
fclose(fp);
return 0;
}
int nodeTofile()
{
FILE* fp = NULL;
fp = fopen("student.txt","wb");
lnode* current = head->next;
while(current != NULL)
{
fwrite(&(current->data),sizeof(st),1,fp);
current = current->next;
}
fclose(fp);
return 0;
}



