반응형
- 이제 form에 어떠한 양식을 더 만들 거임
3가지로 나뉨
- ShrubberyCreationForm: 필수 성적: 서명 145, 실행 137
- 작업 디렉터리에 <target>_shrubbery 파일을 만들고 그 안에 ASCII 트리를 씁니다.
- RobotomyRequestForm: 필수 성적: 서명 72, 실행 45
- 드릴링 소음이 발생합니다. 그런 다음 <target>이 50%의 확률로 로봇화되었음을 알려줍니다. 그렇지 않으면 로봇공학이 실패했음을 알립니다.
- PresidentialPardonForm: 필수 등급: sign 25, exec 5
- <target>이 Zaphod Beeblebrox에 의해 사면되었음을 알립니다.
ShrubberyCreationForm
hpp
#ifndef SHRUBBERYCRETIONFORM_HPP
# define SHRUBBERYCRETIONFORM_HPP
#include <fstream>
#include "AForm.hpp"
#include "ShrubberyCreationForm.hpp"
class ShrubberyCreationForm : public AForm
{
private:
std::string target;
public:
ShrubberyCreationForm(void);
ShrubberyCreationForm(std::string target);
ShrubberyCreationForm(ShrubberyCreationForm const &shrubberyCreationForm);
ShrubberyCreationForm &operator=(ShrubberyCreationForm const &shrubberyCreationForm);
~ShrubberyCreationForm(void);
std::string getTarget(void) const;
void execute(Bureaucrat const &executor) const;
};
# endif
cpp
#include "ShrubberyCreationForm.hpp"
#include <string>
ShrubberyCreationForm::ShrubberyCreationForm(void) \\
: AForm("Unknown", 145, 137), target("unknown")
{
std::cout << this->getName() << " Constructor called\\n";
}
ShrubberyCreationForm::ShrubberyCreationForm(const std::string target)\\
:AForm("Shrubbery", 145, 137), target(target)
{
std::cout << this->getName() << " Constructor called\\n";
}
ShrubberyCreationForm::ShrubberyCreationForm(ShrubberyCreationForm const &shrubberyCreationForm)\\
:AForm(shrubberyCreationForm), target(shrubberyCreationForm.getTarget())
{
std::cout << this->getName() << " copy Constructor called\\n";
}
ShrubberyCreationForm::~ShrubberyCreationForm(void) \\
{
std::cout << this->getName() << " Destructor called\\n";
}
ShrubberyCreationForm& ShrubberyCreationForm::operator=(ShrubberyCreationForm const &shrubberyCreationForm)
{
if (this == &shrubberyCreationForm)
return (*this);
this->target = shrubberyCreationForm.getTarget();
this->AForm::operator=(shrubberyCreationForm);
std::cout << " copy Constructor called\\n";
return (*this);
}
std::string ShrubberyCreationForm::getTarget(void) const
{
return (this->target);
}
void ShrubberyCreationForm::execute(const Bureaucrat &executor) const
{
this->checkExecuteGrade(executor);
std::ofstream File;
std::string tmp = target + "_shrubbery";
File.open(tmp.c_str());
std::string ascii =" - - - \\n\\
- - - -- - \\n\\
- - - - \\n\\
- \\n\\
- -- \\n\\
- - - - \\n\\
- \\'-, - - \\n\\
- \\'b * \\n\\
- \\'$ #- -- \\n\\
- - $: #: - \\n\\
-- - -- *# @): - - - \\n\\
- :@,@): ,-**:\\' - \\n\\
- -, :@@*: --**\\' - - \\n\\
\\'#o- -:(@\\'-@*\\"\\' - \\n\\
- - \\'bq,--:,@@*\\' ,* - - \\n\\
,p$q8,:@)\\' -p*\\' - \\n\\
- \\' - \\'@@Pp@@*\\' - - \\n\\
- - -- Y7\\'.\\' - - \\n\\
:@):. \\n\\
.:@:\\'. \\n\\
.::(@:. ";
if (!File.is_open())
{
throw FileException();
}
File << ascii;
File.close();
}
RobotomyRequestForm
hpp
#ifndef ROBOTOMYREQUESTFROM_HPP
# define ROBOTOMYREQUESTFROM_HPP
#include "AForm.hpp"
#include <cstdlib>
class RobotomyRequestForm : public AForm
{
private:
std::string target;
public:
RobotomyRequestForm(void);
RobotomyRequestForm(const std::string target);
RobotomyRequestForm(RobotomyRequestForm const &robotomyRequestForm);
~RobotomyRequestForm(void);
RobotomyRequestForm &operator=(RobotomyRequestForm const &robotomyRequestForm);
std::string getTarget(void) const;
void execute(Bureaucrat const &executor) const;
};
#endif
cpp
#include "RobotomyRequestForm.hpp"
RobotomyRequestForm::RobotomyRequestForm(void) \\
:AForm("Unknown" , 72, 45), target("unknown")
{
std::cout << this->getName() << " Constructor called\\n";
}
RobotomyRequestForm::RobotomyRequestForm(const std::string target) \\
:AForm("Robotomy" , 72, 45), target(target)
{
std::cout << this->getName() << " Constructor called\\n";
}
RobotomyRequestForm::RobotomyRequestForm(RobotomyRequestForm const &robotomyRequestForm) \\
:AForm(robotomyRequestForm), target(robotomyRequestForm.getTarget())
{
std::cout << this->getName() << " copy Constructor called\\n";
}
RobotomyRequestForm::~RobotomyRequestForm(void) \\
{
std::cout << this->getName() << " Destructor called\\n";
}
RobotomyRequestForm& RobotomyRequestForm::operator=(RobotomyRequestForm const &robotomyRequestForm)
{
if (this == &robotomyRequestForm)
return (*this);
this->target = robotomyRequestForm.getTarget();
this->AForm::operator=(robotomyRequestForm);
std::cout << " copy Constructor called\\n";
return (*this);
}
void RobotomyRequestForm::execute(const Bureaucrat &executor) const
{
this->checkExecuteGrade(executor);
int tmp = (rand() % 2);
std::cout << "Drill... Drrr\\n";
if (tmp)
{
std::cout << "wow! " << this->getName() << " succeeded in becoming a robot" << std::endl;
}
else
{
std::cout << "sorry... " << this->getName() << " failed to becoming a robot" << std::endl;
}
}
std::string RobotomyRequestForm::getTarget(void) const
{
return (this->target);
}
PresidentialPardonForm
hpp
#ifndef PRESIDENTIALPARDONFORM_HPP
# define PRESIDENTIALPARDONFORM_HPP
#include "AForm.hpp"
class PresidentialPardonForm : public AForm
{
private:
std::string target;
public:
PresidentialPardonForm(void);
PresidentialPardonForm(const std::string target);
PresidentialPardonForm(PresidentialPardonForm const &presidentialPardonForm);
~PresidentialPardonForm(void);
PresidentialPardonForm &operator=(PresidentialPardonForm const &presidentialPardonForm);
std::string getTarget(void) const;
void execute(Bureaucrat const &executor) const;
};
#endif
cpp
#include "PresidentialPardonForm.hpp"
PresidentialPardonForm::PresidentialPardonForm(void)
:AForm("Unknown" , 25, 5), target("unknown")
{
std::cout << this->getName() << " Constructor called\\n";
}
PresidentialPardonForm::PresidentialPardonForm(const std::string target)
:AForm("Presidential" , 25, 5), target(target)
{
std::cout << this->getName() << " Constructor called\\n";
}
PresidentialPardonForm::PresidentialPardonForm(PresidentialPardonForm const &presidentialPardonForm)
:AForm(presidentialPardonForm), target(presidentialPardonForm.getTarget())
{
std::cout << this->getName() << " copy Constructor called\\n";
}
PresidentialPardonForm::~PresidentialPardonForm(void)
{
std::cout << this->getName() << " Destructor called\\n";
}
PresidentialPardonForm& PresidentialPardonForm::operator=(PresidentialPardonForm const &presidentialPardonForm)
{
if (this == &presidentialPardonForm)
return (*this);
this->target = presidentialPardonForm.getTarget();
this->AForm::operator=(presidentialPardonForm);
std::cout << " copy Constructor called\\n";
return (*this);
}
void PresidentialPardonForm::execute(const Bureaucrat &executor) const
{
this->checkExecuteGrade(executor);
std::cout << this->getName() << " has been pardoned by Zaphod Beeblebrox.\\n";
}
std::string PresidentialPardonForm::getTarget(void) const
{
return (this->target);
}
- AForm 수정
실행(Bureaucrat const & executor) const 멤버 함수를 추가
- 기본 폼을 만들고 구체적인 클래스의 폼 작업을 실행하는 함수를 구현합니다. 양식에 서명이 되어 있는지, 그리고 해당 양식을 실행하려는 관료의 등급이 충분히 높은지 확인해야 합니다. 그렇지 않으면 적절한 예외를 발생시킵니다.
virtual void execute(Bureaucrat const &executor) const = 0;
AForm
hpp
#ifndef AFORM_HPP
# define AFORM_HPP
#include "Bureaucrat.hpp"
class Bureaucrat;
class AForm{
private:
const std::string name;
bool sign;
const int signGrade;
const int execGrade;
public:
AForm(void);
AForm(std::string name, int signGrade, int execGrade);
AForm(AForm const &form);
AForm &operator=(AForm const &form);
~AForm(void);
std::string getName(void) const;
bool getSign(void) const;
int getSignGrade(void) const;
int getExecGrade(void) const;
void setSign(bool sign);
void beSigned(Bureaucrat const &person);
virtual void execute(Bureaucrat const &executor) const = 0;
void checkExecuteGrade(Bureaucrat const &executor) const;
class GradeTooLowException : public std::exception
{
public:
const char *what(void) const throw();
};
class GradeTooHighException : public std::exception
{
public:
const char *what(void) const throw();
};
class NotSignedException : public std::exception
{
public:
const char *what(void) const throw();
};
class ExecuteDeninedException : public std::exception
{
public:
const char *what(void) const throw();
};
class FileException : public std::exception
{
public:
const char *what(void) const throw();
};
};
std::ostream &operator<<(std::ostream &out, const AForm &form);
# endif
cpp
#include "AForm.hpp"
#include "Bureaucrat.hpp"
AForm::AForm(void) :name("undefined"), sign(false), signGrade(150), execGrade(150)
{
std::cout << this->name << " Constructor called\\n";
}
AForm::AForm(std::string name, int signGrade, int execGrade) : name(name), sign(false), signGrade(signGrade), execGrade(execGrade)
{
if (signGrade < 1 || execGrade < 1)
throw GradeTooHighException();
if (signGrade > 150 || execGrade > 150)
throw GradeTooLowException();
std::cout << this->name << " Constructor called\\n";
}
AForm::AForm(AForm const &form) : name(form.getName()), sign(form.getSign()), signGrade(form.getSignGrade()), execGrade(form.getExecGrade())
{
std::cout << this->name << " copy Constructor called\\n";
}
AForm& AForm::operator=(AForm const &form)
{
if (this == &form)
return (*this);
this->sign = form.getSign();
std::cout << this->name << " copy Constructor called\\n";
return (*this);
}
AForm::~AForm()
{
std::cout << this->name << " Destructor called\\n";
}
std::string AForm::getName(void) const
{
return (this->name);
}
bool AForm::getSign(void) const
{
return (this->sign);
}
int AForm::getSignGrade(void) const
{
return (this->signGrade);
}
int AForm::getExecGrade(void) const
{
return (this->execGrade);
}
void AForm::setSign(bool sign)
{
this->sign = sign;
}
void AForm::beSigned(Bureaucrat const &person)
{
if (person.getGrade() >= this->signGrade)
{
throw GradeTooLowException();
}
else if (sign)
{
throw sign;
}
else
{
sign = true;
}
}
void AForm::checkExecuteGrade(Bureaucrat const &executor) const
{
if (!this->getSign())
{
throw NotSignedException();
}
if (executor.getGrade() >= this->getExecGrade())
{
throw ExecuteDeninedException();
}
}
const char *AForm::GradeTooLowException::what(void) const throw()
{
return "Grade is too low\\n";
}
const char *AForm::GradeTooHighException::what(void) const throw()
{
return "Grade is too high\\n";
}
const char *AForm::NotSignedException::what(void) const throw()
{
return "AForm is not signed\\n";
}
const char *AForm::ExecuteDeninedException::what(void) const throw()
{
return "Executable Access Denined";
}
const char *AForm::FileException::what(void) const throw()
{
return "File error";
}
std::ostream &operator<<(std::ostream &out, const AForm &form)
{
out << "AForm : " << form.getName() << "\\nSign : " << form.getSign() << "\\nSignGrade : " << form.getSignGrade() << "\\nExecgrade : " << form.getExecGrade() << "\\n";
return (out);
}
- Bureaucrat 수정
void executeForm(AForm const & form);
hpp
#ifndef BUREAUCRAT_HPP
# define BUREAUCRAT_HPP
#include <iostream>
#include "AForm.hpp"
class AForm;
class Bureaucrat{
private:
const std::string name;
int grade;
public:
Bureaucrat(void);
Bureaucrat(std::string name, int grade);
Bureaucrat(Bureaucrat const &bureaucrat);
Bureaucrat &operator=(Bureaucrat const &bureaucrat);
~Bureaucrat(void);
std::string getName(void) const;
int getGrade(void) const;
void upGrade(void);
void downGrade(void);
void signForm(AForm &form);
void executeForm(AForm const & form);
class GradeTooLowException : public std::exception
{
public:
const char *what(void) const throw();
};
class GradeTooHighException : public std::exception
{
public:
const char *what(void) const throw();
};
};
std::ostream &operator<<(std::ostream &out, const Bureaucrat &bureaucrat);
# endif
main.cpp
#include "Bureaucrat.hpp"
#include "AForm.hpp"
#include "ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "PresidentialPardonForm.hpp"
#include "Intern.hpp"
int main(void) {
try {
Intern intern;
AForm* scf = intern.makeForm("shrubbery creation", "42seoul");
std::cout << *scf << "\\n";
delete scf;
std::cout << "================ Shrubbery ================\\n";
AForm *rrfe = intern.makeForm("robotomy request", "42seoul");
std::cout << *rrfe << "\\n";
delete rrfe;
std::cout << "\\n================ Robotomy ================\\n";
AForm *ppf = intern.makeForm("presidential pardon", "42seoul");
std::cout << *ppf << "\\n";
delete ppf;
std::cout << "\\n================ Presidential ================\\n";
AForm *unknown = intern.makeForm("Unknown", "42seoul");
std::cout << unknown << "\\n";
delete unknown;
std::cout << "\\n================ Unknown ================\\n";
{
Intern someRandomIntern;
AForm* rrf;
rrf = someRandomIntern.makeForm("robotomy request" , "Bender");
std::cout << *rrf << "\\n";
delete rrf;
}
std::cout << "\\n================ Main ================\\n";
} catch (const std::exception &e) {
std::cout << e.what() << "\\n";
}
}
반응형
'42Seoul > CPP Module 05' 카테고리의 다른 글
ex03 (0) | 2024.01.26 |
---|---|
ex01 (0) | 2024.01.26 |
ex00 (0) | 2024.01.26 |