九度online judge-开门人和关门人

题目来源:http://ac.jobdu.com/problem.php?pid=1013

我的代码:已ac

#include <iostream>

#include <string>

#include <stdio.h>

using namespace std;

class Employee{

//   string s;

   char* s;

   int hour1,hour2;

   int minute1,minute2;

   int second1,second2;

public:

    Employee(char* str=0,int h1=0,int s1=0,int m1=0,int h2=0,int s2=0,int m2=0):s(str),hour1(h1),minute1(s1),second1(m1),hour2(h2),minute2(s2),second2(m2){}

   void print(){

       cout<<s;

    }

   booloperator> (constEmployee& a){

       if(this->hour1>a.hour1)

           return true;

       else if(this->hour1==a.hour1){

           if(this->minute1>a.minute1)

               return true;

           else if(this->minute1==a.minute1){

               if(this->second1>a.second1)

                   return true;

               else if(this->second1<a.second1)

                   return false;

            }

           else if(this->minute1<a.minute1)

               return false;

        }

       else if(this->hour1<a.hour1)

           return false;

        

       return true;

    }

   booloperator< (constEmployee& a){

       if(this->hour2<a.hour2)

           return true;

       else if(this->hour2==a.hour2){

           if(this->minute2<a.minute2)

               return true;

           else if(this->minute2==a.minute2){

               if(this->second2<a.second2)

                   return true;

               else if(this->second2>a.second2)

                   return false;

            }

           else if(this->minute2>a.minute2)

               return false;

        }

       else if(this->hour2>a.hour2)

           return false;

        

       return true;

    }

};

Employee employee[100];

int main(){

   int n;

   int m;


//    string tstr;

   int th1,ts1,tm1;

   int th2,ts2,tm2;

   scanf("%d",&n);

    

   while(n--){

       int i=0;

       scanf("%d",&m);

       while(i<m){

      //          char tstr[15];

           char* tstr=newchar[15];

           scanf("%s %2d:%2d:%2d %2d:%2d:%2d",tstr,&th1,&ts1,&tm1,&th2,&ts2,&tm2);

           employee[i++]=*(newEmployee(tstr,th1,ts1,tm1,th2,ts2,tm2));

        }

       int j=0;

       Employee * first=employee;

       Employee * last=employee;

        for(;j<m;j++){   //比较这一天内m条记录中的最早到达者和最晚离开者,两个函数分别返回类对象指针

           if(*first>employee[j])

                first=&employee[j];

           if(*last<employee[j])

                last=&employee[j];

        }

        first->print();

       cout<<" ";

        last->print();

       cout<<endl;

    }

   return 0;

}


在本题中我碰到了两个问题:

1.string和char* 之间的联系  http://blog.csdn.net/cogbee/article/details/8931838

   注意到类Tree的数据成员用的是 char*而不是string,具体他们之间的转化会出现什么问题下次会增加

2.指针与内存空间问题    http://blog.csdn.net/goodlixueyong/article/details/6068631

首先:

分配数组空间语句放在while循环外面时,运行结果发现每次新增加一天记录后之前的记录中的证件号码均被修改为新增加记录的值,说明多个类对象的s指针指向了同一块内存空间,即均指向了tstr数组空间,所以应在每次新建类对象之前,动态为其分配一块空间,将分配数组空间语句放到循环内。

另外:

下面的while循环,循环内有个分配内存的语句。

while(1)

 {

      char command[100];

    printf("请输入命令:>");

      memset(command,0,100);
      gets(command);

      printf("%s/n",command);

}

那么它是每次都分配内存,还是只分配一次内存呢?

      经过验证发现,每当程序执行到右大括号时,command指向的内存都被释放掉了,然后重新执行到char command[100]时,系统又重新给程序分配了内存。但是需要注意的是,在程序每次运行过程中,程序使用的内存的其实地址都是一样的。也就是说每次分配了相同位置的内存。

所以不能这样在while内分配内存,改用new动态分配堆空间,必须手动释放该空间才可以,而不是遇到大括号就释放,只要不手动释放,下次分配的空间地址肯定会不同。


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值