#include<iostream>
//#include"Accommodations.h"
#define Area_number 3
#define Staff_number 10
using namespace std;
typedef struct Staff_love
{
int Area_NO;
int Love;
struct Staff_love*next;
} Staff_love;
typedef struct Staff
{
int staff_NO;
struct Staff_love *Staff_love1;//*staff_love;
struct Staff*next;
} Staff;
typedef struct Area_sselect
{
int Areo_NO;
}
void Staff_number_allocation(int area_number,int staff_number,int *area_staff_number);
void Staff_love_statistics(int area_number,int staff_number,int **staff);
void Show_arry(int*arry,int all);
void Point_free(int staff_number,int **staff);
void Show(int *area_staff_number,int **staff,int area_number,int staff_number);
Staff* Create_list(int staff_number,int area_number,int **staff);
Staff_love* Insert(Staff_love*p,int idata,int _NO);
void Show_List(Staff*List);
void Accommodations(Staff*List,int **staff,int *area_staff_number,int area_number,int staff_number);
int main()
{
初始化,模拟输入数据
int *area_staff_number=new int[Area_number]; // 定义每个区域可承受员工的数量
int **staff=new int *[Staff_number]; //定义了一个Staff_number的指向指针的指针,其中每个指针包含了Area_number个int型数据
Staff_number_allocation(Area_number,Staff_number,area_staff_number);
//Show_arry(area_staff_number,Area_number);
Staff_love_statistics(Area_number,Staff_number,staff);
Show(area_staff_number,staff,Area_number,Staff_number);
// 建立链表,并且将每个员工对区域的喜好排序
Staff* List;
if((List=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
List=Create_list(Staff_number,Area_number,staff);
Show_List(List);
//释放空间
delete []area_staff_number;
Point_free(Staff_number,staff);
return 0;
}
Staff_love* Insert(Staff_love*p,int idata,int _NO)
{
Staff_love*head,*insert,*form;
if(p==NULL)
{
if((p=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
p->Area_NO=_NO;
p->Love=idata;
p->next=NULL;
return p;
}
if((head=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
head=p;
if((insert=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
insert->Area_NO=_NO;
insert->Love=idata;
insert->next=NULL;
// 如果比head->love小
if(insert->Love>=head->Love)
{
insert->next=head;
head=insert;
}
else
{
if((form=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
while((insert->Love<=p->Love)&&(p->next!=NULL))
{
form=p;
p=p->next;
}
if((p->next!=NULL)||(insert->Love>p->Love) ) // 如果idata不是大于所有的值
{
form->next=insert;
insert->next=p;
}
else /// 当p指向NULL时,此处出错 如何修改?????
{
p->next=insert;
//form->next=insert;
//p->next=NULL;
}
}
return head;
}
Staff* Create_list(int staff_number,int area_number,int **staff)
{
Staff*head,*present,*form;
if((head=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
head->staff_NO=0;
Staff_love*stafflove,*stafflove_head;
if((stafflove=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
stafflove->Love=*(*(staff+0)+0);
stafflove->Area_NO=0;
stafflove->next=NULL;
if((stafflove_head=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
stafflove_head=stafflove;
for(int i=1;i<area_number;i++)
{
stafflove=Insert(stafflove,*(*(staff+0)+i),i);
}
head->Staff_love1=stafflove;
head->next=NULL;
if((form=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
form=head;
for(int k=1;k<staff_number;k++)
{
if((present=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
present->staff_NO=k;
Staff_love*stafflove;
if((stafflove=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
stafflove->Love=*(*(staff+k)+0);
stafflove->Area_NO=0;
stafflove->next=NULL;
for(int i=1;i<area_number;i++)
{
stafflove=Insert(stafflove,*(*(staff+k)+i),i);
}
present->Staff_love1=stafflove;
present->next=NULL;
form->next=present;
form=present;
}
//head->Staff_love1=stafflove_head;
return head;
}
void Staff_number_allocation(int area_number,int staff_number,int *area_staff_number)
{
int sub=0;
for(int i=0;i<area_number;i++)
*(area_staff_number+i)=1;
sub=staff_number-area_number;
int k=0;
while(sub!=0)
{
int n=rand()%2;
*(area_staff_number+k)+=n;
sub-=n;
if(k==area_number-1)
k=0;
else
k++;
}
}
void Staff_love_statistics(int area_number,int staff_number,int **staff)
{
//staff=new int *[staff_number];
for(int k=0;k<staff_number;k++)
{
*(staff+k)=new int[area_number];
for(int i=0;i<area_number;i++)
{
*(*(staff+k)+i)=rand()%100;
}
//cout<<"第"<<k+1<<"个员工的喜爱"<<endl;
//Show_arry(*(staff+k),area_number);
}
}
void Point_free(int staff_number,int **staff) // 二级指针释放有问题,有待解决!
{ // 问题解决 ,需要在main函数中,事先对二级指针申请空间
for (int i=0;i<staff_number;i++)
delete []*(staff+i);
delete []staff;
}
void Show(int *area_staff_number,int **staff,int area_number,int staff_number)
{
cout<<"员工数目="<<staff_number<<'\t'<<"区域数目:"<<area_number<<endl;
cout<<endl;
Show_arry(area_staff_number,area_number);
cout<<endl;
cout<<"员工编号 ";
for(int i=0;i<area_number;i++)
cout<<"第"<<i+1<<"个区域 ";
cout<<endl;
for(int k=0;k<staff_number;k++)
{
cout<<" "<<k+1<<'\t'<<" ";
for(int m=0;m<area_number;m++)
cout<<*(*(staff+k)+m)<<'\t'<<" ";
cout<<endl;
}
}
void Show_arry(int*arry,int all)
{
for(int i=0; i<all;i++)
cout<<"第"<<i+1<<"个区域,可容纳员工数为: "<<*(arry+i)<<endl;
}
void Show_List(Staff*List)
{
Staff*head;
if((head=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
head=List;
while(List!=NULL)
{
cout<<"第"<<List->staff_NO+1<<"个员工:"<<endl;
Staff_love *love_head=List->Staff_love1;
while(List->Staff_love1!=NULL)
{
cout<<List->Staff_love1->Area_NO+1<<":"<<List->Staff_love1->Love<<endl;
List->Staff_love1=List->Staff_love1->next;
}
List=List->next;
//List->Staff_love1=love_head;
}
List=head;
}
void Accommodations(Staff*List,int **staff,int *area_staff_number,int area_number,int staff_number)
{
}
//#include"Accommodations.h"
#define Area_number 3
#define Staff_number 10
using namespace std;
typedef struct Staff_love
{
int Area_NO;
int Love;
struct Staff_love*next;
} Staff_love;
typedef struct Staff
{
int staff_NO;
struct Staff_love *Staff_love1;//*staff_love;
struct Staff*next;
} Staff;
typedef struct Area_sselect
{
int Areo_NO;
}
void Staff_number_allocation(int area_number,int staff_number,int *area_staff_number);
void Staff_love_statistics(int area_number,int staff_number,int **staff);
void Show_arry(int*arry,int all);
void Point_free(int staff_number,int **staff);
void Show(int *area_staff_number,int **staff,int area_number,int staff_number);
Staff* Create_list(int staff_number,int area_number,int **staff);
Staff_love* Insert(Staff_love*p,int idata,int _NO);
void Show_List(Staff*List);
void Accommodations(Staff*List,int **staff,int *area_staff_number,int area_number,int staff_number);
int main()
{
初始化,模拟输入数据
int *area_staff_number=new int[Area_number]; // 定义每个区域可承受员工的数量
int **staff=new int *[Staff_number]; //定义了一个Staff_number的指向指针的指针,其中每个指针包含了Area_number个int型数据
Staff_number_allocation(Area_number,Staff_number,area_staff_number);
//Show_arry(area_staff_number,Area_number);
Staff_love_statistics(Area_number,Staff_number,staff);
Show(area_staff_number,staff,Area_number,Staff_number);
// 建立链表,并且将每个员工对区域的喜好排序
Staff* List;
if((List=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
List=Create_list(Staff_number,Area_number,staff);
Show_List(List);
//释放空间
delete []area_staff_number;
Point_free(Staff_number,staff);
return 0;
}
Staff_love* Insert(Staff_love*p,int idata,int _NO)
{
Staff_love*head,*insert,*form;
if(p==NULL)
{
if((p=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
p->Area_NO=_NO;
p->Love=idata;
p->next=NULL;
return p;
}
if((head=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
head=p;
if((insert=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
insert->Area_NO=_NO;
insert->Love=idata;
insert->next=NULL;
// 如果比head->love小
if(insert->Love>=head->Love)
{
insert->next=head;
head=insert;
}
else
{
if((form=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
while((insert->Love<=p->Love)&&(p->next!=NULL))
{
form=p;
p=p->next;
}
if((p->next!=NULL)||(insert->Love>p->Love) ) // 如果idata不是大于所有的值
{
form->next=insert;
insert->next=p;
}
else /// 当p指向NULL时,此处出错 如何修改?????
{
p->next=insert;
//form->next=insert;
//p->next=NULL;
}
}
return head;
}
Staff* Create_list(int staff_number,int area_number,int **staff)
{
Staff*head,*present,*form;
if((head=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
head->staff_NO=0;
Staff_love*stafflove,*stafflove_head;
if((stafflove=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
stafflove->Love=*(*(staff+0)+0);
stafflove->Area_NO=0;
stafflove->next=NULL;
if((stafflove_head=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
stafflove_head=stafflove;
for(int i=1;i<area_number;i++)
{
stafflove=Insert(stafflove,*(*(staff+0)+i),i);
}
head->Staff_love1=stafflove;
head->next=NULL;
if((form=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
form=head;
for(int k=1;k<staff_number;k++)
{
if((present=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
present->staff_NO=k;
Staff_love*stafflove;
if((stafflove=(Staff_love*)malloc(sizeof(Staff_love)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
stafflove->Love=*(*(staff+k)+0);
stafflove->Area_NO=0;
stafflove->next=NULL;
for(int i=1;i<area_number;i++)
{
stafflove=Insert(stafflove,*(*(staff+k)+i),i);
}
present->Staff_love1=stafflove;
present->next=NULL;
form->next=present;
form=present;
}
//head->Staff_love1=stafflove_head;
return head;
}
void Staff_number_allocation(int area_number,int staff_number,int *area_staff_number)
{
int sub=0;
for(int i=0;i<area_number;i++)
*(area_staff_number+i)=1;
sub=staff_number-area_number;
int k=0;
while(sub!=0)
{
int n=rand()%2;
*(area_staff_number+k)+=n;
sub-=n;
if(k==area_number-1)
k=0;
else
k++;
}
}
void Staff_love_statistics(int area_number,int staff_number,int **staff)
{
//staff=new int *[staff_number];
for(int k=0;k<staff_number;k++)
{
*(staff+k)=new int[area_number];
for(int i=0;i<area_number;i++)
{
*(*(staff+k)+i)=rand()%100;
}
//cout<<"第"<<k+1<<"个员工的喜爱"<<endl;
//Show_arry(*(staff+k),area_number);
}
}
void Point_free(int staff_number,int **staff) // 二级指针释放有问题,有待解决!
{ // 问题解决 ,需要在main函数中,事先对二级指针申请空间
for (int i=0;i<staff_number;i++)
delete []*(staff+i);
delete []staff;
}
void Show(int *area_staff_number,int **staff,int area_number,int staff_number)
{
cout<<"员工数目="<<staff_number<<'\t'<<"区域数目:"<<area_number<<endl;
cout<<endl;
Show_arry(area_staff_number,area_number);
cout<<endl;
cout<<"员工编号 ";
for(int i=0;i<area_number;i++)
cout<<"第"<<i+1<<"个区域 ";
cout<<endl;
for(int k=0;k<staff_number;k++)
{
cout<<" "<<k+1<<'\t'<<" ";
for(int m=0;m<area_number;m++)
cout<<*(*(staff+k)+m)<<'\t'<<" ";
cout<<endl;
}
}
void Show_arry(int*arry,int all)
{
for(int i=0; i<all;i++)
cout<<"第"<<i+1<<"个区域,可容纳员工数为: "<<*(arry+i)<<endl;
}
void Show_List(Staff*List)
{
Staff*head;
if((head=(Staff*)malloc(sizeof(Staff)))==NULL)
{
cout<<"不能分配空间"<<endl;
exit(0);
}
head=List;
while(List!=NULL)
{
cout<<"第"<<List->staff_NO+1<<"个员工:"<<endl;
Staff_love *love_head=List->Staff_love1;
while(List->Staff_love1!=NULL)
{
cout<<List->Staff_love1->Area_NO+1<<":"<<List->Staff_love1->Love<<endl;
List->Staff_love1=List->Staff_love1->next;
}
List=List->next;
//List->Staff_love1=love_head;
}
List=head;
}
void Accommodations(Staff*List,int **staff,int *area_staff_number,int area_number,int staff_number)
{
/ 最关键的程序忘记保存 容我做完再次上传
}