CPP05
1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* main.cpp :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2025/06/30 12:26:03 by kamitsui #+# #+# */
9
/* Updated: 2025/07/18 02:48:08 by kamitsui ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
21
#include "
AForm.hpp
"
22
#include "
Bureaucrat.hpp
"
23
#include "
PresidentialPardonForm.hpp
"
24
#include "
RobotomyRequestForm.hpp
"
25
#include "
ShrubberyCreationForm.hpp
"
26
#include <
cstdlib
>
// For srand
27
#include <
ctime
>
// For time
28
#include <
iostream
>
29
30
void
print_header
(
const
std::string
&title) {
std::cout
<<
"\n\033[1;33m--- "
<< title <<
" ---\033[0m"
<<
std::endl
; }
31
32
int
main
() {
33
// Seed the random number generator once for RobotomyRequestForm
34
std::srand
(
std::time
(NULL));
35
36
// Create bureaucrats
37
Bureaucrat
high(
"Zaphod"
, 1);
38
Bureaucrat
mid(
"Ford"
, 50);
39
Bureaucrat
low(
"Arthur"
, 148);
40
41
// Create forms
42
ShrubberyCreationForm
shrub(
"garden"
);
43
RobotomyRequestForm
robo(
"Bender"
);
44
PresidentialPardonForm
pardon(
"Marvin"
);
45
46
print_header
(
"Initial Status"
);
47
std::cout
<< high <<
std::endl
;
48
std::cout
<< mid <<
std::endl
;
49
std::cout
<< low <<
std::endl
;
50
std::cout
<< shrub <<
std::endl
;
51
std::cout
<< robo <<
std::endl
;
52
std::cout
<< pardon <<
std::endl
;
53
54
// --- Test Case 1: Low-level Bureaucrat ---
55
print_header
(
"Test Case 1: Low-level Bureaucrat 'Arthur' (Grade 148)"
);
56
low.
signForm
(shrub);
// Fails (needs 145)
57
low.
signForm
(robo);
// Fails
58
low.
signForm
(pardon);
// Fails
59
low.
executeForm
(shrub);
// Fails (not signed)
60
61
// --- Test Case 2: Signing Shrubbery and executing ---
62
print_header
(
"Test Case 2: Signing & Executing Shrubbery Form"
);
63
Bureaucrat
shrubExpert(
"Shrub-Master"
, 140);
64
std::cout
<< shrubExpert <<
std::endl
;
65
shrubExpert.
signForm
(shrub);
66
shrubExpert.
executeForm
(shrub);
// Fails (exec grade 137 needed)
67
68
std::cout
<<
"---"
<<
std::endl
;
69
mid.
executeForm
(shrub);
// Mid-level can execute it
70
71
// --- Test Case 3: Signing and Executing Robotomy ---
72
print_header
(
"Test Case 3: Signing & Executing Robotomy Form"
);
73
mid.
signForm
(robo);
74
mid.
executeForm
(robo);
// Fails (exec grade 45 needed)
75
76
std::cout
<<
"---"
<<
std::endl
;
77
high.
executeForm
(robo);
// High-level can execute it
78
79
// --- Test Case 4: Signing and Executing Presidential Pardon ---
80
print_header
(
"Test Case 4: Signing & Executing Presidential Pardon"
);
81
mid.
signForm
(pardon);
// Fails (sign grade 25 needed)
82
83
std::cout
<<
"---"
<<
std::endl
;
84
high.
signForm
(pardon);
85
high.
executeForm
(pardon);
86
87
// --- Test Case 5: Executing a form that isn't signed ---
88
print_header
(
"Test Case 5: Executing an unsigned form"
);
89
RobotomyRequestForm
robo2(
"WALL-E"
);
90
high.
executeForm
(robo2);
91
92
print_header
(
"Tests complete."
);
93
return
0;
94
}
std::string
Bureaucrat
Represents a bureaucrat with a name and a grade.
Definition
Bureaucrat.hpp:30
Bureaucrat::signForm
void signForm(Form &form)
Attempts to sign a form. Calls the form's beSigned method and prints the result.
Definition
Bureaucrat.cpp:68
Bureaucrat::executeForm
void executeForm(AForm const &form)
Attempts to execute a form. Calls the form's execute method and prints the result.
Definition
Bureaucrat.cpp:70
PresidentialPardonForm
A concrete form that grants a presidential pardon.
Definition
PresidentialPardonForm.hpp:27
RobotomyRequestForm
A concrete form that simulates a robotomy attempt.
Definition
RobotomyRequestForm.hpp:28
ShrubberyCreationForm
A concrete form that creates a file with ASCII trees.
Definition
ShrubberyCreationForm.hpp:28
std::cout
cstdlib
ctime
std::endl
T endl(T... args)
print_header
void print_header(const std::string &title)
Prints a formatted header to the console.
Definition
main.cpp:28
main
int main()
Main entry point of the program.
Definition
main.cpp:34
AForm.hpp
Defines the abstract base class AForm for all specific forms.
Bureaucrat.hpp
Defines the Bureaucrat class, updated to execute AForms.
PresidentialPardonForm.hpp
Defines the PresidentialPardonForm class.
RobotomyRequestForm.hpp
Defines the RobotomyRequestForm class.
ShrubberyCreationForm.hpp
Defines the ShrubberyCreationForm class.
iostream
std::srand
T srand(T... args)
std::time
T time(T... args)
ex02
main.cpp
構築:
1.9.8