Skip to content

Commit d5a00c8

Browse files
author
David Júnior
committed
ADD.: added calc forms and its logic.
0 parents  commit d5a00c8

File tree

7 files changed

+664
-0
lines changed

7 files changed

+664
-0
lines changed

.gitignore

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This file is used to ignore files which are generated
2+
# ----------------------------------------------------------------------------
3+
4+
*~
5+
*.autosave
6+
*.a
7+
*.core
8+
*.moc
9+
*.o
10+
*.obj
11+
*.orig
12+
*.rej
13+
*.so
14+
*.so.*
15+
*_pch.h.cpp
16+
*_resource.rc
17+
*.qm
18+
.#*
19+
*.*#
20+
core
21+
!core/
22+
tags
23+
.DS_Store
24+
.directory
25+
*.debug
26+
Makefile*
27+
*.prl
28+
*.app
29+
moc_*.cpp
30+
ui_*.h
31+
qrc_*.cpp
32+
Thumbs.db
33+
*.res
34+
*.rc
35+
/.qmake.cache
36+
/.qmake.stash
37+
38+
# qtcreator generated files
39+
*.pro.user*
40+
41+
# xemacs temporary files
42+
*.flc
43+
44+
# Vim temporary files
45+
.*.swp
46+
47+
# Visual Studio generated files
48+
*.ib_pdb_index
49+
*.idb
50+
*.ilk
51+
*.pdb
52+
*.sln
53+
*.suo
54+
*.vcproj
55+
*vcproj.*.*.user
56+
*.ncb
57+
*.sdf
58+
*.opensdf
59+
*.vcxproj
60+
*vcxproj.*
61+
62+
# MinGW generated files
63+
*.Debug
64+
*.Release
65+
66+
# Python byte code
67+
*.pyc
68+
69+
# Binaries
70+
# --------
71+
*.dll
72+
*.exe
73+

Calc.pro

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
QT += core gui
2+
3+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4+
5+
CONFIG += c++11
6+
7+
# You can make your code fail to compile if it uses deprecated APIs.
8+
# In order to do so, uncomment the following line.
9+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
10+
11+
SOURCES += \
12+
main.cpp \
13+
mainwindow.cpp
14+
15+
HEADERS += \
16+
mainwindow.h
17+
18+
FORMS += \
19+
mainwindow.ui
20+
21+
TRANSLATIONS += \
22+
Calc_pt_BR.ts
23+
CONFIG += lrelease
24+
CONFIG += embed_translations
25+
26+
# Default rules for deployment.
27+
qnx: target.path = /tmp/$${TARGET}/bin
28+
else: unix:!android: target.path = /opt/$${TARGET}/bin
29+
!isEmpty(target.path): INSTALLS += target

Calc_pt_BR.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE TS>
3+
<TS version="2.1" language="pt_BR"></TS>

main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "mainwindow.h"
2+
3+
#include <QApplication>
4+
#include <QLocale>
5+
#include <QTranslator>
6+
7+
int main(int argc, char *argv[])
8+
{
9+
QApplication a(argc, argv);
10+
11+
QTranslator translator;
12+
const QStringList uiLanguages = QLocale::system().uiLanguages();
13+
for (const QString &locale : uiLanguages) {
14+
const QString baseName = "Calc_" + QLocale(locale).name();
15+
if (translator.load(":/i18n/" + baseName)) {
16+
a.installTranslator(&translator);
17+
break;
18+
}
19+
}
20+
MainWindow w;
21+
22+
w.setWindowState(
23+
w.windowState() &
24+
(~Qt::WindowMaximized | Qt::WindowActive)
25+
);
26+
27+
Qt::WindowFlags windowFlags = (Qt::Dialog | Qt::CustomizeWindowHint);
28+
29+
if (true) {
30+
windowFlags |= Qt::WindowCloseButtonHint;
31+
} else {
32+
windowFlags |= Qt::WindowMinimizeButtonHint;
33+
windowFlags |= Qt::WindowCloseButtonHint;
34+
}
35+
36+
w.setWindowFlags(windowFlags);
37+
38+
w.show();
39+
return a.exec();
40+
}

mainwindow.cpp

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
#include "mainwindow.h"
2+
#include "ui_mainwindow.h"
3+
#include <QRegularExpression>
4+
#include <QMessageBox>
5+
6+
MainWindow::MainWindow(QWidget *parent)
7+
: QMainWindow(parent)
8+
, ui(new Ui::MainWindow)
9+
{
10+
ui->setupUi(this);
11+
}
12+
13+
MainWindow::~MainWindow()
14+
{
15+
delete ui;
16+
}
17+
18+
void MainWindow::makeOperations(bool percent = false)
19+
{
20+
const QString msg = "Não é possível realizar divisão por 0!";
21+
if(!percent) {
22+
if(data[OPER] == "+")
23+
// Sum
24+
result = QString::number(data[NUM1].toDouble() + data[NUM2].toDouble());
25+
else if (data[OPER] == "-")
26+
// Sub
27+
result = QString::number(data[NUM1].toDouble() - data[NUM2].toDouble());
28+
else if (data[OPER] == "x" && !percent)
29+
// Mult
30+
result = QString::number(data[NUM1].toDouble() * data[NUM2].toDouble());
31+
else if (data[OPER] == ":" && data[NUM2] != "0")
32+
// Div
33+
result = QString::number(data[NUM1].toDouble() / data[NUM2].toDouble());
34+
else if (data[OPER] == ":" && data[NUM2] == "0") {
35+
QMessageBox msgBox;
36+
msgBox.setInformativeText(msg);
37+
msgBox.exec();
38+
data[NUM2] = "";
39+
return;
40+
}
41+
} else {
42+
if (data[OPER] == "x")
43+
// Percent
44+
result = QString::number(data[NUM1].toDouble() * data[NUM2].toDouble() / 100);
45+
else if (data[OPER] == "+")
46+
result = QString::number(data[NUM1].toDouble() * data[NUM2].toDouble() / 100 + data[NUM1].toDouble());
47+
else if (data[OPER] == "-")
48+
result = QString::number((data[NUM1].toDouble() * data[NUM2].toDouble() / 100 - data[NUM1].toDouble()) * -1);
49+
else if (data[OPER] == ":")
50+
result = QString::number(data[NUM1].toDouble() / (data[NUM1].toDouble() * data[NUM2].toDouble() / 100));
51+
}
52+
53+
// writeLcd
54+
writeInLcd();
55+
}
56+
57+
void MainWindow::writeInLcd()
58+
{
59+
if(data[OPER] == "")
60+
ui->lcdNumber->display(data[NUM1]);
61+
else if(data[OPER] != "" && result == "")
62+
ui->lcdNumber->display(data[NUM2]);
63+
else
64+
ui->lcdNumber->display(result);
65+
}
66+
67+
void MainWindow::manageTmpData(QString _data)
68+
{
69+
if(result != "") {
70+
result = "";
71+
data[NUM1] = "";
72+
data[OPER] = "";
73+
data[NUM2] = "";
74+
}
75+
76+
// Regex to check operator.
77+
QRegularExpression oper("^\\+|\\-|x|\\:$");
78+
79+
// Match regex to operator data.
80+
QRegularExpressionMatch operMatch = oper.match(_data);
81+
82+
if(operMatch.hasMatch() && data[NUM1] != "") {
83+
data[OPER] = _data;
84+
tmpData = "";
85+
return;
86+
}
87+
88+
// Regex to check digit values.
89+
QRegularExpression digit("^\\d$");
90+
91+
// Match regex to digit data.
92+
QRegularExpressionMatch mdig = digit.match(_data);
93+
94+
// If matched append the _data value into tmpData.
95+
if(mdig.hasMatch()){
96+
tmpData += _data;
97+
} else {
98+
// Regex to find dot into tmpData.
99+
QRegularExpression dot("^\\d+\\.\\d{0,}$");
100+
// Match regex to dot data.
101+
QRegularExpressionMatch mdot = dot.match(tmpData);
102+
103+
// If dot not found then add the dot value into tmpData.
104+
if(!mdot.hasMatch() && !operMatch.hasMatch() && _data != "=" && _data != "%") {
105+
tmpData += _data;
106+
}
107+
}
108+
109+
// If operator space in data array is empty.
110+
if(data[OPER] == "") {
111+
// Clean data position 0
112+
data[NUM1] = "";
113+
// attribute tmpData into data position 0 as number.
114+
data[NUM1] = tmpData;
115+
} else if(!operMatch.hasMatch() && _data != "=" && _data != "%") { // if operator space was setted.
116+
data[NUM2] = "";
117+
data[NUM2] = tmpData;
118+
}
119+
120+
if(_data == "=") {
121+
makeOperations(false);
122+
data[NUM1] = result;
123+
result = "";
124+
data[OPER] = "";
125+
data[NUM2] = "";
126+
tmpData = "";
127+
} else if(_data == "%") {
128+
makeOperations(true);
129+
data[NUM1] = result;
130+
result = "";
131+
data[OPER] = "";
132+
data[NUM2] = "";
133+
tmpData = "";
134+
} else {
135+
writeInLcd();
136+
}
137+
}
138+
139+
140+
void MainWindow::on_pb_clean_clicked()
141+
{
142+
result = "";
143+
data[NUM1] = "";
144+
data[OPER] = "";
145+
data[NUM2] = "";
146+
tmpData = "";
147+
ui->lcdNumber->display("0");
148+
}
149+
150+
void MainWindow::on_pb_equal_clicked()
151+
{
152+
manageTmpData("=");
153+
}
154+
155+
void MainWindow::on_pb_dot_clicked()
156+
{
157+
// tmpData += ".";
158+
manageTmpData(".");
159+
}
160+
161+
void MainWindow::on_pb_0_clicked()
162+
{
163+
// tmpData += "0";
164+
manageTmpData("0");
165+
}
166+
167+
void MainWindow::on_pb_1_clicked()
168+
{
169+
// tmpData += "1";
170+
manageTmpData("1");
171+
}
172+
173+
void MainWindow::on_pb_2_clicked()
174+
{
175+
// tmpData += "2";
176+
manageTmpData("2");
177+
}
178+
179+
void MainWindow::on_pb_3_clicked()
180+
{
181+
// tmpData += "3";
182+
manageTmpData("3");
183+
}
184+
185+
void MainWindow::on_pb_4_clicked()
186+
{
187+
// tmpData += "4";
188+
manageTmpData("4");
189+
}
190+
191+
void MainWindow::on_pb_5_clicked()
192+
{
193+
// tmpData += "5";
194+
manageTmpData("5");
195+
}
196+
197+
void MainWindow::on_pb_6_clicked()
198+
{
199+
// tmpData += "6";
200+
manageTmpData("6");
201+
}
202+
203+
void MainWindow::on_pb_7_clicked()
204+
{
205+
// tmpData += "7";
206+
manageTmpData("7");
207+
}
208+
209+
void MainWindow::on_pb_8_clicked()
210+
{
211+
// tmpData += "8";
212+
manageTmpData("8");
213+
}
214+
215+
void MainWindow::on_pb_9_clicked()
216+
{
217+
// tmpData += "9";
218+
manageTmpData("9");
219+
}
220+
221+
void MainWindow::on_pb_sum_clicked()
222+
{
223+
manageTmpData("+");
224+
}
225+
226+
void MainWindow::on_pb_sub_clicked()
227+
{
228+
manageTmpData("-");
229+
}
230+
231+
void MainWindow::on_pb_mult_clicked()
232+
{
233+
manageTmpData("x");
234+
}
235+
236+
void MainWindow::on_pb_div_clicked()
237+
{
238+
manageTmpData(":");
239+
}
240+
241+
void MainWindow::on_pb_percent_clicked()
242+
{
243+
manageTmpData("%");
244+
}

0 commit comments

Comments
 (0)