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

📄 学生通讯录.cpp

📁 学生通讯录管理系统
💻 CPP
字号:
#include<iostream.h>
#include<stdio.h>
#include"student.h"
#include<string.h> 
#include<fstream.h> 

Link::Link(void)
{
  head=new LinkNode;  
  head->next=NULL;
}////////////////////////  函数功能:将链头指针初始化为带表头结点的空链

Link::~Link(void)
{
 struct LinkNode *p;
 
 while(head!=NULL)
 {
	 p=head;
	 head=head->next;
	 delete p;
 }
}///////////////// 函数功能:将链表所有结点的空间释放(包括表头结点)

void Link::HeadCreate(int n)
{
  struct LinkNode *p, *h=NULL;
  for(int i=0;i<n;i++)
  {
	  p=new LinkNode;
	  
	  p->data=InputStuInfo();   

	  p->next=h;
	  h=p;
  };
  head->next=h;   
}///////////////  函数功能1:用头插入建立链表方法生成一个具有n个结点的链表


int Link::Insert(int i ,DataType x)
{
 struct LinkNode *temp;
 struct LinkNode *p,*q; 
 

 if ( i<=0) 
  {
	cout<<"\n the position is error\n\n";
    return 0;   // error
  }
  
  int j=1;
  q=head; p=head->next; 
  while(j <i && p!=NULL)     
  {
     q=p; p=p->next;
	 j++;
  }
  temp=new LinkNode;
  temp->data=x;
  temp->next=p;
  q->next=temp;
  return 1;  // ok
}///////////////////////////// 函数功能:将一个新值插入到某个位置的结点之后上,如果超过总结点个数,则做尾插入

DataType InputStuInfo(void)   
{
    DataType  x;

	cout<<"\n姓名:";
	cin>>x.stname;
	cout<<"\n学号:";
	cin>>x.stno;
	cout<<"\n出生日期:";
	cin>>x.birthday;
	cout<<"\n性别(0=男,1=女):";
	cin>>x.sex;
	cout<<"\n联系电话:"	;
	cin>>x.tel;
	cout<<"\n地址:";
	cin>>x.address;

	return x;

}////////////  函数功能2:从键盘中接受用户输入学生的信息

int Link::Delete(int i)
{
 struct LinkNode *q,*p;
 
 int j=1;

 if ( i<=0) 
  {
	cout<<"\n the position is error\n\n";
    return 0;   
  }
  
  q=head; p=head->next;
  while(j <i && p!=NULL)
  {
     q=p;
	 p=p->next;
	 j++;
  }
  if (p==NULL)
  {
	  cout<<"the position is out of range\n\n";
	  return 0;  
  }
  q->next=p->next;
  delete p;
  return 1; 
}//////////////////////// 函数功能3:将链表中某个位置上的结点删除

void Link::ReadFile(void)   
{
  ifstream fp;
  DataType x;
  LinkNode *p;

  fp.open("student.dat",ios::in|ios::binary);
  
  head=new LinkNode;   
  head->next=NULL;

  while(fp.read((char*)&x,sizeof(x))) 
  {  
     p=new LinkNode;
	 p->data=x;
	 p->next=head->next;
	 head->next=p;
  }
  fp.close();
}//////////////////函数功能4:从文件中读取所有的学生信息并用头插入方法建立链表

void Link::WriteFile(void)     
{
  LinkNode *p;
  ofstream fp;
  DataType x;

  fp.open("student.dat",ios::out|ios::binary);
  p=head->next;  
  while(p!=NULL)
  {  
     x=p->data;
     fp.write((char *) &x,sizeof(x));
	 p=p->next;
  }
  fp.close();
}///////////////函数功能5:将所有的学生信息保存到文件中

LinkNode *Link::Locate(void)                                              
{
    char StuNo[10];
	LinkNode *p;

	cout<<endl<<"please input the NO to search:";
	cin>>StuNo;

	p=head->next;
    while(p!=NULL)
	{
		if (strcmp(p->data.stno,StuNo)==0)
			 return p;
	    else
			p=p->next;

	}
     return NULL; 
}/////////////函数功能6:按学号进行查找


void Link::Print(void)
{
 struct LinkNode *p=head->next;   
 cout<<"\n The data in the link are:\n";
 while ( p!=NULL)
 {
     
	 OutputStuInfo(p->data);  

	 p=p->next;
 }
 cout<<"\n\n\n";
}///////////////  函数功能7:从头到尾的将链表中的所有结点一个一个输出来(不包括表头结点)


void OutputStuInfo(DataType x)   
{
    
	cout<<"name:"<<x.stname;
	cout<<"  No:"<<x.stno;
	cout<<"  birthday:"<<x.birthday;
	cout<<"  sex(0=boy,1=girl):"<<x.sex;
	cout<<"  tel:"<<x.tel;
	cout<<"  address:"<<x.address<<endl;

}///////////// 函数功能:将学生信息输出到计算机屏幕上


void main()
{
	ofstream fop("student.dat");
	Link link1;    
	int finished=0;
    int choice , n ,pos;
	DataType x;

	while ( !finished )
    {
		cout<<"\n\n*********Menu*********\n";
		cout<<"1:新建学生通讯录\n";
        cout<<"2:插入学生信息\n";
		cout<<"3:删除学生信息\n";
		cout<<"4:读取通讯录信息\n";
		cout<<"5:向文件写入学生通讯录信息\n";
        cout<<"6:查询\n";                              
		cout<<"7:在屏幕中输出全部学生信息\n";
		cout<<"8:退出\n";
		cout<<"请选择(1-8):";
		cin>>choice;
		switch(choice)
		{
		case 1:
			   cout<<"\nplease enter the number of nodes(insert in head):";   //调用头插入建立链表
			   cin>>n;
			   link1.HeadCreate(n);
			   link1.Print();
               break; 
        
		case 2:
               cout<<"\nplease enter the position to insert:";     //调用链表插入函数
			   cin>>pos;
			   cout<<"\n please enter the data to insert\n";
			   
			   x=InputStuInfo();  
			   
			   if ( link1.Insert(pos,x)==1)  
				    link1.Print();
               break; 
		case 3:
               cout<<"\nplease enter the position to delete:";     //调用链表删除函数
			   cin>>pos;
			   if ( link1.Delete(pos)==1 )
			      link1.Print();  
               break; 
        case 4:
			  link1.ReadFile();      //调用文件读取函数
			  break;
        case 5:
			  link1.WriteFile();      //调用文件写入函数
			  break;
        case 6:                                   
			  LinkNode *p;
			  p=link1.Locate();      //调用按学号查找函数
			  if (p==NULL)
				  cout<<"不存在该学号的学生信息"<<endl;
			  else 
			      OutputStuInfo(p->data);
			  break;
        case 7:
			  link1.Print();      //调用链表输出函数     
			  break;
        case 8:
			   finished=1;     //结束程序
			   break;
		}  
	}
}			   





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -