Курсовая работа по дисциплине Объектноориентированное программирование
Скачать 415.64 Kb.
|
|
Действия | № перехода | Комментарий |
15
1 | | Вывод "Object tree" и | 2 | | |
| переход на новую строку | | | ||
| | | | | |
2 | | Вызов метода | 3 | | |
| print_hierarchy_tree | | | ||
| | | | | |
3 | | Объявление строковой | 4 | | |
| переменной coordinate | | | ||
| | | | | |
4 | | Ввод значения переменной | 5 | | |
| | coordinate | | | |
5 | | Объявление указателя | 6 | | |
| coordinate_object | | | ||
| | | | | |
| | Направление указателя | | | |
| | coordinate_object на объект, | | | |
| coordinate не | возвращенный в результате | 7 | | |
6 | равен "//" | вызова метода | | | |
| | get_object_by_path с | | | |
| | аргументом coordinate | | | |
| | | 10 | | |
| | Переход на новую строку и | | | |
7 | | вывод значения переменной | 8 | | |
| | coordinate | | | |
| | Вывод " Object name: " и | | | |
| coordinate_obje | результата вызова метода | | | |
8 | ct не нулевой | get_object_name объекта, на | 9 | | |
указатель | который указывает | | | | |
| | coordinate_object | | | |
| | Вывод " Object not found" | 9 | | |
9 | | Ввод значения переменной | 6 | | |
| | coordinate | | | |
10 | | Возврат 0 | ∅ | | |
16
Блок-схема алгоритма
17
18
19
20
21
22
23
24
Код программы
Программная реализация алгоритмов для решения задачи представлена ниже.
Файл BaseApplication.cpp
#include "BaseApplication.h"
#include "Class2.h"
#include "Class3.h"
#include "Class4.h"
#include "Class5.h"
#include "Class6.h"
#include
#include
void BaseApplication::build_tree_objects() {
std::string root_object_name;
std::cin >> root_object_name;
define_object_name(root_object_name);
define_object_state(1);
Base* child;
Base* parent_object;
std::string parent_object_name, object_name; int object_class, object_state;
while (true) {
std::cin >> parent_object_name;
if (parent_object_name == "endtree") {
break;
}
std::cin >> object_name >> object_class >> object_state; parent_object = get_object_by_path(parent_object_name); switch (object_class) {
case 2:
child = new Class2(parent_object, object_name); child->define_object_state(object_state); break;
case 3:
child = new Class3(parent_object, object_name); child->define_object_state(object_state); break;
case 4:
child = new Class4(parent_object, object_name); child->define_object_state(object_state); break;
case 5:
child = new Class5(parent_object, object_name); child->define_object_state(object_state); break;
case 6:
child = new Class6(parent_object, object_name);
25
child->define_object_state(object_state); break;
}
}
}
int BaseApplication::exec_app() {
std::cout << "Object tree\n";
print_hierarchy_tree();
std::string coordinate;
std::cin >> coordinate;
Base* coordinate_object;
while (coordinate != "//") {
coordinate_object = get_object_by_path(coordinate); std::cout << "\n" << coordinate;
if (coordinate_object != nullptr) {
std::cout << " Object name: " << coordinate_object-
>get_object_name();
}
else {
std::cout << " Object not found";
}
std::cin >> coordinate;
}
return 0;
}
Файл BaseApplication.h
#ifndef BASE_APPLICATION_H_
#define BASE_APPLICATION_H_
#include "Base.h"
class BaseApplication: public Base {
public:
using Base::Base;
void build_tree_objects();
int exec_app();
};
#endif // BASE_APPLICATION_H_
Файл Base.cpp
26
#include "Base.h"
#include
#include
#include
#include
Base::Base(Base* parent, std::string name)
object_parent(parent), object_name(name) { if (parent != nullptr) {
parent->children_object_list.push_back(this);
}
}
Base::Base() {
for (auto child_object : children_object_list) { delete child_object;
}
}
void Base::define_object_name(std::string name) { object_name = name;
}
std::string Base::get_object_name() {
return object_name;
}
void Base::print_object_tree() {
if (object_parent == nullptr) {
std::cout << object_name;
}
if (!children_object_list.empty()) { std::cout << "\n" << object_name;
for (auto child_object : children_object_list) { std::cout << " " << child_object->object_name;
}
for (auto child_object : children_object_list) { child_object->print_object_tree();
}
}
}
void Base::redefine_object_parent(Base* new_parent) {
remove(object_parent->children_object_list.begin(), object_parent-
>children_object_list.end(), this);
object_parent = new_parent;
new_parent->children_object_list.push_back(this);
}
Base* Base::get_object_parent() {
return object_parent;
}
int Base::get_object_state() {
return object_state;
}
void Base::define_object_state(int state) { object_state = state;
27
}
void Base::print_state_tree() {
std::string not_ready = "";
if (object_state <= 0) {
not_ready = "not ";
}
std::cout << "The object " << object_name << " is " << not_ready <<
"ready";
if (!children_object_list.empty()) {
std::cout << "\n";
}
for (auto child_object : children_object_list) { child_object->print_state_tree();
if (child_object != children_object_list.back()) { std::cout << "\n";
}
}
}
Base* Base::get_object_by_name(std::string name) { if (object_name == name) {
return this;
}
Base* search_result;
for (auto child_object : children_object_list) {
search_result = child_object->get_object_by_name(name);
if (search_result != nullptr) {
return search_result;
}
}
return nullptr;
}
void Base::print_hierarchy_tree(int level) {
std::cout << std::string(level * 4, ' ') << get_object_name(); if (!children_object_list.empty()) {
std::cout << "\n";
}
for (auto child_object : children_object_list) { child_object->print_hierarchy_tree(level + 1);
if (child_object != children_object_list.back()) { std::cout << "\n";
}
}
}
Base* Base::get_object_by_path(std::string path) { std::string name;
if (path[1] == '/') {
name = path.substr(2, path.length() - 2); return get_object_by_name(name);
}
28
int start = -1, end = path.length();
for (int i = 1; i < path.length(); i++) { if (path[i] == '/') {
if (start == -1) {
start = i;
}
else {
end = i;
break;
}
}
}
if (start == -1) {
return this;
}
name = path.substr(start + 1, end - start - 1);
std::string child_object_name;
for (auto child_object : children_object_list) {
child_object_name = child_object->get_object_name();
if (child_object_name == name) {
name = path.substr(start, path.length() - start); return child_object->get_object_by_path(name);
}
}
return nullptr;
}