课程作业六

作业要求

  1. 本次作业要求实现核心算法,请将表达式生成的代码及相关的检验、计算表达式结果的代码贴在博客中,并对代码进行必要的解释。
  2. 发表一篇博客,博客内容为:提供本次作业的github链接,本次程序运行的截图,对界面编程的探索。

作业链接

表达式生成
Qt代码

关于表达式生成

正如以前说过的 采用后缀表达式的方法

(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

运行截图

表达式

1093404-20170605025406965-1209566621.jpg

界面化

1093404-20170605004340086-978709009.jpg

转载于:https://www.cnblogs.com/circlek/p/6926112.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值