Post
by jhonny_yang » Thu Dec 16, 2004 5:23 pm
I almost get grazy about this easy problem
[c]
#include <stdio.h>
#include <string.h>
typedef struct{
char name[80];
char country[80];
char department[80];
char homephone[80];
char workphone[80];
char campusbox[80];
}DataStruct;
void Swap(DataStruct *a, DataStruct *b)
{
DataStruct temp;
strcpy(temp.campusbox,a->campusbox);
strcpy(temp.country,a->country);
strcpy(temp.department,a->department);
strcpy(temp.homephone,a->homephone);
strcpy(temp.name,a->name);
strcpy(temp.workphone,a->workphone);
strcpy(a->campusbox,b->campusbox);
strcpy(a->country,b->country);
strcpy(a->department,b->department);
strcpy(a->homephone,b->homephone);
strcpy(a->name,b->name);
strcpy(a->workphone,b->workphone);
strcpy(b->campusbox,temp.campusbox);
strcpy(b->country,temp.country);
strcpy(b->department,temp.department);
strcpy(b->homephone,temp.homephone);
strcpy(b->name,temp.name);
strcpy(b->workphone,temp.workphone);
}
int partition(DataStruct *s, int l, int h)
{
int i;
int p;
int firsthigh;
p = h;
firsthigh = l;
for (i=l; i<h; i++)
if (strcmp(s.name,s[p].name)<0){
Swap(&s,&s[firsthigh]);
firsthigh ++;
}
Swap(&s[p],&s[firsthigh]);
return(firsthigh);
}
void qsort(DataStruct *s, int l, int h)
{
int p;
if ((h-l)>0) {
p = partition(s,l,h);
qsort(s,l,p-1);
qsort(s,p+1,h);
}
}
int main()
{
int n,i,j,k;
DataStruct Buffer[80];
char buff[4200],*temp,department[4000];
scanf("%d\n",&n);
k=0;
while (gets(buff)){
strcpy(department,buff);
while (temp=gets(buff)){
if (strlen(buff)==0)break;
strcpy(Buffer[k].department,department);
i=0;j=0;
while (buff!=','){
Buffer[k].name[j++]=buff[i++];
}
Buffer[k].name[j++]=' ';++i;
while (buff!=','){
Buffer[k].name[j++]=buff[i++];
}
Buffer[k].name[j++]=' ';++i;
while (buff!=','){
Buffer[k].name[j++]=buff[i++];
}
Buffer[k].name[j++]=0;++i;
j=0;
while (buff!=','){
Buffer[k].country[j++]=buff[i++];
}
Buffer[k].country[j++]=0;++i;
j=0;
while (buff!=','){
Buffer[k].homephone[j++]=buff[i++];
}
Buffer[k].homephone[j++]=0;++i;
j=0;
while (buff!=','){
Buffer[k].workphone[j++]=buff[i++];
}
Buffer[k].workphone[j++]=0;++i;
j=0;
while (buff){
Buffer[k].campusbox[j++]=buff[i++];
}
Buffer[k].campusbox[j++]=0;++i;
++k;
}
if (temp==0)break;
}
qsort(Buffer,0,k-1);
for (i=0;i<k;i++){
printf("----------------------------------------\n");
printf("%s\n",Buffer.name);
printf("%s\n",Buffer[i].country);
printf("Department: %s\n",Buffer[i].department);
printf("Home Phone: %s\n",Buffer[i].homephone);
printf("Work Phone: %s\n",Buffer[i].workphone);
printf("Campus Box: %s\n",Buffer[i].campusbox);
}
return 0;
}
[/c]