Skip to content

Commit 414fb6b

Browse files
committed
Conflicts: computer.pro.user
2 parents c597e47 + 7a2e9b8 commit 414fb6b

File tree

5 files changed

+97
-11
lines changed

5 files changed

+97
-11
lines changed

computer.pro.user

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3+
<<<<<<< HEAD
34
<!-- Written by QtCreator 3.4.1, 2015-06-25T00:54:30. -->
5+
=======
6+
<!-- Written by QtCreator 3.4.1, 2015-06-25T17:13:24. -->
7+
>>>>>>> 7a2e9b8a3a528b40a431977f5e0f735425bbde72
48
<qtcreator>
59
<data>
610
<variable>EnvironmentId</variable>
7-
<value type="QByteArray">{64a7b332-4bd0-4e67-b1ce-01f1d3e482dd}</value>
11+
<value type="QByteArray">{df87cc51-22e0-4c70-9073-e8dea8d2ba15}</value>
812
</data>
913
<data>
1014
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -228,7 +232,7 @@
228232
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
229233
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">computer</value>
230234
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">computer2</value>
231-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/coding/qt/ComputerByQt/computer.pro</value>
235+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/github/ComputerByQt/computer.pro</value>
232236
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
233237
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">computer.pro</value>
234238
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>

utility.cpp

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* 5)当中序表达式的符号序列全部读入后,若栈内仍有元素,把它们依次弹出并放到后序表达式序列尾部。
1515
* 若弹出的元素遇到空括号,则说明不匹配,发生错误,并进行相关处理
1616
**/
17-
bool inToPost(QString &infixexp , vector<QString> &postfixexp)
17+
bool inToPost(QString &infixexp , QVector<QString> &postfixexp)
1818
{
19-
if(infixexp.isNull()) return ;
19+
if(infixexp.isNull()) return false;
2020
QString *number = new QString();
21-
QStack<char> stack;
21+
QStack<QChar> stack;
2222
for(int var = 0; var < infixexp.size(); ++var)
2323
{
2424
if(infixexp.at(var)>='0' && infixexp.at(var)<='9'){
@@ -27,19 +27,19 @@ bool inToPost(QString &infixexp , vector<QString> &postfixexp)
2727
stack.push_back(infixexp.at(var));
2828
}else if (')' == infixexp.at(var)) {
2929
if(stack.isEmpty()) return false;
30-
char oper;
30+
QChar oper;
3131
while(!stack.isEmpty()){
3232
oper = stack.pop();
3333
if(oper != '(')
3434
postfixexp.push_back(oper);
3535
}
3636
}else if (isoperator(infixexp.at(var))){
3737
if(!number->isEmpty()){
38-
postfixexp.push(*number);
38+
postfixexp.push_back(*number);
3939
number->clear();
4040
}
4141
//operator handle
42-
42+
handleOperator(infixexp.at(var),stack,postfixexp);
4343
}else{
4444
//Invalid input
4545
return false;
@@ -49,10 +49,56 @@ bool inToPost(QString &infixexp , vector<QString> &postfixexp)
4949
}
5050

5151

52-
bool handleOperator(char oper, QStack<char> & stack , vector<Qstring> & postfixexp)
52+
bool handleOperator(QChar oper, QStack<QChar> & stack , QVector<QString> &postfixexp)
5353
{
54+
QChar topElement;
5455
while(!stack.isEmpty())
5556
{
57+
//popElement = stack.pop();
58+
topElement=stack.top();
59+
if(topElement == '(' || !isHigherPriority(oper , topElement)){
60+
postfixexp.push_back(stack.pop());
61+
}else{
62+
break;
63+
}
64+
}
65+
stack.push_back(oper);
66+
return true;
67+
}
68+
69+
70+
bool isHigherPriority(const QChar oper1, const QChar oper2)
71+
{
72+
QChar higerOperator[3] = {'*' , '/' , '%'};
73+
QChar lowerOperator[2] = {'+' , 'c'};
74+
for (int var = 0; var < 3; ++var) {
75+
if(oper1 == higerOperator[var]){
76+
for (int i = 0; i < 2; ++i) {
77+
if(oper2 == lowerOperator[i]){
78+
return true;
79+
}
80+
}
81+
}
82+
}
83+
return false;
84+
}
5685

86+
bool isoperator(QChar oper)
87+
{
88+
QChar operatorList[5] = {'+','-','*','/','%'};
89+
for (int var = 0; var < 5; ++var) {
90+
if(oper == operatorList[var])
91+
return true;
5792
}
93+
return false;
5894
}
95+
96+
void printQVector(QVector<QString> & vec)
97+
{
98+
QVector<QString>::const_iterator it_string = vec.begin();
99+
qDebug() << vec.size();
100+
while(it_string != vec.end()){
101+
qDebug() << *it_string;
102+
}
103+
104+
}

utility.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define UTILITY
33
#include <QString>
44
#include <QStack>
5+
#include <QVector>
6+
#include <QDebug>
57

68

79
const int DEFAULT_MODE = 1;
@@ -13,9 +15,30 @@ const int DELETE_MODE = 5;
1315
* @param infixexp
1416
* @return
1517
*/
16-
bool inToPost(QString &infixexp, vector<QString> &postfixexp);
18+
bool inToPost(QString &infixexp, QVector<QString> &postfixexp);
1719

18-
bool handleOperator(char oper, QStack<char> & stack , vector<Qstring> & postfixexp);
20+
/**
21+
* @brief handleOperator
22+
* @param oper
23+
* @param stack
24+
* @param postfixexp
25+
* @return
26+
*/
27+
bool handleOperator(QChar oper, QStack<QChar> & stack , QVector<QString> & postfixexp);
28+
29+
/**
30+
* @brief isHigherPriority: Only support + - * / %
31+
* @param oper1
32+
* @param oper2
33+
* @return
34+
*/
35+
bool isHigherPriority(const QChar oper1, const QChar oper2);
36+
37+
38+
bool isoperator(QChar oper);
39+
40+
41+
void printQVector(QVector<QString> & vec);
1942

2043

2144
#endif // UTILITY

widget.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Widget::Widget(QWidget *parent) :
3030

3131
//connect(ui->operatorbuttonsqrt , SIGNAL(clicked()) , this, SLOT(getOperatorInput()));
3232

33+
connect(ui->operatorbuttonequal , SIGNAL(clicked()) , this , SLOT(getComputeResult()));
34+
3335
}
3436

3537
Widget::~Widget()
@@ -101,3 +103,13 @@ void Widget::updateSequenceExpression(const QString &exp)
101103
sequenceExp.clear();
102104
sequenceExp = exp;
103105
}
106+
107+
void Widget::getComputeResult()
108+
{
109+
QVector<QString> vec;
110+
if(!ui->digitinput->toPlainText().isEmpty())
111+
appendSequenceExpression(ui->digitinput->toPlainText());
112+
qDebug() << getSequenceExpression();
113+
inToPost(getSequenceExpression() , vec);
114+
printQVector(vec);
115+
}

widget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private slots:
2727
void getDigitInput();
2828
void getOperatorInput();
2929
void deleteDigitInput();
30+
void getComputeResult();
3031

3132
private:
3233
void setReadOnlyForButton();

0 commit comments

Comments
 (0)