3 Commits

Author SHA1 Message Date
1484cfa7b6 fullfilled requirements 2021-05-29 20:15:19 +02:00
a76a04f15c everything working 2021-05-29 19:22:46 +02:00
4481431794 added color 2021-05-29 19:11:24 +02:00
4 changed files with 47 additions and 34 deletions

View File

@ -6,6 +6,7 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
setWindowTitle("Square Multiply Calculator");
spinBase = ui->spinBase; spinBase = ui->spinBase;
spinExp = ui->spinExp; spinExp = ui->spinExp;

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>390</width> <width>333</width>
<height>562</height> <height>416</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -19,35 +19,43 @@
<rect> <rect>
<x>10</x> <x>10</x>
<y>10</y> <y>10</y>
<width>371</width> <width>311</width>
<height>71</height> <height>31</height>
</rect> </rect>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
<widget class="QLabel" name="labBase"> <widget class="QLabel" name="labBase">
<property name="text"> <property name="text">
<string>Base:</string> <string>base:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSpinBox" name="spinBase"/> <widget class="QSpinBox" name="spinBase">
<property name="maximum">
<number>9999</number>
</property>
</widget>
</item> </item>
<item> <item>
<widget class="QLabel" name="labExp"> <widget class="QLabel" name="labExp">
<property name="text"> <property name="text">
<string>Exponent:</string> <string>exponent:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSpinBox" name="spinExp"/> <widget class="QSpinBox" name="spinExp">
<property name="maximum">
<number>9999</number>
</property>
</widget>
</item> </item>
<item> <item>
<widget class="QLabel" name="labMod"> <widget class="QLabel" name="labMod">
<property name="text"> <property name="text">
<string>Modulus:</string> <string>modulos:</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -56,6 +64,9 @@
<property name="minimum"> <property name="minimum">
<number>1</number> <number>1</number>
</property> </property>
<property name="maximum">
<number>9999</number>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -64,11 +75,14 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>10</x>
<y>90</y> <y>50</y>
<width>371</width> <width>311</width>
<height>421</height> <height>321</height>
</rect> </rect>
</property> </property>
<property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum>
</property>
<attribute name="verticalHeaderStretchLastSection"> <attribute name="verticalHeaderStretchLastSection">
<bool>false</bool> <bool>false</bool>
</attribute> </attribute>
@ -79,7 +93,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>390</width> <width>333</width>
<height>20</height> <height>20</height>
</rect> </rect>
</property> </property>

View File

@ -1,6 +1,7 @@
#include "sqmtablemodel.h" #include "sqmtablemodel.h"
#include <algorithm> #include <algorithm>
#include <QColor> #include <QColor>
#include <QRegularExpression>
SQMTableModel::SQMTableModel(QObject *parent) SQMTableModel::SQMTableModel(QObject *parent)
@ -32,27 +33,16 @@ QVariant SQMTableModel::data(const QModelIndex &index, int role) const {
break; break;
case Qt::ForegroundRole: case Qt::ForegroundRole:
if (changedHere.isValid()) { if (changedHere.isValid() && highlightChanged) {
if (row >= changedHere.row() && col >= changedHere.column()) { if ((row > changedHere.row() || (col >= changedHere.column() && row == changedHere.row())) && (col != 0 || changedHere.column() == 0)) {
result = QColor(Qt::red); result = QColor(Qt::red);
} }
} }
break; break;
case Qt::TextAlignmentRole:
result = Qt::AlignRight;
break;
} }
// if (role == Qt::DisplayRole) {
// int row = index.row();
// int col = index.column();
// QString result;
// try {
// result = QString::number(sqmMatrix.at(col).at(row));
// } catch (...) {
// result = "x";
// }
// return result;
// }
return result; return result;
} }
@ -80,11 +70,14 @@ bool SQMTableModel::setData(const QModelIndex &index, const QVariant &value, int
int row = index.row(); int row = index.row();
int col = index.column(); int col = index.column();
if (col == 0 && value.toInt() != 0 && value.toInt() != 1) {
return false;
}
// call calculateSqmMatrix // call calculateSqmMatrix
sqmMatrix.at(col).at(row) = value.toInt(); sqmMatrix.at(col).at(row) = value.toInt();
changedHere = index; changedHere = index;
highlightChanged = true;
UpdateSqmMatrix(index); UpdateSqmMatrix(index);
return true; return true;
} }
@ -102,7 +95,7 @@ void SQMTableModel::SetStartValues(int pBase, int pExp, int pMod) {
base = pBase; base = pBase;
exp = pExp; exp = pExp;
mod = pMod; mod = pMod;
highlightChanged = false;
//changedHere.model()->index(-1, -1, QModelIndex()); //changedHere.model()->index(-1, -1, QModelIndex());
CalculateSqmMatrix(); CalculateSqmMatrix();
} }
@ -173,10 +166,14 @@ void SQMTableModel::UpdateSqmMatrix(QModelIndex startIndex) {
// Update sqmMatrix // Update sqmMatrix
for (int i = start_row; i < binLen; i++) { for (int i = start_row; i < binLen; i++) {
if (start_col != 1) { if (start_row == 0 && start_col == 0) {
sqmMatrix.at(1).at(i) = (sqmMatrix.at(2).at(i - 1) * sqmMatrix.at(2).at(i - 1)) % mod; sqmMatrix.at(1).at(i) = 1;
start_row = 1;
} }
else if (start_col != 1) {
sqmMatrix.at(1).at(i) = (sqmMatrix.at(2).at(i - 1) * sqmMatrix.at(2).at(i - 1)) % mod;
}
start_col = 0; start_col = 0;
if (sqmMatrix.at(0).at(i) == 0) { if (sqmMatrix.at(0).at(i) == 0) {
sqmMatrix.at(2).at(i) = sqmMatrix.at(1).at(i); sqmMatrix.at(2).at(i) = sqmMatrix.at(1).at(i);

View File

@ -27,6 +27,7 @@ private:
int exp; int exp;
int mod; int mod;
int binLen = 0; int binLen = 0;
bool highlightChanged;
QModelIndex changedHere; QModelIndex changedHere;
vector<vector <int>> sqmMatrix; vector<vector <int>> sqmMatrix;