⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 复件2 list.txt

📁 学生系统
💻 TXT
字号:


#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;
}

/*
nodetype* List::creatlist (int n)       //创建链表
{
	nodetype *h=NULL;
	int i=1;

	for(int j=0; j<n; j++)
	{
		h=insnode(0);
	}
	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->someone.name, c);
		readstr(fp, c);
		strcpy(p->someone.sex, c);

		readstr(fp, c);
		strcpy(p->someone.num, c);
		readstr(fp, c);
		strcpy(p->someone.birthday, c);
		readstr(fp, c);
		strcpy(p->someone.partment, c);
		readstr(fp, c);
		strcpy(p->someone.workyear, c);
		readstr(fp, c);
		strcpy(p->someone.E-mail, c);

		readstr(fp, c);
		strcpy(p->someone.telphone, c);
		readstr(fp, c);
		strcpy(p->someone.graduate, c);
		readstr(fp, c);
		strcpy(p->someone.address, c);
		readstr(fp, c);
		strcpy(p->someone.duty, c);
		readstr(fp, c);
		strcpy(p->someone.notice, c);

		readstr(fp, c);
		strcpy(p->someone.in_time, c);
	

		p=p->next;
	}

	fclose(fp);

	return p;
}

void List::dispnode(nodetype* p)		//显示一个职工的所有信息
{
	if(p!=NULL)
	{
		
		dispworker(p);
	}
}

void List::dispname()		//显示所有职工姓名
{
	nodetype* p=head;
	cout<<"现有的职工:  "<<endl;
	if(p==NULL)
		cout<<"没有任何职工数据"<<endl;
	while(p!=NULL)
	{
		cout<<"姓名:	"<<p->someone.name;

		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->someone.name)))    //查找第 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->pe.name)))    //查找第 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<<"输入的 i 值不正确"<<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<<"输入的 i 值不正确"<<endl;
	}
	head=h;
}

void List::editworker(nodetype* p)
{
	char c[100];
	cout<<"请输入姓名: "<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.name, c);

	cout<<"请输入性别:"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.sex, c);

        cout<<"请输入编号:"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.num, c);

	cout<<"请输入生日(格式举例:1982-1-1): "<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.birthday, c);

	cout<<"请输入部门:"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.partment, c);

	cout<<"请输入工作年限:"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.workyear, c);

	cout<<"请输入E-mail"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.E-mail, c);

        cout<<"请输入电话"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.telphone, c);

        cout<<"请输入学历"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.graduate, c);
  
        cout<<"请输入地址"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.address, c);

        cout<<"请输入职务"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.duty, c);

        cout<<"请输入进入时间"<<endl;
	cin>>c;
	strcat(c, "\n");
	strcpy(p->someone.in_time, c);

	cout<<"编辑个人信息完成!"<<endl;

	dispworker(p);
}



void List::dispworker(nodetype* p)
{
	cout<<"姓名:		"<<p->someone.name;
	cout<<"性别:		"<<p->someone.sex;
	cout<<"电话:		"<<p->someone.telphone;
	cout<<"进公司时间:      "<<p->someone.in_time;
	cout<<"出生日期:	"<<p->someone.birthday;
	cout<<"家庭住址:	"<<p->someone.address;
	cout<<"编号:		"<<p->someone.num;
	cout<<"职务:	        "<<p->someone.duty;
	cout<<"部门:	        "<<p->someone.partment;
	cout<<"工作年限:	"<<p->someone.workyear;
	cout<<"E-mail:	        "<<p->someone.E-mail;
}


void List::help()
{
	cout<<endl<<endl;
	cout<<"----------------------------------------------------------"<<endl;
	cout<<"1:			编辑职工信息"<<endl;           
	cout<<"2:			显示个人信息"<<endl;
	cout<<"3:			显示该职工所有信息"<<endl;
	cout<<"4:			帮助菜单"<<endl;
	cout<<"5:			返回上一级菜单"<<endl;
	cout<<"----------------------------------------------------------"<<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 + -