下面是代码实现:
SEQ_LIST.h:
#ifndef _DSEQ_LIST_H_
#define _DSEQ_LIST_H_
#define INIT_SIZE 10
typedef struct _STU
{
int num;
char name[20];
int age;
char sex;
char college[20]; //现专业
char old_college[20]; //原专业
bool flag; //fasle true
int grade;
}STU;
////
typedef STU elem_tpye;
typedef struct _DSEQ_LIST
{
elem_tpye *data;
int length;
int total_length;
}DSEQ_LIST;
DSEQ_LIST *init_dseq_list();
bool destory_dseq_list(DSEQ_LIST *p);
bool insert_d(DSEQ_LIST *p, int pos, elem_tpye *e);
int get_length(DSEQ_LIST *p);
bool is_full_d(DSEQ_LIST *p);
bool is_empty_d(DSEQ_LIST *p);
void print_int(elem_tpye *e);
bool show_d(DSEQ_LIST *p, void (*pfunc)(elem_tpye *));
bool clear_seq_list(DSEQ_LIST *p);
bool insert_head(DSEQ_LIST *p, elem_tpye *e);
bool insert_tail(DSEQ_LIST *p, elem_tpye e);
bool del_head(DSEQ_LIST *p, elem_tpye *e);
bool del_tail(DSEQ_LIST *p, elem_tpye *e);
bool del(DSEQ_LIST *p,int pos, elem_tpye *e);
bool put(DSEQ_LIST *p, int pos, elem_tpye *e);
bool get(DSEQ_LIST *p, int pos, elem_tpye *e);
bool get_pos_by_value(DSEQ_LIST *p,elem_tpye *e,int *n);
bool sort(DSEQ_LIST *p);
bool get_max(DSEQ_LIST *p, elem_tpye *e);
bool get_min(DSEQ_LIST *p, elem_tpye *e);
DSEQ_LIST *intersec_a_and_b(DSEQ_LIST *a,DSEQ_LIST *b);
bool meger(DSEQ_LIST *p,DSEQ_LIST *s,DSEQ_LIST *rs);
#endif
CSTU.h
#ifndef _CSTU_H_
#define _CSTU_H_
#include "DESQ_LIST.h"
//初始化顺序表
DSEQ_LIST *init_cstu(char *su_data_path);
int show_main_interface();//显示主界面
char show_search_interface();//显示查询界面
void print_stu(elem_tpye *p);
void add_stu(DSEQ_LIST *su);
bool del_stu(DSEQ_LIST *su);
bool change_stu(DSEQ_LIST *su);
bool change_college_information(DSEQ_LIST *su);
void search_fun(DSEQ_LIST *su,char rs);
void search_all(DSEQ_LIST *su);
bool search_num(DSEQ_LIST *su);
bool search_name(DSEQ_LIST *su);
bool search_college(DSEQ_LIST *su);
void quit(DSEQ_LIST *su,char *su_data_path);
#endif
DESQ_LIST.cpp
#include "DESQ_LIST.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
DSEQ_LIST *init_dseq_list()
{
DSEQ_LIST* p = (DSEQ_LIST *)malloc(sizeof(DSEQ_LIST));
assert(p != NULL);
p->length = 0;
p->total_length = INIT_SIZE;
p->data = (elem_tpye *)malloc(sizeof(elem_tpye)*INIT_SIZE);
assert(p->data != NULL);
return p;
}
bool destory_dseq_list(DSEQ_LIST *p)
{
if(p == NULL||p->data == NULL)
{
return false;
}
free(p->data);
free(p);
return true;
}
static void inc(DSEQ_LIST *p)
{
p->data = (elem_tpye *)realloc(p->data,sizeof(elem_tpye)*p->total_length*2);
p->total_length = p->total_length*2;
}
bool insert_d(DSEQ_LIST *p, int pos, elem_tpye *e)
{
if(p == NULL||pos<0||pos>p->length)
{
return false;
}
if(is_full_d(p))
{
inc(p);
}
for(int i =p->length -1;i>=pos;i--)
{
p->data[i+1] = p->data[i];
}
p->data[pos] = *e;
p->length++;
return true;
}
bool is_full_d(DSEQ_LIST *p)
{
return p->length == p->total_length;
}
bool is_empty_d(DSEQ_LIST *p)
{
return p->length == 0;
}
void print_int(elem_tpye *e)
{
printf("%d ",*e);
}
bool show_d(DSEQ_LIST *p, void (*pfunc)(elem_tpye *))
{
if(p == NULL)
{
return false;
}
for(int i = 0;i<p->length;i++)
{
pfunc(&p->data[i]);
}
return 0;
}
bool insert_head(DSEQ_LIST *p, elem_tpye *e)
{
if (p == NULL)
{
return false;
}
if(is_full_d(p))
{
inc(p);
}
for (int i=p->length-1;i>=0;i--)
{
p->data[i+1] = p->data[i];
}
p->data[0] = *e;
p->length++;
return true;
}
bool insert_tail(DSEQ_LIST *p, elem_tpye e)
{
if (p == NULL)
{
return false;
}
if(is_full_d(p))
{
inc(p);
}
p->data[p->length] = e;
p->length++;
return true;
}
bool del_head(DSEQ_LIST *p, elem_tpye *e)
{
if (p == NULL)
{
return false;
}
*e = p->data[0];
for(int i = 0;i<p->length-1;i++)
{
p->data[i] = p->data[i+1];
}
p->length--;
return true;
}
bool del(DSEQ_LIST *p,int pos, elem_tpye *e)
{
if (p == NULL||pos<0||pos>=p->length||is_full_d(p))
{
return false;
}
*e = p->data[pos];
for (int i = pos;i<p->length-1;i++)
{
p->data[i] = p->data[i+1];
}
p->length--;
return true;
}
bool put(DSEQ_LIST *p, int pos, elem_tpye *e)
{
if(p == NULL||pos<0||pos>p->length+1)
{
return false;
}
if (pos = p->length+1)
{
p->data[pos-1] = *e;
p->length++;
}
else
{
p->data[pos-1] = *e;
}
return true;
}
bool get(DSEQ_LIST *p, int pos, elem_tpye *e)
{
if(p == NULL||pos<0||pos>p->length)
{
return false;
}
*e = p->data[pos-1];
return true;
}
int get_length(DSEQ_LIST *p)
{
if (p == NULL)
{
return -1;
}
return p->length;
}
bool sort(DSEQ_LIST *p)
{
if (p == NULL || is_empty_d(p))
{
return false;
}
elem_tpye tmp;
for (int i = 0;i<p->length;i++)
{
for (int j = 0;j<p->length-i;j++)
{
if ((strcmp(p->data[j].name,p->data[j+1].name))>0)
{
tmp = p->data[j];
p->data[j] = p->data[j+1];
p->data[j+1] = tmp;
}
}
}
return true;
}
CSTU.cpp
#include "CSTU.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//初始化顺序表
DSEQ_LIST *init_cstu(char *su_data_path)
{
DSEQ_LIST* su = init_dseq_list();
FILE *fr = fopen(su_data_path,"rb");
STU tmp;
if (fr != NULL)
{
while (fread(&tmp,sizeof(STU),1,fr)!=0)
{
insert_tail(su,tmp);
}
fclose(fr);
}
return su;
}
//显示主界面
int show_main_interface()
{
int rs = 6;
printf("##################################\n");
printf("###########学生管理系统###########\n");
printf("# 1.录入学生信息 #\n");
printf("# 2.查询学生信息 #\n");
printf("# 3.修改学生信息 #\n");
printf("# 4.删除学生信息 #\n");
printf("# 5.转专业信息 #\n");
printf("# 6.退出系统 #\n");
printf("请输入编号:");
scanf("%d",&rs);//
fflush(stdin);
return rs;
}
//显示查询界面
char show_search_interface()
{
char rs = 'a';
printf("######################################\n");
printf("###########欢迎进入查询系统###########\n");
printf("# a.查询所有 #\n");
printf("# b.按学号查询 #\n");
printf("# c.按姓名查询 #\n");
printf("# d.按专业查询 #\n");
printf("# e.查询结束 #\n");
printf("请输入序号\n");
scanf("%c",&rs);
fflush(stdin);
return rs;
}
void search_fun(DSEQ_LIST *su,char rs)
{
switch(rs)
{
case 'a':
search_all(su);
break;
case 'b':
search_num(su);
break;
case 'c':
search_name(su);
break;
case 'd':
search_college(su);
break;
default:
printf("输入的编号有误\n");
break;
}
printf("\n\n");
}
//对于已存在的学号不进行增加
void add_stu(DSEQ_LIST *su)
{
STU tmp;
printf("请输入学生信息(格式如下学号-姓名-年龄-性别-专业-年级)\n");
scanf("%d-%[^-]-%d-%c-%[^-]-%d",
&tmp.num,
tmp.name,
&tmp.age,
&tmp.sex,
tmp.college,
&tmp.grade);
fflush(stdin);
for (int i = 0;i<su->length;++i)
{
if (su->data[i].num == tmp.num)
{
printf("该学号的学生已存在,请不要重新插入\n");
return ;
}
}
insert_tail(su,tmp);
printf("插入成功!");
}
bool change_stu(DSEQ_LIST *su)
{
if (su == NULL)
{
return false;
}
if (is_empty_d(su))
{
printf("此系统中无学生信息,请先录入!");
return false;
}
STU tmp;
printf("请输入要修改的学生的学号:\n");
scanf("%d",&tmp.num);
fflush(stdin);
int i = 0;
for (i = 0;i<su->length;i++)
{
if (su->data[i].num == tmp.num)
{
break;
}
}
strcpy((su->data[i]).old_college,(su->data[i]).college);
printf("请重新录入该学生的信息:\n");
scanf("%d-%[^-]-%d-%c-%[^-]-%d",
&(su->data[i]).num,
(su->data[i]).name,
&(su->data[i]).age,
&(su->data[i]).sex,
(su->data[i]).college,
&(su->data[i]).grade);
fflush(stdin);
if (strcmp((su->data[i]).old_college , (su->data[i]).college) )
{
su->data[i].flag = false;
}
printf("学生信息修改成功!\n");
printf("修改后的信息为:\n");
print_stu(&su->data[i]);
return true;
}
bool del_stu(DSEQ_LIST *su)
{
if(su==NULL||is_empty_d(su))
{
return false;
}
STU tmp;
printf("请输入要删除学生的学号:");
scanf("%d",&tmp.num);
fflush(stdin);
int i = 0;
for(i=0;i<su->length;i++)
{
if((su->data[i]).num==tmp.num)
{
break;
}
}
del(su,i,&tmp);
printf("删除成功\n");
return true;
}
void print_stu(elem_tpye *p)
{
if (!p->flag)
{
printf("%d-%s-%d-%c-原专业:%s-现专业:%s-%d\n",
p->num,
p->name,
p->age,
p->sex,
p->old_college,
p->college,
p->grade);
}
else
{
printf("%d-%s-%d-%c-%s-%d\n",
p->num,
p->name,
p->age,
p->sex,
p->college,
p->grade);
}
}
void search_all(DSEQ_LIST *su)
{
if (su == NULL)
{
return ;
}
if (is_empty_d(su))
{
printf("无学生,请先输入!");
return ;
}
sort(su);
show_d(su,print_stu);
}
bool search_num(DSEQ_LIST *su)
{
if (su == NULL)
{
return false;
}
if (is_empty_d(su))
{
printf("没有学生信息,请先录入!");
return false;
}
elem_tpye tmp;
printf("请输入要查询的学号:\n");
scanf("%d",&tmp.num);
fflush(stdin);
int i = 0;
for (i = 0;i<su->length;i++)
{
if ((su->data[i]).num == tmp.num)
{
print_stu(&su->data[i]);
break;
}
}
if (i == su->length)
{
printf("无此学生信息!");
}
return true;
}
bool search_name(DSEQ_LIST *su)
{
if (su == NULL)
{
return false;
}
if (is_empty_d(su))
{
printf("无学生信息,请先录入!");
return false;
}
elem_tpye tmp;
printf("请输入要查询的学生姓名:");
scanf("%s",tmp.name);
fflush(stdin);
int num = 0;
for (int i = 0;i<su->length;i++)
{
if ( !strcmp( (su->data[i]).name , tmp.name ))
{
num++;
print_stu(&su->data[i]);
}
}
if (num == 0)
{
printf("没有此名字的学生!");
}
return true;
}
bool search_college(DSEQ_LIST *su)
{
if (su == NULL)
{
return false;
}
if (is_empty_d(su))
{
printf("无学生信息,请先录入!");
return false;
}
elem_tpye tmp;
printf("请输入要查询的学生专业:");
scanf("%s",tmp.college);
fflush(stdin);
int num = 0;
for (int i = 0;i<su->length;i++)
{
if ( !strcmp( (su->data[i]).college , tmp.college ))
{
num++;
print_stu(&su->data[i]);
}
}
if (num == 0)
{
printf("没有此专业的学生!");
}
return true;
}
bool change_college_information(DSEQ_LIST *su)
{
printf("以下为转专业同学的信息\n");
for(int i=0;i<su->length;i++)
{
if((su->data[i]).flag)
{
print_stu(&su->data[i]);
}
}
return true;
}
void quit(DSEQ_LIST *su,char *su_data_path)
{
FILE *fw = fopen(su_data_path,"wb");
if (fw == NULL)
{
printf("error-文件打开失败");
return ;
}
for (int i = 0;i<get_length(su);i++)
{
fwrite(&su->data[i],sizeof(STU),1,fw);
}
fclose(fw);
destory_dseq_list(su);
printf("系统退出成功");
exit(0);
}
测试代码:
#define _CRT_SECURE_NO_WARNINGS
#include "CSTU.h"
#include <stdio.h>
#include <stdlib.h>
#include <vld.h>
int main()
{
char *su_data_path = "e:\\su.seq_data";
DSEQ_LIST *su = init_cstu(su_data_path);
int rs1 = 0;
char rs2 = 'e';
while(1)
{
rs1 = show_main_interface();
switch(rs1)
{
case 1:
add_stu(su);
break;
case 2:
rs2 = show_search_interface();
search_fun(su,rs2);
break;
case 3:
change_stu(su);
break;
case 4:
del_stu(su);
break;
case 5:
change_college_information(su);
break;
case 6:
quit(su,su_data_path);
break;
default:
printf("输入有误!\n");
break;
}
printf("\n\n");
}
return 0;
}