#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#define PLACE_OF_LA D:\\data1.txt
#define PLACE_OF_LB D:\\data2.txt
#define LEN_OF_LA 6
#define LEN_OF_LB 3
#define LEN_OF_LC LEN_OF_LA+LEN_OF_LB
typedef struct elem{
int id;
char name[12];
}ElemType,*TpList;
typedef struct seqlist{
TpList list;
int num;
char *place;
}TSeqList,*pTSeqList;
TpList list_init(int num);
void list_read(pTSeqList p);
void list_sort(TpList p,int len);
TpList list_init(int num)
{
TpList p=(TpList)malloc(sizeof(ElemType)*num);
if(!p)
{
printf("Fail!");
exit(0);
}
return p;
}
void list_read(pTSeqList p)
{
int i;
FILE *pData=NULL;
if((pData=fopen("p->place","r"))==NULL)
{
printf("fail to open!");
return;
}
for(i=0;i<p->num;i++)
{
if(fscanf(pData,"%d",&(((p->list)+i)->id))==EOF)
{
printf("fail to read!");
return ;
}
if(fscanf(pData,"%s",(((p->list)+i)->name))==EOF)
{
printf("fail to read!");
return ;
}
}
fclose(pData);
return ;
}
void list_sort(TpList p,int len)
{
int i,j,id;
char *temp;
for(i=0;i<len-1;i++)
{
for(j=i;j<len;j++)
{
if((p+i)->id>(p+j)->id)
{
id=(p+i)->id;
(p+i)->id=(p+j)->id;
(p+j)->id=id;
strcpy(temp,(p+i)->name);
strcpy((p+i)->name,(p+j)->name);
strcpy((p+j)->name,(p+i)->name);
}
}
}
return ;
}
int main()
{
int i;
pTSeqList pa,pb;
TpList pc;
pa->num=LEN_OF_LA;
pb->num=LEN_OF_LB;
pa->place="PLACE_OF_LA";
pb->place="PLACE_OF_LB";
pa->list=list_init(pa->num);
pb->list=list_init(pb->num);
pc=list_init(LEN_OF_LC);
list_read(pa);
list_read(pb);
for(i=0;i<pa->num;i++)
{
pc[i].id=(pa->list)[i].id;
strcpy(pc[i].name,(pa->list)[i].name);
}
for(i=0;i<pa->num;i++)
{
(pc+i+pa->num)->id=((pb->list)+i)->id;
strcpy((pc+i+pa->num)->name,((pb->list)+i)->name);
}
list_sort(pc,LEN_OF_LC);
printf("id name");
for(i=0;i<LEN_OF_LC;i++)
{
printf("%d\t",pc[i].id);
printf("%s\n",pc[i].name);
}
free(pa);
free(pb);
free(pc);
return 0;
}
追问运行出问题
本回答被网友采纳