当前位置:网站首页>C language project: student achievement system
C language project: student achievement system
2022-07-03 10:49:00 【Humboldt】
1. Description of project
The project is in linux Pass through C Written in language , The amount of code is not large, only 300 Row or so , The purpose of writing this project is to practice C Some basic knowledge of language . If the reader wants the code to jump directly to Chapter 3, copy the code according to the file name .
1.1 Project requirements
The project needs to achieve the following objectives :
- There are two ways of administrator and student login ;
- The account and password of the administrator are confirmed , The student login is verified according to the entered student name , Account number input 3 Exit after failure .
- The administrator has entered new students 、 Show all student information 、 Modify student information 、 Permission to delete student information , Student login only has display permission
- The entered information is saved to a local file
1.2 Overview of project document structure
main.c Call in file list.c In the document list_creathead() Function to create a header node , After completion of creation fileTonode() Function to open the entered student information file , Write the information into the linked list . At the same time, the file also calls manage.c In the document login() Function to log in to the learning achievement system .
The project document is mainly composed of four parts ,main.c The file starts as the entry function of the program ,student.h Define the data structure of learning information ,manage.c File defines user operation functions , These functions need to be called list.c Some function implementations in the file
2. Introduction to project functions
2.1 Introduction to project keywords
Used when defining header files #ifndef #define #endif keyword . During the compilation preprocessing process, some header files will be added to the current file , Like here. list.c The file contains list.h file , During pretreatment, the list.h Copy the entire file to list.c In file , and manage.c The file also contains list.h file , At this time, if the pretreatment will list.h When the file is added, it will appear Repeat the definition , It's like defining in a file int x; And then again int x; There will be repeated definitions and errors .
In order to avoid this kind of repetition, the problem of inclusion arises #ifndef #define #endif keyword , If there is no definition xxx(if not define abbreviation ifndef) So define (define) xxx, If it is defined xxx, Then jump to endif after , That is, do not operate .
extern keyword : In the header file extern + Variables and extern + The function name is Declare the scope of a function or global variable , Its declared functions and variables can be used in this module and other modules , Remember that it is a declaration, not a definition ! in other words B modular ( Compilation unit ) If you reference a module ( Compilation unit )A When a global variable or function defined in , It just contains A The header file of the module , In the compilation phase , modular B Although the function or variable cannot be found , But it won't report an error , It will link from the module A This function was found in the generated object code . Like here. manage.c Add list.h File can be called list.c The function defined in .
2.2 manage Function introduction in the document
void login() function : The user can choose whether to login with administrator account or student user
int adminor() function : Administrator user login , After logging in, select the operation
int students() function : Student user login
void quit() function : sign out
void input() function : Enter new student
void delet( ) function : according to id Delete a student's information
void update() function : according to id Information modify a student's information
2.3 list Function introduction in the document
lnode* list_creathead() function : adopt malloc Dynamically allocate memory to create header nodes
void list_insert(st msg) function : adopt manage.c Insert the input information in the student information node
void list_show() function : Display information through linked list
int list_delete(int id) function : adopt id Find information first , Then delete the found node information
int list_update(int id) function : Modify the information
int list_find(char* str) function : find information
int fileTonode() function : Write the information in the file to the node by reading
int nodeTofile() function : Write node information to file
Many linked list operations are involved here , For a detailed introduction, please refer to another article I wrote :《 Introduction to single linked list operation 》
There's one more thing to note here , The project uses student information to put a structure , Then make it a variable and put it into another structure , Such benefits can be seen in : Structure contains structure member variables
3. Code
3.1 main and student file
main.c file
#include <stdio.h>
#include <unistd.h>
#include "manage.h"
#include "list.h"
int main()
{
head = list_creathead();
fileTonode();
login();
}
Student data file student.h file
#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 file
manage.h file
#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 file
#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 file
list.h file
#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 file
#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;
}
边栏推荐
- Mysql5.7 installation and configuration tutorial (Graphic ultra detailed version)
- MySQL checks for automatic updates at 0:00 every day
- Leetcode skimming ---202
- Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
- 带你走进云原生数据库界扛把子Amazon Aurora
- Numpy realizes the classification of iris by perceptron
- 帶你走進雲原生數據庫界扛把子Amazon Aurora
- Leetcode skimming ---1
- 神经网络入门之预备知识(PyTorch)
- Knowledge map enhancement recommendation based on joint non sampling learning
猜你喜欢

Bidding website architecture project progress -- Network Security

神经网络入门之矩阵计算(Pytorch)

Cache routing component

面试官:Redis中列表的内部实现方式是什么?

Interviewer: what is the internal implementation of the list in redis?

Uni app learning 1 bottom menu and parent-child components

Ut2016 learning notes

6、 Data definition language of MySQL (1)
![[untitled]](/img/2b/177970366174e50e75b5c820c95d08.jpg)
[untitled]

神经网络入门之预备知识(PyTorch)
随机推荐
Common scenarios in which Seata distributed transactions fail and do not take effect (transactions do not rollback)
MAUI Developer Day in GCR
How to make a blood bar in the game
If you always feel that you need to persist in learning English
C语言项目:学生成绩系统
How to hide cvxpy warnings: warn: a- > P (column pointers) not strictly increasing, column x empty?
Iterator iterator enhances for loop
Large scale e-commerce project - environment construction
【吐槽&脑洞】关于逛B站时偶然体验的弹幕互动游戏魏蜀吴三国争霸游戏的一些思考
Flink <-->JDBC的使用介绍+with参数
QT:QSS自定义QMenu实例
Nuget add reference error while installing packages
.Net Core-做一个微信公众号的排队系统
Weight decay (pytorch)
Multilayer perceptron (pytorch)
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
conda9.0+py2.7+tensorflow1.8.0
QT:QSS自定义QGroupBox实例
Leetcode skimming ---374
Bidding website architecture project progress -- Network Security