落書きノート

ふと自分が気になった事を書いてます

C++で問題を解く

クラスの問題なので、コード量が多い以外は、そんなに難しくないです。ツェラーの公式ぐらいでしょうか。難しいのは。

#ifndef INCLUDED_DATE
#define INCLUDED_DATE

#include <string>
#include <iostream>

class Date {
  int y;
  int m;
  int d;
 public:
  Date();
  Date(int yy, int mm = 1, int dd = 1);

  int year() const { return y; }
  int month() const { return m; }
  int day() const { return d; }

  Date preceding_day() const;

  std::string to_string() const;
  int day_of_week() const;
};

std::ostream& operator<<(std::ostream& s, const Date& x);
std::istream& operator>>(std::istream& s, Date& x);

#endif


#include <ctime>
#include <sstream>
#include <iostream>
#include "q11-6.h"

using namespace std;

Date::Date(){
  time_t current = time(NULL);
  struct tm *local = localtime(&current);

  y = local->tm_year + 1900;
  m = local->tm_mon + 1;
  d = local->tm_mday;
}

Date::Date(int yy, int mm, int dd){
  y = yy;
  m = mm;
  d = dd;
}

Date Date::preceding_day() const {
  int dmax[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  Date temp = *this;

  if(temp.d > 1)
    temp.d--;
  else {
    if(--temp.m < 1){
      temp.y--;
      temp.m = 12;
    }
    temp.d = dmax[temp.m - 1];
  }
  return temp;
}

string Date::to_string() const {
  ostringstream s;
  s << y << "年" << m << "月" << d << "日";
  return s.str();
}

int Date::day_of_week() const {
  int yy = y;
  int mm = m;
  
  if(mm == 1 || mm == 2){
    yy--;
    mm += 12;
  }

  return (yy + yy / 4 - yy / 100 + yy / 400 + (13 * mm + 8) / 5 + d) % 7;
}

ostream& operator<<(ostream& s, const Date& x){
  return s << x.to_string();
}

istream& operator>>(istream& s, Date& x){
  int yy, mm, dd;
  char ch;

  s >> yy >> ch >> mm >> ch >> dd;
  x = Date(yy, mm, dd);
  return s;
}


#include <iostream>
#include "q11-6.h"

using namespace std;

int main(void){
  Date xday;
  const Date birthday(1963, 11, 18);
  Date day[3];

  cout << "xdayは : "; cin >> xday;
  cout << "その日の前日は" << xday.preceding_day() << "です。" << endl;
  cout << "birthday = " << birthday << endl;
  for(int i = 0; i < 3; i++)
    cout << "day[" << i << "]の文字列表現 : \"" << day[i].to_string() << "\"" << endl;
}
#ifndef INCLUDED_HUMAN
#define INCLUDED_HUMAN

#include <string>
#include <iostream>
#include "q11-6.h"

class Human {
  std::string full_name;
  int height;
  int weight;
  Date birth;
 public:
 Human(std::string name, int h, int w)
   : full_name(name), height(h), weight(w){}
  
  std::string name() const { return full_name; }
  int get_height() const { return height; }
  int get_weight() const { return weight; }
  Date get_birth() const { return birth; }
  
  void grow_fat(int dw);
  void slim_off(int dw);
};

#endif


#include <string>
#include "q11-7.h"

using namespace std;

Human::Human(string name, int h, int w){
  full_name = name;
  height = h;
  weight = w;
}

void Human::grow_fat(int dw){
  weight += dw;
}

void Human::slim_off(int dw){
  weight -= dw;
}
#ifndef INCLUDED_CAR
#define INCLUDED_CAR

#include <iostream>
#include <cmath>
#include <string>
#include "q11-6.h"

class Car{
  std::string name;
  std::string number;
  int width;
  int length;
  int height;
  double x;
  double y;
  double sfc;
  double tankage;
  double fuel;
  Date purchase;
  
public:
 Car(std::string n, std::string no, int w, int l, int h, double s, double t, double f, Date pd) : name(n), number(no), width(w), length(l), height(h), sfc(s), tankage(t), fuel(f <= tankage ? f : tankage), x(0.0), y(0.0), purchase(pd) {}

  double getX() const { return this->x; }
  double getY() const { return this->y; }
  double getFuel() const { return this->fuel; }
  Date getPurchase() const { return this->purchase; }
  
  void print_spec() {
    std::cout << "名前 = " << this->name << std::endl;
    std::cout << "ナンバー = " << this->number << std::endl;
    std::cout << "車幅 = " << this->width << std::endl;
    std::cout << "車長 = " << this->length << std::endl;
    std::cout << "車高 = " << this->height << std::endl;
    std::cout << "燃費 = " << this->sfc << std::endl;
    std::cout << "タンク容量 = " << this->tankage << std::endl;
  }

  bool move(double dx, double dy){
    double dist = sqrt(dx * dx + dy * dy);
    double f = dist / sfc;
    if(f > fuel)
      return false;
    else {
      fuel -= f;
      this->x += dx;
      this->y += dy;
      return true;
    }
  }

  void refuel(double df){
    if(df > 0) {
      fuel += df;
      if(fuel > tankage)
        fuel = tankage;
    }
  }
};

int main(void){
  std::string name, number;
  int width, length, height;
  double sfc, tankage, fuel;

  std::cout << "車のデータを入力せよ。" << std::endl;
  std::cout << "名前 : "; std::cin >> name;
  std::cout << "ナンバー : "; std::cin >> number;
  std::cout << "車幅 : "; std::cin >> width;
  std::cout << "車長 : "; std::cin >> length;
  std::cout << "車高 : "; std::cin >> height;
  std::cout << "燃費 : "; std::cin >> sfc;
  std::cout << "タンク容量 : "; std::cin >> tankage;
  std::cout << "ガソリン量 : "; std::cin >> fuel;

  Car myCar(name, number, width, length, height, sfc, tankage, fuel);
  myCar.print_spec();

  while(true){
    std::cout << "現在地(" << myCar.getX() << ", " << myCar.getY() << ")" << std::endl;
    std::cout << "残り燃料 : " << myCar.getFuel() << "リットル" << std::endl;
    std::cout << "移動[0...No/1...Yes] : ";
    int move;
    std::cin >> move;
    if(move == 0) break;

    double dx, dy;
    std::cout << "X方向の移動距離 : "; std::cin >> dx;
    std::cout << "Y方向の移動距離 : "; std::cin >> dy;
    if(!myCar.move(dx, dy))
      std::cout << "\a燃料が足りません!\n";
  }

  return 0;
}

#endif