📄 list.cpp
字号:
#include "LIST.H"
LIST::LIST()
{
head = NULL;
}
bool LIST::Check(char *a, char *b) //对比两个字符串是否相等
{
int i;
int j = strlen(b);
for(i = 0; i < j; i ++)
{
if(*a == *b)
{
a ++;
b ++;
}
else
return 0;
}
return 1;
}
nodetype* LIST::Creatlist (int n) //创建链表
{
nodetype *h = NULL, *s, *t;
int i=1;
for(int j=0; j < n; j ++)
{
if(i == 1) //创建第一个节点
{
h = (nodetype*)malloc(sizeof(nodetype));
h->next = NULL;
t = h;
}
else //创建其余节点
{
s = (nodetype*)malloc(sizeof(nodetype));
s->next = NULL;
t->next = s;
t = s; //t 始终指向生成的单链表的最后一个节点
}
i++;
}
head = h;
return h;
}
void LIST::Readstr(FILE *f,char *string)
{
do
{
// 先读入一行文本
fgets(string, 255, f); //fgets(): 从文件 f 读入长度为 255-1 的字符串并存入到 string 中
} while ((string[0] == '/') || (string[0] == '\n'));
return;
}
nodetype* LIST::Load()
{
FILE *fp;
nodetype *p;
char c[255];
int num;
if((fp = fopen("student.txt", "r")) == NULL)
{
cout << "打开文件失败" << endl;
return 0;
}
Readstr(fp, c);
sscanf(c, "The Length Of Link: %d", &num); //获取链表长度
p = Creatlist(num); //创建链表
for(int i = 0; i < num; i ++)
{
Readstr(fp, c);
strcpy(p->st.pe.name,c);
Readstr(fp, c);
strcpy(p->st.pe.num, c);
Readstr(fp, c);
strcpy(p->st.pe.sex, c);
Readstr(fp, c);
strcpy(p->st.pe.birthday, c);
Readstr(fp, c);
strcpy(p->st.pe.nation, c);
Readstr(fp, c);
strcpy(p->st.pe.clas, c);
Readstr(fp, c);
strcpy(p->st.pe.college, c);
Readstr(fp, c);
strcpy(p->st.te.phone, c);
Readstr(fp, c);
strcpy(p->st.te.tel, c);
Readstr(fp, c);
strcpy(p->st.te.address, c);
Readstr(fp, c);
strcpy(p->st.sc.english, c);
Readstr(fp, c);
strcpy(p->st.sc.chiness, c);
Readstr(fp, c);
strcpy(p->st.sc.math, c);
Readstr(fp, c);
strcpy(p->st.sc.pe, c);
p=p->next;
}
fclose(fp);
return p;
}
void LIST::DispNode(nodetype* p) //显示一个学生的所有信息
{
if(p != NULL)
{
cout << "--------------------------------------------------" << endl;
DispPerson(p);
DispTelephone(p);
DispScore(p);
}
}
void LIST::DispName() //显示所有学生姓名
{
nodetype* p = head;
cout << "现有学生姓名数据: " << endl;
if(p == NULL)
cout << "没有任何学生数据" << endl;
while(p != NULL)
{
cout << "姓名: "<< p->st.pe.name;
p=p->next;
}
}
void LIST::DispNum()//显示所有学生学号
{
nodetype* p = head;
cout << "现有学生学号数据: " << endl;
if(p == NULL)
cout << "没有任何学生数据" << endl;
while(p != NULL)
{
cout << "学号: "<< p->st.pe.num;
p=p->next;
}
}
void LIST::DisAll() //显示所有学生信息
{
nodetype* p = head;
cout << "现有学生数据: " << endl << endl;
if(p == NULL)
cout << "没有任何学生数据" << endl;
while(p != NULL)
{
DispNode(p);
p=p->next;
}
}
int LIST::Listlen() //返回链表长度
{
int i = 0;
nodetype* p = head;
while(p != NULL)
{
p = p->next;
i ++;
}
return i;
}
nodetype* LIST::FindNode (int i) //通过查找序号返回节点的指针
{
nodetype* p = head;
int j = 1;
if( i > Listlen() || i <= 0 ) // i 上溢或下溢
return NULL;
else
{
while( p != NULL && j < i ) //查找第 i 个节点并由 p 指向该节点
{
j ++;
p = p->next;
}
return p;
}
}
nodetype* LIST::Find(char c[]) //通过查找姓名或学号返回节点的指针
{
nodetype* p = head;
int j = 1;
strcat(c, "\n"); //从外部读入的字符串末尾都带了一个换行符
while( p != NULL && !((Check(c, p->st.pe.name)) || (Check(c,p->st.pe.num)))) //查找第 i 个节点并由 p 指向该节点
{
j ++;
p = p->next;
}
return p;
}
int LIST::Find2(char c[]) //通过查找姓名或学号返回节点的序号
{
nodetype* p = head;
int j = 1;
strcat(c, "\n"); //从外部读入的字符串末尾都带了一个换行符
while( p != NULL && !((Check(c, p->st.pe.name)) || (Check(c,p->st.pe.num)))) //查找第 i 个节点并由 p 指向该节点
{
j ++;
p = p->next;
}
return j;
}
nodetype* LIST::InsNode(int i)
{
nodetype *h = head, *p, *s;
s = (nodetype*)malloc(sizeof(nodetype)); //创建节点 s
s->next = NULL;
if(i == 0) //i=0 时 s 作为该单链表的第一个节点
{
s->next = h;
h = s; //重新定义头节点
}
else
{
p = FindNode(i); //查找第 i 个节点,并由 p 指向该节点
if(p != NULL)
{
s->next = p->next;
p->next = s;
}
else cout << "没有相关记录!" << endl;
}
head = h;
return s;
}
void LIST::DelNode(int i) //删除第 i 个节点
{
nodetype *h = head, *p = head, *s;
int j = 1;
if(i == 1) //删除第一个节点
{
h = h->next;
free(p);
}
else
{
p = FindNode(i-1); //查找第 i-1 个节点,并由 p 指向这个节点
if(p != NULL && p->next != NULL)
{
s = p->next; // s 指向要删除的节点
p->next = s->next;
free(s);
}
else
cout<<"没有相关记录!"<<endl;
}
head = h;
}
void LIST::EditPerson(nodetype* p)
{
char c[100];
cout << "请输入学号: " << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.pe.num, c);
cout << "请输入姓名: " << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.pe.name, c);
cout<<"请输入性别:"<<endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.pe.sex, c);
cout << "请输入生日(格式举例:2007-6-1): " << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.pe.birthday, c);
cout << "请输入民族:" << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.pe.nation, c);
cout << "请输入学院:" << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.pe.college, c);
cout << "请输入专业班级:" << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.pe.clas, c);
cout << endl << "输入个人信息完成!"<<endl;
DispPerson(p);
}
void LIST::EditScore(nodetype* p)
{
char a[50];
cout << "请输入语文成绩: " << endl;
cin >> a;
strcat(a, "\n");
strcpy(p->st.sc.chiness, a);
cout << "请输入英语成绩: " << endl;
cin >> a;
strcat(a, "\n");
strcpy(p->st.sc.english, a);
cout<<"请输入数学成绩: "<<endl;
cin >> a;
strcat(a, "\n");
strcpy(p->st.sc.math, a);
cout << "请输入体育成绩: "<<endl;
cin >> a;
strcat(a, "\n");
strcpy(p->st.sc.pe, a);
cout << endl << "输入学科成绩完成!"<<endl;
DispScore(p);
}
void LIST::EditScore2(nodetype*p)
{
char a[50],cho[20];
cout << "**********************************************" << endl;
cout << "1.修改语文成绩" << endl;
cout << "2.修改英语成绩" << endl;
cout << "3.修改数学成绩" << endl;
cout << "4.修改体育成绩" << endl;
cin >> cho;
system("cls");
if(Check(cho,"1"))
{
cout << "请输入语文成绩: " << endl;
cin >> a;
strcat(a, "\n");
strcpy(p->st.sc.chiness, a);
}
if(Check(cho,"2"))
{
cout << "请输入英语成绩: " << endl;
cin >> a;
strcat(a, "\n");
strcpy(p->st.sc.english, a);
}
if(Check(cho,"3"))
{
cout<<"请输入数学成绩: "<<endl;
cin >> a;
strcat(a, "\n");
strcpy(p->st.sc.math, a);
}
if(Check(cho,"4"))
{
cout<<"请输入体育成绩: "<<endl;
cin>>a;
strcat(a, "\n");
strcpy(p->st.sc.pe, a);
}
}
void LIST::EditTelephone(nodetype* p)
{
char c[50];
cout << "请输入手机号码: " << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.te.phone, c);
cout << "请输入家庭电话: " << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.te.tel, c);
cout << "请输入家庭地址: " << endl;
cin >> c;
strcat(c, "\n");
strcpy(p->st.te.address, c);
cout << endl << "输入联系方式完成!" << endl;
this->DispTelephone(p);
}
void LIST::DispPerson(nodetype* p)
{
cout << "学号:" << p->st.pe.num << endl;
cout << "姓名:" << p->st.pe.name << endl;
cout << "性别:" << p->st.pe.sex << endl;
cout << "生日:" << p->st.pe.birthday << endl;
cout << "民族:" << p->st.pe.nation << endl;
cout << "学院:" << p->st.pe.college << endl;
cout << "班级:" << p->st.pe.clas << endl;
}
void LIST::DispScore(nodetype* p)
{
cout << "语文成绩: " << p->st.sc.chiness << endl;
cout << "英语成绩: " << p->st.sc.english << endl;
cout << "数学成绩: " << p->st.sc.math << endl;
cout << "体育成绩: " << p->st.sc.pe << endl;
}
void LIST::DispTelephone(nodetype* p)
{
cout << "手机号码: " << p->st.te.phone << endl;
cout << "家庭电话: " << p->st.te.tel << endl;
cout << "家庭地址: " << p->st.te.address << endl << endl;
}
LIST::~LIST()
{
nodetype *pa = head, *pb;
if(pa != NULL)
{
pb = pa->next;
if(pb == NULL)
free(pa);
else
{
while(pb != NULL)
{
free(pa);
pa = pb;
pb = pb->next;
}
free(pa);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -