Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grechko_Veronica_CW #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file added Grechko_Veronica_cw/8381_AiStrD_Grechko_VD_CW.pdf
Binary file not shown.
50 changes: 50 additions & 0 deletions Grechko_Veronica_cw/bin_struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef BIN_STRUCT_H
#define BIN_STRUCT_H
#include <QGraphicsItem>
#include <QGraphicsView>
#include <QGraphicsEffect>
#include <QString>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextEdit>
#include <QMainWindow>
#include <QStandardPaths>
#include <QtGui>
#include <QColorDialog>
#include <QInputDialog>
#include <QPushButton>
#include <QStringList>

struct Node // структура для представления узлов дерева
{
int key;
int size;
Node* left;
Node* right;
Node(int k) { key = k; left = right = 0; size = 1; }

};


class BinTree
{
private:
Node* Current = nullptr;
public:
Node* Head = nullptr;
BinTree();
Node* insert(Node* p, int k);
Node* insertroot(Node* p, int k);
Node* rotateright(Node* p);
Node* rotateleft(Node* q);
int max_depth(Node *hd);
int getsize(Node* p);
void fixsize(Node* p);
Node* join(Node* p, Node* q);
Node* remove(Node* p, int k);

};
int treePainter(QGraphicsScene *scene, Node *node, int w, int h, int wDelta, int hDelta, QPen &pen, QBrush &brush, QFont &font, int depth, int c);

QGraphicsScene *graphic(BinTree *tree, QGraphicsScene *scene, int depht);
#endif // BIN_STRUCT_H
50 changes: 50 additions & 0 deletions Grechko_Veronica_cw/draw_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<bin_struct.h>

QGraphicsScene *graphic(BinTree *tree, QGraphicsScene *scene, int depth)
{
if (tree == nullptr)
return scene;
scene->clear();
QPen pen;
QColor color;
color.setRgb(220, 220, 220);
pen.setColor(color);
int color_c = 220;
color.setRgb(220, 220, 220);
QBrush brush (color);
QFont font;
font.setFamily("Tahoma");
pen.setWidth(3);
int wDeep = static_cast<int>(pow(2, depth));
int hDelta = 70;
int wDelta = 15;
font.setPointSize(wDelta);
int width = (wDelta*wDeep)/2;
treePainter(scene, tree->Head, width/2, hDelta, wDelta, hDelta, pen, brush, font, wDeep, color_c);
return scene;
}

int treePainter(QGraphicsScene *scene, Node *node, int w, int h, int wDelta, int hDelta, QPen &pen, QBrush &brush, QFont &font, int depth, int colour)
{
if (node == nullptr)
return 0;
QString out;
out.append(QString::number(node->key));
QColor color;
color.setRgb(colour, colour, colour);
pen.setColor(color);
QGraphicsTextItem *textItem = new QGraphicsTextItem;
if (node->left != nullptr)
scene->addLine(w+wDelta/2, h+wDelta, w-(depth/2)*wDelta+wDelta/2, h+hDelta+wDelta, pen);
if (node->right != nullptr)
scene->addLine(w+wDelta/2, h+wDelta, w+(depth/2)*wDelta+wDelta/2, h+hDelta+wDelta, pen);
textItem->setPos(w, h);
textItem->setPlainText(out);
textItem->setFont(font);
scene->addEllipse(w-wDelta/2, h, wDelta*5/2, wDelta*5/2, pen, brush);

scene->addItem(textItem);
treePainter(scene, node->left, w-(depth/2)*wDelta, h+hDelta, wDelta, hDelta, pen, brush, font, depth/2, colour - 30);
treePainter(scene, node->right, w+(depth/2)*wDelta, h+hDelta, wDelta, hDelta, pen, brush, font, depth/2, colour- 50);
return 0;
}
Binary file added Grechko_Veronica_cw/lr5.pdf
Binary file not shown.
11 changes: 11 additions & 0 deletions Grechko_Veronica_cw/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}
10 changes: 10 additions & 0 deletions Grechko_Veronica_cw/main_fun.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef MAIN_FUN_H
#define MAIN_FUN_H
#include <bin_struct.h>


int treePainter(QGraphicsScene *scene, Node *node, int w, int h, int wDelta, int hDelta, QPen &pen, QBrush &brush, QFont &font, int depth);

QGraphicsScene *graphic(BinTree *tree, QGraphicsScene *scene, int depht);

#endif // MAIN_FUN_H
147 changes: 147 additions & 0 deletions Grechko_Veronica_cw/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<bin_struct.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene;
ui->graphicsView->setScene(scene);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_printTree_clicked()
{

QString data = ui->input_tree->text();
QStringList abc = data.split(' ');
int* mas = new int[100];
int i =0;
bool go = true;
for (auto x:abc){
bool convertOK;
x.toInt(&convertOK);
if(convertOK == false){
go = false;

}
else{
mas[i] = x.toInt();
i++;
}
}
if(!go){
QMessageBox::warning(this,"Error", "Incorrect input");
return;
}
BinTree* BT = new (BinTree);
for(int j = 0; j < i; j++){
BT->Head = BT->insert(BT->Head, mas[j]);
}

int depth = BT->max_depth(BT->Head);
ui->input_del->setText(QString::number(depth));
graphic(BT, scene, depth);

}

void MainWindow::on_delete_elem_clicked()
{
QString data = ui->input_tree->text();
QStringList abc = data.split(' ');
int* mas = new int[100];
int i =0;
bool go = true;
for (auto x:abc){
bool convertOK;
x.toInt(&convertOK);
if(convertOK == false){
go = false;

}
else{
mas[i] = x.toInt();
i++;
}
}
if(!go){
QMessageBox::warning(this,"Error", "Incorrect input");
return;
}
BinTree* BT = new (BinTree);
for(int j = 0; j < i; j++){
BT->Head = BT->insert(BT->Head, mas[j]);
}
QString elem_ = ui->input_del->text();
bool convert_;
int elem = elem_.toInt(&convert_);
if(!convert_){
QMessageBox::warning(this,"Error", "Incorrect input");
return;
}
BT->Head = BT->remove(BT->Head, elem);
int depth = BT->max_depth(BT->Head);
QString out;
for(int l = 0; l < i - 1; l++){
if (mas[l] == elem){
for(int k = l; k < i - 1; k++){
mas[k] = mas[k + 1];
}
}
out.append(QString::number(mas[l]));
if(l != i -2) out.append(" ");
}
ui->input_tree->setText(out);
graphic(BT, scene, depth);
}

void MainWindow::on_choose_file_clicked()
{
QString file_name = QFileDialog().getOpenFileName();
if(!file_name.isNull()){
QFile file(file_name);
if(file.open(file.ReadOnly)){
QString data = file.readAll();
if (data == "") {
QMessageBox::critical(this, "Error!", "Введите дерево");
return;
}
else{
QStringList abc = data.split(' ');
int* mas = new int[100];
int i =0;
bool go = true;
for (auto x:abc){
bool convertOK;
x.toInt(&convertOK);
if(convertOK == false){
go = false;
}
else{
mas[i] = x.toInt();
i++;
}
}
if(!go){
QMessageBox::warning(this,"Error", "Incorrect input");
return;
}
BinTree* BT = new (BinTree);
for(int j = 0; j < i; j++){
BT->Head = BT->insert(BT->Head, mas[j]);
}
int depth = BT->max_depth(BT->Head);
graphic(BT, scene, depth);
}
}


}
else QMessageBox::warning(this,"Error", "Not Found");
}

34 changes: 34 additions & 0 deletions Grechko_Veronica_cw/mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsItem>
#include <QGraphicsView>
#include <QGraphicsEffect>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_printTree_clicked();

void on_delete_elem_clicked();

void on_choose_file_clicked();


private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
};

#endif // MAINWINDOW_H
Loading