※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

1月23日提出のソフトウェア演習課題

まずは自分で実装してみる。

STL使えの一言で済みそうですが、あえて自己言及構造体の考えを発展させて自分で組んだのが下のコード。

#include <iostream>
#include <string>
 
class date{
private:
  int year;
  unsigned int month, day;
  const static unsigned int num_of_days[];
public:
  bool is_valid(){
    return (month < 13 && day <= num_of_days[month]);
  };
  bool operator<(date &obj);
  friend std::istream &operator>>(std::istream &stream, date &obj);
  friend std::ostream &operator<<(std::ostream &stream, date &obj);
};
 
const unsigned int date::num_of_days[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
#if 0 /*コメントの代わり*/
bool date::operator<(date &obj)
{
  if(year != obj.year)
    return (year < obj.year);
  else if(month != obj.month)
    return (month < obj.month);
  else
    return (day < obj.day);
}
#endif
 
inline bool date::operator<(date &obj)
{
  return ((year << 9) + (month << 5) + day)
    < ((obj.year << 9) + (obj.month << 5) +obj.day);
}
 
std::istream &operator>>(std::istream &stream, date &obj)
{
  int y, m, d;
  //char c;
  if(!(stream >> y >> m >> d))
    return stream;
  else
    {
      obj.year = y;
      obj.month = m;
      obj.day = d;
    }
  return stream;
}
 
std::ostream &operator<<(std::ostream &stream, date &obj)
{
  stream << obj.year << ' ' << obj.month << ' ' << obj.day;
  return stream;
}
 
class person{
private:
  std::string name; // name
public:
  date birthday; // birthday
  person *next;
  person();
  friend std::istream &operator>>(std::istream &stream, person &obj);
  friend std::ostream &operator<<(std::ostream &stream, person &obj);
};
 
person::person()
{
  next = NULL;
}
 
std::istream &operator>>(std::istream &stream, person &obj)
{
  std::cout << "Name:";
  stream >> obj.name;
  std::cout << "Birthday:";
  stream >> obj.birthday;
  return stream;
}
 
std::ostream &operator<<(std::ostream &stream, person &obj)
{
  stream << "Name=" << obj.name << ", birthday=" << obj.birthday << std::endl;
  return stream;
}

ここから先がlistの実装になる。

class list
{
private:
  person *start;
public:
  list();
  ~list();
  void insert(person *add);
  friend std::istream &operator>>(std::istream &stream, list &obj);
  friend std::ostream &operator<<(std::ostream &stream, list &obj);
};
 
list::list()
{
  start = NULL;
}
 
list::~list()
{
  person *i, *j=start;
  while(i = j)
    {
      j = i->next;
      delete i;
    }
}
 
void list::insert(person *add)
{
  //first element
  if(!start)
    {
      start = add;
      return;
    }
 
  //top element
  if(add->birthday < start->birthday)
    {
      add->next = start;
      start = add;
      return;
    }
 
  person *seek;
  for(seek=start; seek->next != NULL; seek=seek->next)
    {
      if(add->birthday < seek->next->birthday)
        {
          add->next = seek->next;
          seek->next = add;
          return;
        }
    }
  seek->next = add;
  return;
}
 
std::istream &operator>>(std::istream &stream, list &obj)
{
  person *t;
 
  while(t = new person)
    {
      if( !(std::cin >> *t))
        {
          delete t;
          break;
        }
      if(!t->birthday.is_valid())
        {
          std::cerr << "Date is invalid." << std::endl;
          delete t;
        }
      else
        obj.insert(t);
    }
  return stream;
}
 
std::ostream &operator<<(std::ostream &stream, list &obj)
{
  person *i = obj.start;
  while(i)
    {
      stream << *i;
      i = i->next;
    }
  return stream;
}
 
inline void test()
{
  person P;
  std::cin >> P;
  std::cout << P;
  std::cin >> P;
  std::cout << P;
}
 
int main()
{
  //test();
  list L;
  std::cin >> L;
  std::cout << L;
  return 0;
}
|新しいページ|検索|ページ一覧|RSS|@ウィキご利用ガイド | 管理者にお問合せ
|ログイン|