본문 바로가기
카테고리 없음

명품 C++ 프로그래밍 3장 오픈챌린지, 실습문제 일부만

by 레피스토 2024. 11. 3.
반응형

오픈챌린지

// Exp.h
#ifndef CHAP1_EXP_EXP_H_
#define CHAP1_EXP_EXP_H_

class Exp {
public:
  Exp();
  Exp(int base);
  Exp(int base, int exp);

  int GetBase() const { return base_; }
  int GetExp() const { return exp_; }
  int GetValue() const;
  bool Equals(const Exp& exp) const;

private:
  int base_;
  int exp_;
};


#endif

//Exp.cc
#include "Exp.h"
#include <cmath>

Exp::Exp() :base_(1), exp_(1) {}
Exp::Exp(int base) :base_(base), exp_(1) {}
Exp::Exp(int base, int exp) :base_(base), exp_(exp) {}

int Exp::GetValue() const {
  return static_cast<int>(std::pow(base_, exp_));
}
bool Exp::Equals(const Exp& exp) const {
  return (this->GetValue() == exp.GetValue()) ? true : false;
}

// main.cc
#include <iostream>
#include "Exp.h"

int main() {
  Exp a{ 3,2 };
  Exp b{ 9 };
  Exp c{};

  std::cout << a.GetValue() << ' ' << b.GetValue() << ' ' << c.GetValue() << '\n';
  std::cout << "a's base: " << a.GetBase() << ", a's expoenet: " << a.GetExp() << '\n';

  if (a.Equals(b))
    std::cout << "same" << "\n";
  else
    std::cout << "not same" << '\n';

  
}

실습문제))

// quiz1
#include <iostream>

class Tower {
public:
  Tower();
  Tower(int height);
  int GetHeight() const { return height_; }
private:
  int height_;
};
Tower::Tower() :height_(1) {}
Tower::Tower(int height) :height_(height) {}

int main() {
  Tower my_tower{};
  Tower seoul_tower{ 100 };
  std::cout << "height: " << my_tower.GetHeight() << "m \n";
  std::cout << "height: " << seoul_tower.GetHeight() << "m \n";
}

// quiz2
// string 객체를 stringstream 객체로 바꾼후 그 문자열을 읽어들이면, 자동적으로 문자열을 숫자로 바꾸고 '/'같은 문자로 delimiter가 형성된다.
#include <iostream>
#include <sstream>
#include <string>

class Date {
public:
  Date(int year, int month, int day);
  Date(std::string date);
  void Show() const;
  int GetYear() const;
  int GetMonth() const;
  int GetDay() const;

private:
  int year_;
  int month_;
  int day_;
};
Date::Date(int year, int month, int day) :year_(year), month_(month), day_(day) {}
Date::Date(std::string date) {
  std::stringstream ss{ date };
  char delimiter{};

  ss >> year_ >> delimiter >> month_ >> delimiter >> day_;
}
void Date::Show() const { std::cout << year_ << "년" << month_ << "월" << day_ << "일" << "\n"; }
int Date::GetYear() const { return year_; }
int Date::GetMonth() const { return month_; }
int Date::GetDay() const { return day_; }

int main() {
  Date birth{ 2014,3,20 };
  Date independence_day{ "1945/8/15" };
  independence_day.Show();
  std::cout << birth.GetYear() << ", " << birth.GetMonth() << ", " << birth.GetDay() << "\n";
}

// quiz3
#include <iostream>
#include <string>

class Account {
public:
  Account(std::string name, int id, int balance);
  void Deposit(int deposit_amount);
  int Withdraw(int withdraw_amount);
  std::string GetOwner() const;
  int Inquiry() const;

private:
  std::string name_;
  int id_;
  int balance_;
};
Account::Account(std::string name, int id, int balance)
  :name_(name), id_(id), balance_(balance) {}
void Account::Deposit(int deposit_amount) {
  balance_ += deposit_amount;
}
int Account::Withdraw(int withdraw_amount) {
  balance_ -= withdraw_amount;
  return withdraw_amount;
}
std::string Account::GetOwner() const {
  return name_;
}
int Account::Inquiry() const {
  return balance_;
}
int main() {
  Account a{ "kitae",1,5000 };
  a.Deposit(50000);
  std::cout << a.GetOwner() << "의 잔액은 " << a.Inquiry() << '\n';
  int money{ a.Withdraw(20000) };
  std::cout << a.GetOwner() << "의 잔액은 " << a.Inquiry() << '\n';

}

// quiz4
#include <iostream>
#include <string>

class CoffeeMachine {
public:
  CoffeeMachine(int coffee, int water, int sugar);
  void DrinkEspresso();
  void DrinkAmericano();
  void DrinkSugarCoffee();
  void Show() const;
  void Fill();

private:
  int coffee_;
  int water_;
  int sugar_;
};
CoffeeMachine::CoffeeMachine(int coffee, int water, int sugar) :coffee_(coffee), water_(water), sugar_(sugar) {}
void CoffeeMachine::DrinkEspresso() {
  coffee_ -= 1;
  water_ -= 1;
}
void CoffeeMachine::DrinkAmericano() {
  coffee_ -= 1;
  water_ -= 2;
}
void CoffeeMachine::DrinkSugarCoffee() {
  coffee_ -= 1;
  water_ -= 2;
  sugar_ -= 1;
}
void CoffeeMachine::Show() const {
  std::cout << "커피 머신 상태, 커피:" << coffee_ << " 물:" << water_ << "\t설탕:" << sugar_ << "\n";
}
void CoffeeMachine::Fill() {
  coffee_ = water_ = sugar_ = 10;
}

int main() {
  CoffeeMachine java(5, 10, 3);
  java.DrinkEspresso();
  java.Show();
  java.DrinkAmericano();
  java.Show();
  java.DrinkSugarCoffee();
  java.Show();
  java.Fill();
  java.Show();

}

// quiz5
#include <iostream>
#include <string>
#include <cstdlib>

class Random {
public:
  Random();
  int Next() const;
  int NextInRange(int start, int end) const;
};
Random::Random() {
  srand(static_cast<unsigned int>(time(0)));
}
int Random::Next() const {
  return rand();
}
int Random::NextInRange(int start, int end) const {
  return start + rand() % (end - start+1);
}
int main() {
  Random r;
  std::cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10개--\n";
  for (int i{ 0 }; i < 10; i++) {
    int n{ r.Next() };
    std::cout << n << ' ';
  }
  std::cout << "\n\n-- 2에서 4까지의 랜덤 정수 10개 --\n";
  for (int i{ 0 }; i < 10; i++) {
    int n{ r.NextInRange(2,4) };
    std::cout << n << ' ';
  }
  std::cout << '\n';

}

// quiz6
#include <iostream>
#include <string>
#include <cstdlib>

class EvenRandom {
public:
  EvenRandom();
  int Next() const;
  int NextInRange(int start, int end) const;
};
EvenRandom::EvenRandom() {
  srand(static_cast<unsigned int>(time(0)));
}
int EvenRandom::Next() const {
  while (true) {
    int num{ rand() };
    if (num % 2 == 0)
      return num;
  }
}
int EvenRandom::NextInRange(int start, int end) const {
  while (true) {
    int num{ start + rand() % (end - start + 1)};
    if (num % 2 == 0)
      return num;
  }
}
int main() {
  EvenRandom r;
  std::cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10개--\n";
  for (int i{ 0 }; i < 10; i++) {
    int n{ r.Next() };
    std::cout << n << ' ';
  }
  std::cout << "\n\n-- 2에서 10까지의 랜덤 정수 10개 --\n";
  for (int i{ 0 }; i < 10; i++) {
    int n{ r.NextInRange(2,10) };
    std::cout << n << ' ';
  }
  std::cout << '\n';

}

// quiz7
#include <iostream>
#include <string>
#include <cstdlib>

class SelectableRandom {
public:
  SelectableRandom(std::string parity);
  int Next() const;
  int NextInRange(int start, int end) const;
private:
  bool is_even;
};
SelectableRandom::SelectableRandom(std::string parity) {
  srand(static_cast<unsigned int>(time(0)));
  if (parity == "짝수")
    is_even = true;
  else
    is_even = false;

}
int SelectableRandom::Next() const {
  if (is_even) {
    while (true) {
      int num{ rand() };
      if (num % 2 == 0)
        return num;
    }
  }
  else {
    while (true) {
      int num{ rand() };
      if (num % 2 == 1)
        return num;
    }
  }
}

int SelectableRandom::NextInRange(int start, int end) const {
  if (is_even) {
    while (true) {
      int num{ start + rand() % (end - start + 1) };
      if (num % 2 == 0)
        return num;
    }
  }
  else {
    while (true) {
      int num{ start + rand() % (end - start + 1) };
      if (num % 2 == 1)
        return num;
    }
  }
}
int main() {
  SelectableRandom r1{ "짝수" };
  std::cout << "-- 0에서 " << RAND_MAX << "까지의 짝수 랜덤 정수 10개--\n";
  for (int i{ 0 }; i < 10; i++) {
    int n{ r1.Next() };
    std::cout << n << ' ';
  }

  SelectableRandom r2{ "홀수" };
  std::cout << "\n\n-- 2에서 9까지의 홀수 랜덤 정수 10개 --\n";
  for (int i{ 0 }; i < 10; i++) {
    int n{ r2.NextInRange(2,9) };
    std::cout << n << ' ';
  }
  std::cout << '\n';

}

// quiz8
#include <iostream>
#include <string>
#include <cstdlib>

class Integer {
public:
  Integer(int num) :num_(num) {}
  Integer(std::string num) :num_(std::stoi(num)) {}
  int Get() const { return num_; }
  void Set(int num) { num_ = num; }
  bool IsEven() const { return (num_ % 2 == 0) ? true : false; }

private:
  int num_;
};
int main() {
  Integer n{ 30 };
  std::cout << n.Get() << ' ';
  n.Set(50);
  std::cout << n.Get() << ' ';

  Integer m{ "300" };
  std::cout << m.Get() << ' ';
  std::cout << m.IsEven();

}

// quiz9
#include <iostream>
#include <string>
#include <cstdlib>

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&);               \
  void operator=(const TypeName&)
  
class Oval {
public:
  Oval(int width, int height);
  Oval();
  ~Oval();
  int GetWidth() const;
  int GetHeight() const;
  void Set(int width, int height);
  void Show() const;

private:
  int width_;
  int height_;
  DISALLOW_COPY_AND_ASSIGN(Oval);
};
Oval::Oval(int width, int height) :width_(width), height_(height) {}
Oval::Oval() :width_(1), height_(1) {}
Oval::~Oval() {
  std::cout << "Oval 소멸 : width = " << width_ << ", height = " << height_ << "\n";
}
int Oval::GetWidth() const { return width_; }
int Oval::GetHeight() const { return height_; }
void Oval::Set(int width, int height) {
  width_ = width;
  height_ = height;
}
void Oval::Show() const {
  std::cout << "width = " << width_ << ", height = " << height_ << "\n";
}
int main() {
  Oval a{};
  Oval b{ 3,4 };
  a.Set(10, 20);
  a.Show();
  std::cout << b.GetWidth() << ", " << b.GetHeight() << "\n";

}

//quiz10 코드가 좀 깁니다.. 
//Add.h
#ifndef CHAP1_CHAP3_PROJECT_ADD_H_
#define CHAP1_CHAP3_PROJECT_ADD_H_

class Add {
public:
  Add();
  void SetValue(int num1, int num2);
  int Calculate() const;

private:
  int num1_;
  int num2_;
};

#endif
//Sub.h
#ifndef CHAP1_CHAP3_PROJECT_SUB_H_
#define CHAP1_CHAP3_PROJECT_SUB_H_

class Sub {
public:
  Sub();
  void SetValue(int num1, int num2);
  int Calculate() const;

private:
  int num1_;
  int num2_;
};

#endif
//Mul.h
#ifndef CHAP1_CHAP3_PROJECT_MUL_H_
#define CHAP1_CHAP3_PROJECT_MUL_H_

class Mul {
public:
  Mul();
  void SetValue(int num1, int num2);
  int Calculate() const;

private:
  int num1_;
  int num2_;
};

#endif
//Div.h
#ifndef CHAP1_CHAP3_PROJECT_DIV_H_
#define CHAP1_CHAP3_PROJECT_DIV_H_

class Div {
public:
  Div();
  void SetValue(int num1, int num2);
  int Calculate() const;

private:
  int num1_;
  int num2_;
};

#endif
// Add.cc
#include "Add.h"

Add::Add() {}
void Add::SetValue(int num1, int num2) { num1_ = num1; num2_ = num2; }
int Add::Calculate() const { return num1_ + num2_; }

// Sub.cc
#include "Sub.h"

Sub::Sub() {}
void Sub::SetValue(int num1, int num2) { num1_ = num1; num2_ = num2; }
int Sub::Calculate() const { return num1_ - num2_; }

// Mul.cc
#include "Mul.h"

Mul::Mul() {}
void Mul::SetValue(int num1, int num2) { num1_ = num1; num2_ = num2; }
int Mul::Calculate() const { return num1_ * num2_; }

// Div.cc
#include "Div.h"

Div::Div() {}
void Div::SetValue(int num1, int num2) { num1_ = num1; num2_ = num2; }
int Div::Calculate() const { return num1_ / num2_; }

// main.cc
#include <iostream>
#include <string>
#include <cstdlib>
#include "Add.h"
#include "Sub.h"
#include "Mul.h"
#include "Div.h"

int main() {
  Add a{};
  Sub s{};
  Mul m{};
  Div d{};

  while (true) {
    std::cout << "두 정수와 연산자를 입력하세요>>";
    int num1{};
    int num2{};
    char op{};
    std::cin >> num1 >> num2 >> op;

    switch (op) {
    case '+':
      a.SetValue(num1, num2);
      std::cout << a.Calculate() << "\n";
      break;
    case '-':
      s.SetValue(num1, num2);
      std::cout << s.Calculate() << "\n";
      break;
    case '*':
      m.SetValue(num1, num2);
      std::cout << m.Calculate() << "\n";
      break;
    case '/':
      d.SetValue(num1, num2);
      std::cout << d.Calculate() << "\n";
      break;
    default:
      std::cout << "Wrong input. Try again\n";
    }
  }

}
// Ram.h
#ifndef SOLUTION1_CHAP3_RAM_H_
#define SOLUTION1_CHAP3_RAM_H_

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&);               \
  void operator=(const TypeName&)
class Ram {
public:
  Ram();
  ~Ram();
  char Read(int address) const;
  void Write(int address, char value);
private:
  char mem_[100 * 1024];
  int size_;
  DISALLOW_COPY_AND_ASSIGN(Ram);
};

#endif

// Ram.cc
#include "Ram.h"
#include <iostream>

Ram::Ram() :size_(100*1024){
  for (int i = 0; i < size_; i++) {
    mem_[i] = 0;
  }
}
Ram::~Ram() { std::cout << "메모리 제거됨\n"; }
char Ram::Read(int address) const { return mem_[address]; }
void Ram::Write(int address, char value) {
  mem_[address] = value;
}
// quiz12
#include <iostream>
#include <string>
#include "Ram.h"

int main() {
  Ram ram{};
  ram.Write(100, 20);
  ram.Write(101, 30);
  char res{ ram.Read(100) + ram.Read(101) };
  ram.Write(102, res);
  std::cout << "102번지의 값 = " << static_cast<int>(ram.Read(102)) << "\n";
}
반응형

댓글