注:该项目是本人在2020年刚刚学完数据结构时,写的一个小项目,其中对变量和函数的命名还不成熟,仅供大家做一个参考。
主函数部分
#include "../include/test.h"
int main(int argc, const char *argv[])
{
//学生高考报名系统
//创建哈系表即学生信息总表
zong *Student = NULL;
if(HashCreat(&Student) < 0 )
{
printf("创建失败\n");
}
printf("创建成功\n");
int fr = open("Message.txt", O_RDONLY);
if(fr < 0 )
{
printf("打开失败\n");
}
else
{
printf("打开成功\n");
}
//先将文件内的内容进行报名,创建一个xinxi类型用来接受文件的内容;
xinxi *OneData= NULL;
while(1)
{
OneData = (xinxi *)malloc(sizeof(xinxi));
int MyRead = read(fr, OneData, sizeof(xinxi) );
if(MyRead < 0 )
{
printf("读取失败\n");
close(fr);
break;
}
else if( 0 == MyRead )
{
printf("导入信息完毕\n");
close(fr);
break;
}
else
{
//证明已经读取到了一个学生的信息;
if( baoming(Student,OneData) < 0 )
{
close(fr);
return -1;
}
printf("一个学生已成功报名\n");
Student->Last =OneData->zhengjian;
}
}
int i=0;
int zhengjian;
char school[20];
while(1)
{
printf("输入1进入报名阶段\n");
printf("输入2进入取消报名节阶段\n");
printf("输入3进入修改信息阶段\n");
printf("输入4进入改卷子阶段\n");
printf("输入5查看总成绩\n");
printf("输入6进入填报志愿阶段\n");
printf("输入7将所以的数据保存在文件中\n");
printf("输入-1退出\n");
printf("请输入\n");
scanf("%d",&i);
if( -1 == i)
{
printf("程序已退出\n");
break;
}
switch(i)
{
case 1:
{
printf("现在开始报名\n");
xinxi *NewStudent = (xinxi*)malloc(sizeof(xinxi));
printf("请输入你的姓名\n");
scanf("%s",NewStudent->name);
//自动生成准考证
NewStudent->zhengjian = Student->Last+1;
printf("请输入你的年龄\n");
scanf("%d",&(NewStudent->age));
if( baoming(Student,NewStudent) < 0 )
{
DestoryStudent(&Student);
return -1;
}
printf("%d号考生已报名成功\n",NewStudent->zhengjian);
Student->Last = NewStudent->zhengjian;
break;
}
case 2:
{
//不想考了,取消报名
printf("请输入你的考号\n");
scanf("%d",&zhengjian);
if(DelStudent(Student, zhengjian) < 0 )
{
DestoryStudent(&Student);
return -1;
}
printf("%d号考生已成功取消报名\n",zhengjian);
break;
}
case 3:
//填错了,想修改以下信息
printf("请输入考生的考号\n");
scanf("%d",&zhengjian);
//调用修改函数;
if( xiugai(Student,zhengjian) < 0 )
{
printf("修改失败\n");
break;
}
break;
case 4:
//进行成绩的录入,遍历哈系表给chengji赋值
chengji(Student);
break;
case 5:
//查询信息
chaxun(Student);
break;
case 6:
printf("请输入你的证件\n");
scanf("%d",&zhengjian);
printf("请输入你要报考的大学,其中有qinghua,beida,baowenli,other");
scanf("%s",school);
if(strcmp(school,"qinghua") == 0 )
{
if((Student->arr[zhengjian-1])->chengji[0] >= qinghua )
{
printf("你被录取了\n");
break;
}
else
{
printf("没考上\n");
break;
}
}
case 7:
{
int fw = open("Message.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fw < 0)
{
perror("open_write error");
close(fw);
DestoryStudent(&Student);
return -1;
}
for(i=0; i<Student->Last;i++)
{
xinxi *pTemp = Student->arr[i];
int Mywrite = write(fw, pTemp,sizeof(xinxi));
if(Mywrite < 0)
{
perror("write error");
break;
}
if(Mywrite == 0)
{
printf("写入完毕\n");
break;
}
if(Mywrite > 0)
{
printf("正在写入\n");
}
}
close(fw);
}
}
}
xiaohui(Student);
DestoryStudent(&Student);
printf("销毁成功\n");
return 0;
}
下面是具体函数实现部分
#include "../include/test.h"
int HashCreat(zong **Student)
{
if( NULL == Student)
{
printf("传参失败\n");
return -1;
}
*Student = (zong *)malloc(sizeof(zong));
if( NULL == *Student)
{
return -1;
}
memset(*Student, '\0', sizeof(zong));
return 1;
}
int baoming(zong *Student, xinxi *New)
{
if( NULL == Student)
{
printf("创建失败\n");
return -1;
}
int Index = New->zhengjian-1;
Student->arr[Index] = New;
Student->count++;
return 1;
}
int DelStudent(zong *Student, int zhengjian)
{
Student->arr[zhengjian-1] = NULL;
return 0;
}
int xiugai(zong *Student, int zhengjian)
{
int Age = 0;
printf("请输入你要修改的姓名\n");
scanf("%s",Student->arr[zhengjian-1]->name);
printf("请输入你要修改的年龄\n");
scanf("%d",&Age);
(Student->arr[zhengjian-1])->age = Age ;
return 1;
}
int chengji(zong *Student)
{
//其中共有3门成绩,0的位置放总成绩,123分别放语数英成绩
int i=0;
for(i=0;i<=Student->Last;i++)
{
if(NULL == Student->arr[i])
{
continue;
}
printf("证件为%d的学生的信息\n",i+1);
printf("请输入该学生的语文:");
scanf("%d",&(Student->arr[i])->chengji[1] );
printf("请输入该学生的数学成绩:");
scanf("%d",&(Student->arr[i])->chengji[2] ) ;
printf("请输入该学生的英语成绩:");
scanf("%d",&(Student->arr[i])->chengji[3] ) ;
(Student->arr[i])->chengji[0] = Student->arr[i]->chengji[1]+Student->arr[i]->chengji[2]+Student->arr[i]->chengji[3];
i++;
}
printf("\n");
return 1;
}
int chaxun(zong *Student)
{
int zhengjian;
printf("请输入要查询的考号\n");
scanf("%d",&zhengjian);
if(NULL == (Student->arr[zhengjian-1]) )
{
printf("此人不存在\n");
return -1;
}
else
{
printf("该学生的姓名为%s",Student->arr[zhengjian-1]->name);
printf("该学生的年龄为%d",Student->arr[zhengjian-1]->age);
printf("该学生的总成绩为%d",Student->arr[zhengjian-1]->chengji[0]);
}
return 0;
}
//销毁哈西表
int DestoryStudent(zong **Student)
{
free(*Student);
*Student = NULL;
return 1;
}
//销毁函数
int xiaohui(zong *Student)
{
int i=0;
for(i=0;i<Student->Last;i++)
{
free(Student->arr[i]);
Student->arr[i] = NULL;
}
return 0;
}
下面是主控Makefile
OBJS:=test.o list.o
CC:=gcc
Myapp:=gaokao
CFLAGS:=-c -g -Wall
export OBJS CC Myapp CFLAGS
ALL:
make -C ./src/
make -C ./obj/
.PHONY:clear
clear:
$(RM) ./obj/*.o
$(RM) ./bin/gaokao
下面是src文件下的makefile
ALL:$(OBJS)
mv *.o ../obj/
test.o:test.c
$(CC) $(CFLAGS) $< -o $@
list.o:list.c
$(CC) $(CFLAGS) $< -o $@
下面是obj目录下的makefile
ALL:$(Myapp)
mv $(Myapp) ../bin/
$(Myapp):$(OBJS)
$(CC) $^ -o $@
下面是include文件,其中对函数的命名部分切勿模仿,对院校成绩的数据可以引入数据库(由于本人当时没有掌握数据库的操作故用了一个枚举)
#ifndef _TEST_H
#define _TEST_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define N 20
#define SIZE 100
typedef struct student_xinxi
{
char name[N]; //学生的姓名
int age; //学生的年龄
int zhengjian;
int chengji[4]; //学生的4们成绩;
}xinxi;
typedef struct student_zong
{
xinxi *arr[SIZE]; //最多可以报名100人
int count; //报名的总数
int Last; //报名的最后一个人的准考证号
}zong;
enum gaoxiao
{
qinghua = 700,
beida = 600,
baowenli = 500,
other = 400
};
int HashCreat(zong **Student);
int baoming(zong *Student, xinxi *New);
int DelStudent(zong *Student, int zhengjian);
int xiugai(zong *Student, int zhengjian);
int chengji(zong *Student);
int chaxun(zong *Student);
int DestoryStudent(zong **Student);
int xiaohui(zong *Student);
#endif