C语言项目:学生成绩系统

1. 项目概述

项目是在linux中通过C语言进行编写的,代码量不大只有300行左右,写这个项目的目的是为了练习C语言的一些基础知识。如果读者想要代码直接跳转到第三章按照文件名拷贝代码即可。

1.1 项目需求

项目需要实现以下目的:

  1. 具有管理员与学生登录两种方式;
  2. 管理员登入的账号和密码是确定的,而学生登入是根据已经录入的学生姓名进行核实登入的,账号输入3次失败后退出。
  3. 管理员有输入新学生、显示所有学生信息、修改学生信息、删除学生信息的权限,学生登录只有显示权限
  4. 录入的信息保存到本地文件

1.2 项目文件结构概述

main.c文件中调用list.c文件中的list_creathead() 函数创建头结点,创建完成后调用 fileTonode() 函数将录入的学生信息文件打开,将信息写入链表中。同时该文件还调用 manage.c文件中的login()函数进行学习成绩系统登入。
项目文件主要由四部分组成,main.c文件作为程序的入口函数开始,student.h 定义了学习信息的数据结构,manage.c文件定义用户操作功能,这些功能需要调用list.c文件中的一些函数实现

2. 项目函数介绍

2.1 项目关键字介绍

定义头文件时会使用#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信息修改某个学生信息

2.3 list文件中函数介绍

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()函数:将节点信息写入文件
这里涉及到很多链表操作,详细的介绍可以参见我写的另一篇文章:《单链表操作介绍》
这里还有一点需要注意,项目中采用学生信息放一个结构体,然后将其做一个一个变量放入另一个结构体中,这样的好处可以参见:结构体中含有结构体成员变量

3. 代码

3.1 main 和student文件

main.c文件

#include <stdio.h>
#include <unistd.h>
#include "manage.h"
#include "list.h"

int main()
{
	head = list_creathead();
	fileTonode();
	login();	
}

学生数据文件 student.h文件

#ifndef STUDENT_H
#define STUDENT_H

#include <stdio.h>

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

3.2 manage 文件

manage.h文件

#ifndef MANAGE_H
#define MANAGE_H

#include <stdio.h>
#include <unistd.h>

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文件

#include <stdlib.h>

#include "manage.h"
#include "student.h"

void login()
{
    printf("Please input your ID\n");
    printf("1:admin         2:student\n");
    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 password\n");
    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 system\n");
            while(1)
            {
                printf("please choice your operate\n");
                printf("1. input            2. show\n");
                printf("3. update           4. del\n");
                printf("5. quite\n");

                int temp = 0;
                scanf("%d",&temp);
                switch(temp)
                {
                    case 1:
                        printf("chose 1 input\n");
						input();
                        break;
                    case 2:
                        printf("chose 2 show\n");
                        list_show();
						break;
                    case 3:
                        printf("chose 3 update\n");
                        update();
						break;
                    case 4:
                        printf("chose 4 delete\n");
						delet();
						break;
                    default:
                        printf("chose 5 quit\n");
						nodeTofile();
						quit(); 
                        break;
                }
            }
        }
        else
        {
            printf("name or password wrong\n");
            sleep(1);
            printf("please input your name and password\n");
            scanf("%s %d",name, &num);
        }

    }
    printf("Username or password error 3 times and Quit\n");
}

int students()
{   
    printf("Please input your name\n");
    char name[20];
    int times = 0;
    scanf("%s", name);
    while(times < 2)
    {   
        times = times + 1;
		if(!list_find(name))
		{	
            printf("Welcom to student score system\n");
            while(1)
            {
                printf("please choice your operate\n");
                printf("1. show           2. quite\n");
                
				int temp = 0;
                scanf("%d",&temp);
                switch(temp)
                {
                    case 1:
                        printf("chose 1 input\n");
						list_show();
                        break;
                    default:
                        printf("chose 2 quite\n");
                        quit();
						break;
				}
			}
		} else
		{   
			printf("name  wrong\n");
            sleep(1);
            printf("please input your name \n");
            scanf("%s",name);
        }
	}
	printf("input 3 times error and will quite\n");
	sleep(1);
	return 1;

}

void quit()
{
	exit(0);
}

void input()
{
	st s1;
	printf("please input\n");
	printf("id name age classid sex math chinese\n");
	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=%d\n",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 delete\n");
	scanf("%d",&temp);
	list_delete(temp);
}

void update()
{
	int temp = 0;
	printf("please input id which node will be update\n");
	scanf("%d", &temp);
	list_update(temp);
}

3.3 list 文件

list.h文件

#ifndef LIST_H
#define LIST_H

#include <stdio.h>
#include <stdlib.h>
#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 = %d\n",node ,current->data.id, current->data.name, current->data.age, current->data.classid, current->data.sex, current->data.math, current->data.chinese);		
	}
	printf("printf finish\n");
}

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 node\n", id);
			p = current->next;
			current->next = p->next;
			return 1;
		}
		current = current->next;
	}
	printf("not find node which id = %d\n",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 chinese\n");
			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 = %d\n",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 = %d\n",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 error\n");
		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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值