Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа 12 / LAB1_CONSOLE - final / main
.cpp#include <iostream.h>
#include <vector>
#include "Triangle.h"
#include "Pentagon.h"
#include "Text.h"
#include "Graph.h"
#include "GraphStructureException.h"
#include "Iterator.h"
#include "Triangle.h"
#include "Pentagon.h"
#include "Text.h"
#include "TextInPentagon.h"
#include "ExtGraphIterator.h"
/*
#include <conio.h>
Graph<Figure*>*g = NULL;
Figure* node_value = NULL;
int show_menu();
void initialize_graph() {
}
Figure* read_figure() {
Figure* f = NULL;
int selection;
double centerX,centerY,size;
char txt[255];
system("cls");
cout << "[1] Read Triangle" << endl;
cout << "[2] Read Pentagon" << endl;
cout << "[3] Read Text" << endl;
cout << "[4] Read TextInPentagon" << endl;
selection = getch();
if (selection == '1') {
system("cls");
cout << "Enter Triangles center point (X,Y) and size: ";
cin >> centerX >> centerY >> size;
f = new Triangle(centerX, centerY, size);
cout << "Press any key to continue" << endl;
getch();
} else if (selection=='2') {
system("cls");
cout << "Enter Pentagons center point (X,Y) and size: ";
cin >> centerX >> centerY >> size;
f = new Pentagon(centerX, centerY, size);
cout << "Press any key to continue" << endl;
getch();
} else if (selection == '3') {
system("cls");
cout << "Enter Texts characters and center point (X,Y): ";
cin >> txt >> centerX >> centerY;
f = new Text(txt,centerX, centerY);
cout << "Press any key to continue" << endl;
getch();
} else if (selection == '4') {
system("cls");
cout << "Enter TextsInPentagons center point (X,Y) size and characters: ";
cin >> centerX >> centerY >> size >> txt;
f = new TextInPentagon(centerX, centerY, size, txt);
cout << "Press any key to continue" << endl;
getch();
}
return f;
}
void add_node() {
if (g == NULL) {
system("cls");
cout << "Graph should be initialized to perform this operation" << endl;
getch();
return;
}
system("cls");
Figure*f = read_figure();
int node_number;
int node_attach_number;
cout << "Enter node number and number of node to attach to: ";
cin >> node_number >> node_attach_number;
try {
g->add(f,node_number,node_attach_number);
} catch (GraphStructureException ex) {
ex.showCause();
delete f;
getch();
}
}
void remove_node() {
if (g == NULL) {
system("cls");
cout << "Graph should be initialized to perform this operation" << endl;
getch();
return;
}
system("cls");
int node_number;
cout << "Enter node number to be removed: ";
cin >> node_number;
try {
Figure*f = g->getValByNode(node_number);
g->removeByNode(node_number);
delete f;
getch();
if (g->getCurrentSize() == 0) {
delete g;
g = NULL;
}
} catch (GraphStructureException ex) {
ex.showCause();
getch();
}
}
void add_link() {
if (g == NULL) {
system("cls");
cout << "Graph should be initialized to perform this operation" << endl;
getch();
return;
}
system("cls");
int node_from;
int node_to;
cout << "Enter node numbers to link: ";
cin >> node_from >> node_to;
try {
g->link(node_from,node_to);
} catch (GraphStructureException ex) {
ex.showCause();
getch();
}
}
void remove_link() {
if (g == NULL) {
system("cls");
cout << "Graph should be initialized to perform this operation" << endl;
getch();
return;
}
system("cls");
int node_from, node_to;
cout << "Enter node numbers of link to be removed: ";
cin >> node_from >> node_to;
try {
g->unlink(node_from,node_to);
} catch (GraphStructureException ex) {
ex.showCause();
getch();
}
}
void show_matrix() {
if (g == NULL) {
system("cls");
cout << "Graph should be initialized to perform this operation" << endl;
getch();
return;
}
system("cls");
g->show_rel_matrix();
getch();
}
void iterator_test() {
if (g == NULL) {
system("cls");
cout << "Graph should be initialized to perform this operation" << endl;
getch();
return;
}
system("cls");
Iterator<Figure*>*i = g->iterator();
while(i->hasNext()) {
Figure* val = i->next();
cout << *val << endl;
cout << "hit any key for next element" << endl;
getch();
}
cout << "No more elements" << endl;
getch();
}
void perform_delete() {
if (g == NULL) {
cout << "Graph is NULL. Initialize first." << endl;
getch();
return;
}
Iterator<Figure*>*i = g->iterator();
while(i->hasNext()) {
Figure*f = i->next();
g->removeByVal(f);
delete f;
cout << "Press any key to continue" << endl;
getch();
}
}
int main()
{
int choice;
do {
choice = show_menu();
switch (choice) {
case '1':
//initialize_graph();
system("cls");
node_value = read_figure();
int node_number;
cout << "Enter init node number: " << endl;
cin >> node_number;
if(g == NULL) {
g = new Graph<Figure*>();
}
try {
g->init(node_value,node_number);
} catch (GraphStructureException ex) {
ex.showCause();
delete node_value;
getch();
}
/////////////////////
break;
case '2':
add_node();
break;
case '3':
remove_node();
break;
case '4':
add_link();
break;
case '5':
remove_link();
break;
case '6':
show_matrix();
break;
case '7':
iterator_test();
break;
case '8':
perform_delete();
default:
break;
}
} while (choice!='9');
if( g != NULL) delete g;
return 0;
}
int show_menu() {
system("cls");
cout << "1. Initialize graph" << endl;
cout << "2. Add node" << endl;
cout << "3. Remove node" << endl;
cout << "4. Add link" << endl;
cout << "5. Remove link" << endl;
cout << "6. Show matrix" << endl;
cout << "7. Iterator test" << endl;
cout << "8. Perform delete operation" << endl;
cout << "9. Exit" << endl;
return getch();
}
*/
int main() {
Graph<int>* gr = new Graph<int>();
int v = 1;
gr->add(v,1);
v = 2;
gr->add(v,2);
v = 3;
gr->add(v,3);
v = 4;
gr->add(v,4);
gr->removeByNode(2);
gr->removeByNode(3);
gr->removeByNode(1);
gr->removeByNode(4);
// gr->show_rel_matrix();
Iterator<int>* i = new
ExtGraphIterator<int>(gr);
// i->first();
// cout << "Current: " << i->current() << endl;
// cout << "End: " << i->end() << endl;
// i->next();
// cout << "Current: " << i->current() << endl;
// cout << "End: " << i->end() << endl;
// i->next();
// cout << "Current: " << i->current() << endl;
// cout << "End: " << i->end() << endl;
// i->next();
// cout << "Current: " << i->current() << endl;
// cout << "End: " << i->end() << endl;
cout << gr->getCurrentSize() << endl;
for(i->first();!i->endNext();i->next())
{
cout << i->current() << endl;
}
// i->last();
//
// cout << "Current: " << i->current() << endl;
// cout << "End: " << i->endPrevious() << endl;
//
// i->previous();
// cout << "Current: " << i->current() << endl;
// cout << "End: " << i->endPrevious() << endl;
delete gr;
delete i;
return 0;
}
Соседние файлы в папке LAB1_CONSOLE - final