下面是代碼實現:
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;
}