作业要求
- 本次作业要求实现核心算法,请将表达式生成的代码及相关的检验、计算表达式结果的代码贴在博客中,并对代码进行必要的解释。
- 发表一篇博客,博客内容为:提供本次作业的github链接,本次程序运行的截图,对界面编程的探索。
作业链接
关于表达式生成
正如以前说过的 采用后缀表达式的方法
(2017/06/05更新)
生成写出来了 不过表达式还没有想到要如何排除的是形如 "(1+(2*3)+1)"的表达式
这里只放如何计算的代码(有改动)
string convert_InfixExp_to_PostFix(const string s)
{
stack<char> conv;
string PostFix = string();
while (conv.empty() == false) conv.pop();
int i = 0;
while (1)
{
if (static_cast<unsigned int>(i) >= s.length()) break;
char now = s.at(i);
if (isalnum(now) != false)
{
PostFix += now;
i++;
while ((static_cast<unsigned int>(i) < s.length() && (isalnum(s.at(i)) != 0)))
{
PostFix += s.at(i);
i++;
}
PostFix += " ";
}
else
{
if (now == ')')
{
if (conv.empty() == true)
throw invalid_argument("The expression is invalid!");
while (conv.top() != '(')
{
PostFix += conv.top();
PostFix += " ";
if (!conv.empty()) conv.pop();
if (conv.empty() == true)
throw invalid_argument("The expression is invalid!");
}
conv.pop();
i++;
}
else if ((now == '+') || (now == '-') || (now == '*') || (now == '\/'))
{
while ((conv.empty() != true) && (conv.top() != '(')
&& ((pri_oper.find(conv.top()))->second
>= (pri_oper.find(now)->second)))
{
PostFix += conv.top();
PostFix += " ";
if (!conv.empty()) conv.pop();
}
conv.push(now);
i++;
}
else if (now == '(')
{
conv.push(now);
i++;
}
}
}
while (conv.empty() == false)
{
PostFix += conv.top();
PostFix += " ";
if (!conv.empty()) conv.pop();
}
return PostFix;
}
double calculate_PostFix(const string s)
{
stack<double> cal;
while (cal.empty() !=true)
cal.pop();
double now;
int i = 0;
while (1)
{
if (static_cast<unsigned int>(i) >= s.length()) break;
if (isalnum(s.at(i)) != 0)
{
now = 0;
int wid = 0;
int pos = i;
while ((static_cast<unsigned int>(i) < s.length()) && (isalnum(s.at(i)) != 0))
{
wid++;
i++;
}
now += static_cast<double>(stoi(string(s, pos, wid)));
cal.push(now);
while ((static_cast<unsigned int>(i) < s.length()) && s.at(i) == ' ') i++;
}
else if ((static_cast<unsigned int>(i) < s.length()) && s.at(i) == ' ')
{
while ((static_cast<unsigned int>(i) < s.length()) && s.at(i) == ' ') i++;
}
else
{
double x = 0, y = 0;
if (cal.empty() != true)
{
x = cal.top();
cal.pop();
if (cal.empty() != true)
{
y = cal.top();
cal.pop();
switch (s.at(i))
{
case '+': cal.push(x + y); break;
case '-': cal.push(y - x); break;
case '*': cal.push(x * y); break;
case '/': cal.push(y / x); break;
}
i++;
}
}
}
}
if (cal.empty() != true)
return cal.top();
else return -1;
}
关于Qt
Qt提供了大量适用于GUI的库,目前我所学到的部分中最核心的部分是信号槽机制。
利用Qobject::connect(...)
函数链接信号发送者sender的信号给信号接收者receicer,从而完成事件间的联系
当然这是自定义的做法。。。我正在学习 目前只是按照示例打了一个小小的程序
//main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QLabel label("hello world!");
label.show();
return a.exec();
}
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include <Qstring>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_QuitButton_clicked()
{
QCoreApplication::quit();
}
void MainWindow::on_actionOpen_triggered()
{
QString fileName=QFileDialog::getOpenFileName(this,
tr("Open File"), QString(),tr("Text Files(*.txt);;C++ Files(*.cpp *.h)"));
if (!fileName.isEmpty())
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::critical(this,tr("Error"),tr("Could not open file"));
return ;
}
QTextStream in(&file);
ui->textEdit->setText(in.readAll());
file.close();
}
}
void MainWindow::on_actionSave_triggered()
{
QString fileName=QFileDialog::getSaveFileName(this,
tr("Save File"),QString(),
tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
if (!fileName.isEmpty())
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::critical(this,tr("Error"),tr("Could not open file"));
}
else
{
QTextStream stream(&file);
stream<< (ui->textEdit)->toPlainText();
stream.flush();
file.close();
}
}
}
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_QuitButton_clicked();
void on_actionOpen_triggered();
void on_actionSave_triggered();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H