hi
I am a student in computer science and I'm having some trouble with our latest assignment. (Part of) the assignment is to insert elements into a sorted list.
The elements are some kind of interval-type objects
Every time when I insert an object I want it to be inserted in the right position. if there is an overlapping with another object in the list I dont want to insert it.
I wrote a small testprogram to test this part of the code, first for regular integers, then for the objects I have to insert.
For intergers I had (almost) no problems but with my own objects I am getting strange results. I am staring at my code for almost half a day, rewriting it over and over again. but I just cant see what I am doing wrong.
here are the relevant parts of the code:
the things I have to insert in the list are from the ScheduleItem class.
declarations:
Code:
// part of scheduleitem.h
typedef unsigned long int ulint;
class ScheduleItem
{ public:
ScheduleItem(ulint, ulint, std::string);
ScheduleItem(std::istream&);
ulint getbegin() const;
ulint getend() const;
std::string getdescription() const;
void setbegin(ulint);
void setend(ulint);
void setdescription(std::string);
private:
ulint begin_;
ulint end_;
std::string descr_;
};
std::ostream& operator<<(std::ostream&, const ScheduleItem&);
bool operator<(const ScheduleItem&, const ScheduleItem&);
bool operator>(const ScheduleItem&, const ScheduleItem&);
bool operator==(const ScheduleItem&, const ScheduleItem&);
definitions:
Code:
// small part of scheduleitem.cc
bool operator<(const ScheduleItem& si1, const ScheduleItem& si2)
{ si1.getend() < si2.getbegin();
}
bool operator>(const ScheduleItem& si1, const ScheduleItem& si2)
{ si1.getbegin() > si2.getend();
}
bool operator==(const ScheduleItem& si1, const ScheduleItem& si2)
{ si1.getbegin() == si2.getbegin() && si1.getend() == si2.getend() && si1.getdescription() == si2.getdescription();
}
I am quite sure I defined operator< and operator> right.
a<b if a ends before b starts
a>b if a starts after b ends
here is the class in which I have to insert the scheduleitems.
declarations:
Code:
// most of schedule.h
class Schedule
{ public:
friend std::ostream& operator<<(std::ostream&, const Schedule&);
Schedule();
Schedule(std::istream&);
bool insert_item(const ScheduleItem&);
bool remove_item(const ScheduleItem&);
bool check_item(ScheduleItem&, const ScheduleItem&, ScheduleItem&);
bool check_item(ScheduleItem&, const ScheduleItem&, ScheduleItem&, const std::list<Schedule>&);
private:
std::list<ScheduleItem> items_;
};
definitions:
Code:
// part of schedule.cc
bool Schedule::insert_item(const ScheduleItem& si)
{ for (std::list<ScheduleItem>::iterator i = items_.begin(); ; i++)
{ if ((i == items_.end()) || (*i > si))
{ items_.insert(i, si);
return 1;
}
else if (!(*i < si))
return 0;
}
}
bool Schedule::remove_item(const ScheduleItem& si)
{ for (std::list<ScheduleItem>::iterator i = items_.begin(); ; i++)
{ if (i == items_.end())
return 0;
else if (*i == si)
{ items_.erase(i);
return 1;
}
}
}
std::ostream& operator<<(std::ostream& os, const Schedule& s)
{ for (std::list<ScheduleItem>::const_iterator i = s.items_.begin(); i != s.items_.end(); i++)
os << *i;
}
if I write a small testing-loop in which I insert scheduleitems into a schedule it keeps inserting them at the front!
also, the remove-part always removes the first item from the schedule in stead of the right one.
one other small thing: if I change the last line ( os << *i; ) with os << *i << std::endl; the program crashes at runtime. I also have no idea why it does that...
thanks in advance for your help, and I hope that someone can point me in the right direction.
ken