Copyright (c) 2002, 2006 by ctu_85
All Rights Reserved.
*/
#include \"stdio.h\"
#include \"string.h\"
#include \"malloc.h\"
#define idlength 15
struct Person
{
char id[idlength+1];
char Log[10];
char Leave[10];
};
struct Day
{
Person *work;
int number;
};
struct Day *CreateDayLog(int);
struct Person *CreatePersonLog(int);
int Process(struct Person *,char *);
struct Person *Earliest(struct Day*);
struct Person *Latiest(struct Day*);
int main()
{
int i,j,k;
Day *process;
Person *p;
printf(\"\\nPlease Enter The Days:\");
scanf(\"%d\",&i);
process=CreateDayLog(i);
for(j=0;j<i;j++)
{
printf(\"\\nIn %dth day:\\n\",j+1);
if((process+j)->number!=0)
{
p=Earliest(process+j);
printf(\"The earliest user and his info is:\\n\");
printf(\"%s,%s,%s\",p->id,p->Log,p->Leave);
printf(\"\\n\");
p=Latiest(process+j);
printf(\"The latiest user and his info is:\\n\");
printf(\"%s,%s,%s\",p->id,p->Log,p->Leave);
}
else
printf(\"There is no user that day!\\n\");
}
return 1;
}
struct Day *CreateDayLog(int i)
{
int j,k;
struct Day *day=(struct Day *)malloc(i*sizeof(struct Day));
for(j=0;j<i;j++)
{
printf(\"\\nThe number of users of the %dth day\",j+1);
scanf(\"%d\",&k);
(day+j)->number=k;
(day+j)->work=CreatePersonLog(k);
}
return day;
}
struct Person *CreatePersonLog(int i)
{
int j;
Person *user=(struct Person*)malloc(sizeof(struct Person)*i);
char detail[idlength+20],*t;
gets(detail);
for(j=0;j<i;j++)
{
printf(\"\\nThe deatails of the %dth person\\n\",j+1);
t=gets(detail);
Process(user+j,t);
}
return user;
}
int Process(struct Person *p,char *c)
{
int i=0,j=0,k=0,status=0;
char time[8];
for(;*c!=\'\\0\';c++)
{
if(*c==\' \')
{
if(status==0)
{
p->id=\'\\0\';
status++;
continue;
}
if(status==1)
status++;
}
if(*c!=\' \')
{
if(status==0) /* Now meets the id */
{
p->id=*c;
i++;
}
else
if(status==1) /*Now meets the log time */
{
p->Log[j]=*c;
j++;
}
else
if(status==2) /*Now meets the leave time */
{
p->Leave[k]=*c;
k++;
}
}
}
p->id=p->Log[j]=p->Leave[k]=\'\\0\';
}
struct Person *Earliest(struct Day* day)
{
int minhour=25,minminute=61,minsecond=61;
int i,early,temphour,tempminu,tempsec;
struct Person *p=day->work,*result;
result=p;
for(i=0;i<day->number;i++,p++)
{
temphour=(p->Log[0]-\'0\')*10+(p->Log[1]-\'0\');
tempminu=(p->Log[3]-\'0\')*10+(p->Log[4]-\'0\');
tempsec=(p->Log[6]-\'0\')*10+(p->Log[7]-\'0\');
if(temphour<minhour)
{
minhour=temphour;
minminute=tempminu;
minsecond=tempsec;
early=i;
}
else if(temphour==minhour&&tempminu<minminute)
{
minminute=tempminu;
minsecond=tempsec;
early=i;
}
else if(temphour==minhour&&tempminu==minminute&&tempsec<minsecond)
{
minsecond=tempsec;
early=i;
}
}
return result+early;
}
struct Person *Latiest(struct Day* day)
{
int maxhour=-23,maxminute=-59,maxsecond=-59;
int i,late,temphour,tempminu,tempsec;
struct Person *p=day->work,*result;
result=p;
for(i=0;i<day->number;i++,p++)
{
temphour=(p->Leave[0]-\'2\')*10+(p->Leave[1]-\'3\');
tempminu=(p->Leave[3]-\'5\')*10+(p->Leave[4]-\'9\');
tempsec=(p->Leave[6]-\'5\')*10+(p->Leave[7]-\'9\');
if(temphour>maxhour)
{
maxhour=temphour;
maxminute=tempminu;
maxsecond=tempsec;
late=i;
}
else if(temphour==maxhour&&tempminu>maxminute)
{
maxminute=tempminu;
maxsecond=tempsec;
late=i;
}
else if(temphour==maxhour&&tempminu==maxminute&&tempsec>maxsecond)
{
maxsecond=tempsec;
late=i;
}
}
return result+late;
}作者: 地理初学者 时间: 06-11-27 14:13
由MIPS指令想到数组与指针
对程序员来说,最重要的莫过于掌握指针的用法了。
先来看下面两个程序:
程序1 程序2
clear1(int array[], int size) clear2(int *array[0]; int size)
{ {
int i; int *p;
for (i=0;i<size;i=i+1) for(p=&array[0];p<&array[size];p=p+1)