finished assignment 4.1

main
Isabell Pflug 3 years ago
parent ef7eb5cc66
commit de5558cd5b

@ -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

@ -0,0 +1,48 @@
#include "ExtendedVector.h"
double ExtendedVector::max(const vector<double> &v) {
double max = numeric_limits<double>::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<double> &v) {
for (auto it = v.begin(); it != v.end(); ++it) {
if ( signbit(*it) || (*it == 0) )
return false;
}
return true;
}
double ExtendedVector::product(const vector<double> &v1, const vector<double> &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<double> ExtendedVector::product(const vector<double> &v, double f) {
vector<double> product;
for (auto it = v.begin(); it != v.end(); ++it) {
product.push_back(*it * f);
}
return product;
}
double ExtendedVector::norm(const vector<double> &v) {
return sqrt( ExtendedVector::product(v, v) );
}
void ExtendedVector::normalise(vector<double> &v) {
vector <double> *address = &v;
*address = ExtendedVector::product(v, (1.0)/(ExtendedVector::norm(v)));
return;
}

@ -0,0 +1,16 @@
#include <cmath>
#include <bitset>
#include <limits>
#include <vector>
using namespace std;
class ExtendedVector {
public:
static double max(const vector<double> &v);
static bool allPositive(const vector<double> &v);
static double product(const vector<double> &v1, const vector<double> &v2);
static vector<double> product(const vector<double> &v, double f);
static double norm(const vector<double> &v);
static void normalise(vector<double> &v);
};

@ -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

@ -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();
}

@ -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<double> *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<double> 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<double> vec, double factor, QTextStream *output) {
*output << "Maximum: " << ExtendedVector::max(vec) << Qt::endl;
*output << "Are all values positive? " << ExtendedVector::allPositive(vec) << Qt::endl;
vector<double> product_vector = ExtendedVector::product(vec, factor);
*output << "Product: ";
printVector(product_vector, output);
*output << "Norm: " << ExtendedVector::norm(vec) << Qt::endl;
vector<double> 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<double> vec1;
vector<double> 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());
}

@ -0,0 +1,24 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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

@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>571</width>
<height>681</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLineEdit" name="vec1Input">
<property name="geometry">
<rect>
<x>110</x>
<y>30</y>
<width>391</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>1.2, 4.3, -2.7, -11</string>
</property>
</widget>
<widget class="QLineEdit" name="vec2Input">
<property name="geometry">
<rect>
<x>110</x>
<y>100</y>
<width>391</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>-0.9, 4.5</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>67</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Vector 1</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>30</x>
<y>110</y>
<width>67</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Vector 2</string>
</property>
</widget>
<widget class="QPushButton" name="okButton">
<property name="geometry">
<rect>
<x>410</x>
<y>170</y>
<width>91</width>
<height>29</height>
</rect>
</property>
<property name="text">
<string>Ok</string>
</property>
</widget>
<widget class="QTextBrowser" name="outputBrowser">
<property name="geometry">
<rect>
<x>30</x>
<y>220</y>
<width>511</width>
<height>391</height>
</rect>
</property>
</widget>
<widget class="QDoubleSpinBox" name="factorSpinBox">
<property name="geometry">
<rect>
<x>200</x>
<y>170</y>
<width>66</width>
<height>29</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>110</x>
<y>170</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Multiply by</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>571</width>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuIst_das_hier_gen_gender_User_Input">
<property name="title">
<string>Is this a good enough UI now?</string>
</property>
</widget>
<addaction name="menuIst_das_hier_gen_gender_User_Input"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Loading…
Cancel
Save