1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
#include "dialog.h"
#include<QDebug>
#include<QString>
#include<QKeySequence>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
createGridLayout();
this->setLayout(mainLayout);
slotClearAll();
}
Dialog::~Dialog()
{
}
void Dialog::createGridLayout(){
mainLayout = new QGridLayout;
// edit창 세팅
for(int i=0;i<3;++i){
lineEdit[i] = new QLineEdit;
lineEdit[i]->setAlignment(Qt::AlignRight);
lineEdit[i]->setReadOnly(true);
}
mainLayout->addWidget(lineEdit[0],0,0,1,6);
mainLayout->addWidget(lineEdit[1],1,0,1,6);
// 상위 세개 버튼 세팅
QPushButton *upperBtns[3];
const char* upperBtnsEle[3][3]={
{"BackSpace","Clear","Clear All"}
,{SLOT(slotBackSpace()),SLOT(slotClear()),SLOT(slotClearAll())}};
const QKeySequence qKeySeq[3]={
QKeySequence(Qt::Key_Backspace),NULL,QKeySequence(Qt::Key_Escape)};
for(int i=0;i<3;++i){
upperBtns[i] = new QPushButton(upperBtnsEle[0][i],this);
upperBtns[i]->setShortcut(qKeySeq[i]);
connect(upperBtns[i],SIGNAL(clicked()),this,upperBtnsEle[1][i]);
mainLayout->addWidget(upperBtns[i],2,i*2,1,2);
}
// 나머지 버튼 세팅
QPushButton *mainBtns[24];
const char* mainBtnsEle[2][24]={
{"MC","7","8","9","÷","Sqrt"
,"MR","4","5","6","x","x^2"
,"MS","1","2","3","-","1/x"
,"M+","0",".","±","+","="}
,{SLOT(slotMC()),SLOT(slotDigits()),SLOT(slotDigits()),SLOT(slotDigits()),SLOT(slotMainOp()),SLOT(slotOp())
,SLOT(slotMR()),SLOT(slotDigits()),SLOT(slotDigits()),SLOT(slotDigits()),SLOT(slotMainOp()),SLOT(slotOp())
,SLOT(slotMS()),SLOT(slotDigits()),SLOT(slotDigits()),SLOT(slotDigits()),SLOT(slotMainOp()),SLOT(slotOp())
,SLOT(slotMPlus()),SLOT(slotDigits()),SLOT(slotDot()),SLOT(slotPlusMinus()),SLOT(slotMainOp()),SLOT(slotEqual())}
};
const QKeySequence qKeySeqMain[24]={
NULL,QKeySequence(Qt::Key_7),QKeySequence(Qt::Key_8),QKeySequence(Qt::Key_9),QKeySequence(Qt::Key_Slash),NULL
,NULL,QKeySequence(Qt::Key_4),QKeySequence(Qt::Key_5),QKeySequence(Qt::Key_6),QKeySequence(Qt::Key_Asterisk),NULL
,NULL,QKeySequence(Qt::Key_1),QKeySequence(Qt::Key_2),QKeySequence(Qt::Key_3),QKeySequence(Qt::Key_Minus),NULL
,NULL,QKeySequence(Qt::Key_0),QKeySequence(Qt::Key_Comma),NULL,QKeySequence(Qt::Key_Plus),QKeySequence(Qt::Key_Enter)
};
for(int i=0;i<24;++i){
mainBtns[i] = new QPushButton(mainBtnsEle[0][i],this);
mainBtns[i]->setShortcut(qKeySeqMain[i]);
connect(mainBtns[i],SIGNAL(clicked()),this,mainBtnsEle[1][i]);
mainLayout->addWidget(mainBtns[i]);
}
mainLayout->addWidget(lineEdit[2],7,0,1,6);
}
void Dialog::slotBackSpace(){
QString tmp = lineEdit[0]->text();
double num = tmp.toDouble();
if(tmp=="0")return;
else if(tmp.length()==1){
lineEdit[0]->setText("0");
appendable=false;
}
else{
QString str = lineEdit[0]->text();
str.chop(1);
lineEdit[0]->setText(str);
}
}
void Dialog::slotClear(){}
void Dialog::slotClearAll(){
lineEdit[0]->setText("0");
lineEdit[1]->clear();
lineEdit[2]->clear();
appendable = false;
operationEnd = true;
result=0;
currOperand=0;
prevOperator="";
memoryUsable=false;
}
void Dialog::slotMC(){
memoryUsable=false;
}
void Dialog::slotMR(){
if(!memoryUsable) return;
lineEdit[0]->setText(QString::number(memoryValue));
appendable=true;
}
void Dialog::slotMS(){
QString tmp = lineEdit[0]->text();
memoryValue=tmp.toDouble();
memoryUsable=true;
}
void Dialog::slotMPlus(){}
void Dialog::slotDigits(){
QString tmp = lineEdit[0]->text();
QString inputNum = static_cast<QPushButton*>(sender())->text();
if(tmp=="0"&&inputNum=="0") return;
if(!appendable){
lineEdit[0]->clear();
appendable=true;
}
lineEdit[0]->setText(lineEdit[0]->text()+inputNum);
calculable=true;
}
void Dialog::slotDot(){
QString tmp = lineEdit[0]->text();
for(int i=0;i<tmp.length();++i) if(tmp.at(i)=='.') return;
lineEdit[0]->setText(tmp+'.');
}
void Dialog::slotPlusMinus(){
QString tmp = lineEdit[0]->text();
if(tmp=="0") return;
if(tmp.at(0)=='-') tmp.remove(0,1);
else tmp="-"+tmp;
lineEdit[0]->setText(tmp);
}
void Dialog::slotMainOp(){
QString currOperator = static_cast<QPushButton*>(sender())->text();
currOperand = lineEdit[0]->text().toDouble();
if(!calculable){
QString tmp = lineEdit[1]->text();
tmp.chop(1);
lineEdit[1]->setText(tmp+currOperator);
prevOperator = currOperator;
return;
}
calculable = false;
lineEdit[1]->setText(lineEdit[1]->text() +" "+ QString::number(currOperand)+" "+currOperator);
if(operationEnd){
result=currOperand;
operationEnd=false;
}
else calculate(currOperand);
prevOperator = currOperator;
appendable=false;
currOperand=0;
}
void Dialog::calculate(double value){
if(prevOperator=="+")
result += value;
else if(prevOperator=="-")
result -= value;
else if(prevOperator=="x")
result *= value;
else
result /= value;
lineEdit[0]->setText(QString::number(result));
}
void Dialog::slotOp(){
QString currOperator = static_cast<QPushButton*>(sender())->text();
QString tmp = lineEdit[0]->text();
if(currOperator=="Sqrt")
result = sqrt(tmp.toDouble());
else if(currOperator=="x^2")
result*=result;
else result = 1/result;
lineEdit[0]->setText(QString::number(result));
prevOperator = currOperator;
appendable=false;
currOperand=0;
}
void Dialog::slotEqual(){
lineEdit[1]->clear();
if(currOperand==0) currOperand = lineEdit[0]->text().toDouble();
QString tmp = QString::number(result)+" "+prevOperator+" "+QString::number(currOperand)+" = ";
calculate(currOperand);
lineEdit[2]->setText(tmp+QString::number(result));
appendable=false;
operationEnd=true;
}
|
dialog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QGroupBox>
#include<QGridLayout>
#include<QPushButton>
#include<QLineEdit>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
QGridLayout * mainLayout;
QLineEdit *lineEdit[3];
//QGroupBox * grpBox;
void createGridLayout();
private:
bool memoryUsable;
double memoryValue;
bool appendable;
bool operationEnd;
bool calculable;
double result;
double currOperand;
QString prevOperator;
void calculate(double);
public slots:
void slotBackSpace();
void slotClear();
void slotClearAll();
void slotMC();
void slotMR();
void slotMS();
void slotMPlus();
void slotDigits();
void slotDot();
void slotPlusMinus();
void slotMainOp();
void slotOp();
void slotEqual();
};
#endif // DIALOG_H
|
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
|
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
|
'프로그래밍 > QT' 카테고리의 다른 글
[프로그래밍/Qt] notepad 메모장 기능 구현 예제 (0) | 2019.05.20 |
---|