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->setupUi(this);
setWindowTitle("Square Multiply Calculator");
spinBase = ui->spinBase;
spinExp = ui->spinExp;

View File

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

View File

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

View File

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