From de5558cd5bc67ddda1bc23d526a825c0f00fac7e Mon Sep 17 00:00:00 2001 From: Isabell Pflug Date: Thu, 11 May 2023 22:19:56 +0200 Subject: [PATCH] :sparkles: finished assignment 4.1 --- assignments/04/ExtendedVector/.gitignore | 74 ++++++++++ .../04/ExtendedVector/ExtendedVector.cpp | 48 +++++++ .../04/ExtendedVector/ExtendedVector.h | 16 +++ .../04/ExtendedVector/ExtendedVector.pro | 27 ++++ assignments/04/ExtendedVector/main.cpp | 11 ++ assignments/04/ExtendedVector/mainwindow.cpp | 68 +++++++++ assignments/04/ExtendedVector/mainwindow.h | 24 ++++ assignments/04/ExtendedVector/mainwindow.ui | 136 ++++++++++++++++++ 8 files changed, 404 insertions(+) create mode 100644 assignments/04/ExtendedVector/.gitignore create mode 100644 assignments/04/ExtendedVector/ExtendedVector.cpp create mode 100644 assignments/04/ExtendedVector/ExtendedVector.h create mode 100644 assignments/04/ExtendedVector/ExtendedVector.pro create mode 100644 assignments/04/ExtendedVector/main.cpp create mode 100644 assignments/04/ExtendedVector/mainwindow.cpp create mode 100644 assignments/04/ExtendedVector/mainwindow.h create mode 100644 assignments/04/ExtendedVector/mainwindow.ui diff --git a/assignments/04/ExtendedVector/.gitignore b/assignments/04/ExtendedVector/.gitignore new file mode 100644 index 0000000..4a0b530 --- /dev/null +++ b/assignments/04/ExtendedVector/.gitignore @@ -0,0 +1,74 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* +CMakeLists.txt.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/assignments/04/ExtendedVector/ExtendedVector.cpp b/assignments/04/ExtendedVector/ExtendedVector.cpp new file mode 100644 index 0000000..f150ba8 --- /dev/null +++ b/assignments/04/ExtendedVector/ExtendedVector.cpp @@ -0,0 +1,48 @@ +#include "ExtendedVector.h" + +double ExtendedVector::max(const vector &v) { + double max = numeric_limits::min(); + for (auto it = v.begin(); it != v.end(); ++it) { + // ich war so erleichtert, als ich endlich std::signbit gefunden habe! + if ( signbit(max - *it)) + max = *it; + } + return max; +} + +bool ExtendedVector::allPositive(const vector &v) { + for (auto it = v.begin(); it != v.end(); ++it) { + if ( signbit(*it) || (*it == 0) ) + return false; + } + return true; +} + +double ExtendedVector::product(const vector &v1, const vector &v2) { + double product = 0; + long unsigned int size = v1.size(); + if ( v1.size() > v2.size() ) + size = v2.size(); + for (long unsigned int i = 0; i < size; i++) { + product += v1[i] * v2[i]; + } + return product; +} + +vector ExtendedVector::product(const vector &v, double f) { + vector product; + for (auto it = v.begin(); it != v.end(); ++it) { + product.push_back(*it * f); + } + return product; +} + +double ExtendedVector::norm(const vector &v) { + return sqrt( ExtendedVector::product(v, v) ); +} + +void ExtendedVector::normalise(vector &v) { + vector *address = &v; + *address = ExtendedVector::product(v, (1.0)/(ExtendedVector::norm(v))); + return; +} diff --git a/assignments/04/ExtendedVector/ExtendedVector.h b/assignments/04/ExtendedVector/ExtendedVector.h new file mode 100644 index 0000000..f295f82 --- /dev/null +++ b/assignments/04/ExtendedVector/ExtendedVector.h @@ -0,0 +1,16 @@ +#include +#include +#include +#include + + +using namespace std; +class ExtendedVector { + public: + static double max(const vector &v); + static bool allPositive(const vector &v); + static double product(const vector &v1, const vector &v2); + static vector product(const vector &v, double f); + static double norm(const vector &v); + static void normalise(vector &v); +}; diff --git a/assignments/04/ExtendedVector/ExtendedVector.pro b/assignments/04/ExtendedVector/ExtendedVector.pro new file mode 100644 index 0000000..526e688 --- /dev/null +++ b/assignments/04/ExtendedVector/ExtendedVector.pro @@ -0,0 +1,27 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + ExtendedVector.cpp \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + ExtendedVector.h \ + mainwindow.h \ + ui_mainwindow.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/assignments/04/ExtendedVector/main.cpp b/assignments/04/ExtendedVector/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/assignments/04/ExtendedVector/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/assignments/04/ExtendedVector/mainwindow.cpp b/assignments/04/ExtendedVector/mainwindow.cpp new file mode 100644 index 0000000..1ac3304 --- /dev/null +++ b/assignments/04/ExtendedVector/mainwindow.cpp @@ -0,0 +1,68 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include "ExtendedVector.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) { + ui->setupUi(this); +} + +MainWindow::~MainWindow() { + delete ui; +} + +void parseAsDoubleVector(QString vec_string, vector *vec) { + QStringList vec_string_list = vec_string.split(","); + bool isSuccess; + double t_value; + for (auto vec_it = vec_string_list.begin(); vec_it != vec_string_list.end(); vec_it++) { + t_value = vec_it->toDouble(&isSuccess); + if (isSuccess) { + vec->push_back(t_value); + } else { + throw std::invalid_argument("Please only write doubles into the Input field."); + } + } + return; +} + +void printVector(vector vec, QTextStream *output) { + *output << "< "; + for (auto it = vec.begin(); it != vec.end()-1; it++) { + *output << *it << ", "; + } + *output << *(vec.end()-1) << " >" << Qt::endl; +} + +void addTestToOutput(vector vec, double factor, QTextStream *output) { + *output << "Maximum: " << ExtendedVector::max(vec) << Qt::endl; + *output << "Are all values positive? " << ExtendedVector::allPositive(vec) << Qt::endl; + vector product_vector = ExtendedVector::product(vec, factor); + *output << "Product: "; + printVector(product_vector, output); + *output << "Norm: " << ExtendedVector::norm(vec) << Qt::endl; + vector vec_copy = vec; + ExtendedVector::normalise(vec_copy); + *output << "Normalisation: " << Qt::endl; + printVector(vec_copy, output); +} + +void MainWindow::on_okButton_clicked() { + QString vec1_string = ui->vec1Input->text(); + QString vec2_string = ui->vec2Input->text(); + vector vec1; + vector vec2; + parseAsDoubleVector(vec1_string, &vec1); + parseAsDoubleVector(vec2_string, &vec2); + QString owo = ""; + QTextStream output(&owo); + output << "Vector 1: " << Qt::endl; + addTestToOutput(vec1, ui->factorSpinBox->value(), &output); + output << Qt::endl << "Vector 2: " << Qt::endl; + addTestToOutput(vec2, ui->factorSpinBox->value(), &output); + output << Qt::endl << "Scalarproduct: " << ExtendedVector::product(vec1, vec2) << Qt::endl; + + ui->outputBrowser->setText(*output.string()); +} + diff --git a/assignments/04/ExtendedVector/mainwindow.h b/assignments/04/ExtendedVector/mainwindow.h new file mode 100644 index 0000000..a850800 --- /dev/null +++ b/assignments/04/ExtendedVector/mainwindow.h @@ -0,0 +1,24 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private slots: + void on_okButton_clicked(); + +private: + Ui::MainWindow *ui; +}; +#endif // MAINWINDOW_H diff --git a/assignments/04/ExtendedVector/mainwindow.ui b/assignments/04/ExtendedVector/mainwindow.ui new file mode 100644 index 0000000..b5e1e0b --- /dev/null +++ b/assignments/04/ExtendedVector/mainwindow.ui @@ -0,0 +1,136 @@ + + + MainWindow + + + + 0 + 0 + 571 + 681 + + + + MainWindow + + + + + + 110 + 30 + 391 + 51 + + + + 1.2, 4.3, -2.7, -11 + + + + + + 110 + 100 + 391 + 51 + + + + -0.9, 4.5 + + + + + + 30 + 40 + 67 + 21 + + + + Vector 1 + + + + + + 30 + 110 + 67 + 21 + + + + Vector 2 + + + + + + 410 + 170 + 91 + 29 + + + + Ok + + + + + + 30 + 220 + 511 + 391 + + + + + + + 200 + 170 + 66 + 29 + + + + + + + 110 + 170 + 81 + 21 + + + + Multiply by + + + + + + + 0 + 0 + 571 + 26 + + + + + Is this a good enough UI now? + + + + + + + + +