QT實現簡單計算器
一、模組圖
二、核心演算法 —– 中綴表示式轉化為字尾表示式
1、將中綴表示式轉換為字尾表示式的演算法思想:
1)計算機實現轉換:
·開始掃描;
·數字時,加入字尾表示式;
·運算子:
a. 若為 ‘(‘,入棧;
b. 若為 ‘)’,則依次把棧中的的運算子加入字尾表示式中,直到出現’(‘,從棧中刪除’(’ ;
c. 若為 除括號外的其他運算子, 當其優先順序高於除’(‘以外的棧頂運算子時,直接入棧。否則從棧頂開始,依次彈出比當前處理的運算子優先順序高和優先順序相等的運算子,直到一個比它優先順序低的或者遇到了一個左括號為止。
·當掃描的中綴表示式結束時,棧中的的所有運算子出棧;
2)人工實現轉換
這裡我給出一箇中綴表示式:a+b*c-(d+e)
第一步:按照運算子的優先順序對所有的運算單位加括號:式子變成了:((a+(b*c))-(d+e))
第二步:轉換字首與字尾表示式
字首:把運算子號移動到對應的括號前面
則變成了:-( +(a *(bc)) +(de))
把括號去掉:-+a*bc+de 字首式子出現
字尾:把運算子號移動到對應的括號後面
則變成了:((a(bc)* )+ (de)+ )-
把括號去掉:abc*+de+- 字尾式子出現
發現沒有,字首式,字尾式是不需要用括號來進行優先順序的確定的。如表示式:3+(2-5)*6/3
字尾表示式 棧
3_________________+
3 ______
3 2 _____+(-
3 2 5 -___ +
3 2 5 - ___+*
3 2 5 - 6 * _+/
3 2 5 - 6 *3 ____+/
3 2 5 - 6 *3 /+__
(“_“用於隔開字尾表示式與棧)
另外一個人認為正確的轉換方法:
遍歷中綴表示式的每個節點,如果:
1、 該節點為運算元:
直接拷貝進入字尾表示式
2、 該節點是運算子,分以下幾種情況:
A、 為“(”運算子:
壓入臨時堆疊中
B、 為“)”運算子:
不斷地彈出臨時堆疊頂部運算子直到頂部的運算子是“(”為止,從棧中刪除’(‘。並把彈出的運算子都新增到字尾表示式中。
C、 為其他運算子,有以下步驟進行:
比較該運算子與臨時棧棧頂指標的運算子的優先順序,如果臨時棧棧頂指標的優先順序大於等於該運算子的優先順序,彈出並新增到字尾表示式中,反覆執行前面的比較工作,直到遇到一個棧頂指標的優先順序低於該運算子的優先順序,停止彈出新增並把該運算子壓入棧中。
此時的比較過程如果出現棧頂的指標為‘(’,則停止迴圈並把該運算子壓入棧中,注意:‘(’不要彈出來。
遍歷完中綴表示式之後,檢查臨時棧,如果還有運算子,則全部彈出,並新增到字尾表示式中。
2)運用字尾表示式進行計算的具體做法:
建立一個棧S 。從左到右讀表示式,如果讀到運算元就將它壓入棧S中,如果讀到n元運算子(即需要引數個數為n的運算子)則取出由棧頂向下的n項按運算元運算,再將運算的結果代替原棧頂的n項,壓入棧S中 。如果字尾表示式未讀完,則重複上面過程,最後輸出棧頂的數值則為結束。
**三、程式碼
**
mycalculator.h:
#ifndef MYCALCULATOR_H
#define MYCALCULATOR_H
#include <QWidget>
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QPalette>
#include <QStack>
#include <QPainter>
#include <QDateTime>
namespace Ui
{
class MyCalculator;
}
class MyCalculator : public QMainWindow
{
Q_OBJECT
private:
QLineEdit *lineEdit;//顯示框
QString input; //輸入框
void MYcalculate();
private slots://定義槽函式
void buttonClicked();
public:
explicit MyCalculator(QWidget *parent = 0);
~MyCalculator();
// 增加 paintEvent() 的宣告
protected:
void paintEvent(QPaintEvent *); //重寫paintEvent()
private:
Ui::MyCalculator *ui;
};
#endif // MYCALCULATOR_H
main.cpp:
#include "mycalculator.h"
#include <QApplication>
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyCalculator w;
w.setFixedHeight(525);
w.setFixedWidth(300);
w.move(QApplication::desktop()->rect().center()-QPoint(200,250));
w.show();
return a.exec();
}
mycalculator.cpp:
#include "mycalculator.h"
#include "ui_mycalculator.h"
void Change(const char *S,char OPS[],int &len);
void EXchange(char B[], int len, double &result,bool &flag);
//建構函式
MyCalculator::MyCalculator(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyCalculator)
{
// 通過樣式表給視窗設定背景圖
// "MyCalculator" 為類名
// "../test/test.jpg": 為圖片路徑,相對於可執行程式的相對路徑
this->setStyleSheet("MyCalculator{background-image: url(../MyCalculator/background1.png);} ");
ui->setupUi(this);
connect(ui->Button_0,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號0與槽函式
connect(ui->Button_1,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號1與槽函式
connect(ui->Button_2,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號2與槽函式
connect(ui->Button_3,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號3與槽函式
connect(ui->Button_4,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號4與槽函式
connect(ui->Button_5,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號5與槽函式
connect(ui->Button_6,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號6與槽函式
connect(ui->Button_7,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號7與槽函式
connect(ui->Button_8,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號8與槽函式
connect(ui->Button_9,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號9與槽函式
connect(ui->Button_add,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號+與槽函式
connect(ui->Button_sub,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號-與槽函式
connect(ui->Button_mul,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號*與槽函式
connect(ui->Button_div,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號/與槽函式
connect(ui->Button_backward,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號1/X與槽函式
connect(ui->Button_C,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號C與槽函式
connect(ui->Button_CE,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號CE與槽函式
connect(ui->Button_XY,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號X^Y與槽函式
connect(ui->Button_equal,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號=與槽函式
connect(ui->Button_right,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號)與槽函式
connect(ui->Button_remain,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號%與槽函式
connect(ui->Button_time,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號Time與槽函式
connect(ui->Button_left,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號(與槽函式
connect(ui->Button_point,SIGNAL(clicked()),this,SLOT(buttonClicked()));//訊號.與槽函式
QPalette palette;
palette.setColor(QPalette::Text,Qt::red);//設定字型顏色
ui->lineEdit->setFont(QFont("Timers",20,QFont::Bold));//設定字型大小
ui->lineEdit->setPalette(palette);
input = "0";
}
//解構函式
MyCalculator::~MyCalculator()
{
delete ui;
}
// 重寫paintEvent()
void MyCalculator::paintEvent(QPaintEvent *)
{
QStyleOption opt; // 需要標頭檔案#include <QStyleOption>
opt.init(this);
QPainter p(this); // 需要標頭檔案#include <QPainter>
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void MyCalculator::buttonClicked()//按鈕觸動槽函式
{
lineEdit = ui->lineEdit;
input = lineEdit->text();//輸入框
QPushButton *tb = qobject_cast<QPushButton *>(sender());//把訊號的傳送者轉換成pushbutton型別
QString text = tb->text();//text:獲取或設定文字框的文字內容。
if(text == QString("CE"))
{
text = input.left(input.length()-1); //減去一字元
lineEdit->setText(text);
}
else if(text == QString("C"))//清空所有
{
input="";
lineEdit->setText(input);
}
else if(text == QString("±"))
{
if(input=="")
text='-';
else
{
QString::iterator p=text.end();
p--;
if('-'==*p)
text=text.left(text.length()-1);
else
text=text+'-';
}
lineEdit->setText(text);
}
else if(text == QString("1/X"))
{
if(input != "")
lineEdit->setText("1/"+input);
else
{
lineEdit->setText("divisor can't be 0");
}
}
else if(text == QString("X^Y"))
{
if(input != "")
lineEdit->setText(input+"^");
}
else if(text == QString("Time"))
{
text=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm"); //格式化輸出當前時間
lineEdit->setText(text);
}
else if(text == QString("="))
{
std::string str=input.toStdString();//QString轉化為String
const char *S=str.c_str();//整個輸入框的字串
char OPS[50];//中綴表示式
int len;//去掉括號之後中綴表示式的長度
double result;//計算結果
QString change;
bool flag1 = true;//判斷出書是否為0的標誌
Change(S,OPS,len);
EXchange(OPS,len,result,flag1);
if(flag1 == false)
{
lineEdit->clear();
lineEdit->setText("divisor can't be 0");
}
else
{
change=QString::number(result,10,6);//將計算結果轉換為字串
lineEdit->setText(change);
}
}
else//+ - * / % 0-9
{
lineEdit->setText(input+=text);
}
}
void Change(const char *S,char OPS[],int &len)//將中綴表示式轉變為字尾表示式
{
QStack<char> OPE;//符號棧
unsigned int i,j=0;
unsigned int tmp = strlen(S);
for (i = 0; i < tmp; i++)
{
switch (S[i])
{
case'+':
if(OPE.isEmpty())//棧為空
OPE.push(S[i]);
else if (OPE.top() == '*' || OPE.top() == '/' || OPE.top() == '%' || OPE.top()=='^')
{
OPS[j++] = OPE.pop();//彈出比'+'運算子優先順序高和相等的運算子,依次加入字尾表示式
i--;
}
else
OPE.push(S[i]);
break;
case'-':
if(i!=0 && '('!=S[i-1])//正數
{
if(OPE.isEmpty())//棧為空
OPE.push(S[i]);
else if (OPE.top() == '*' || OPE.top() == '/'|| OPE.top() == '%' || OPE.top()=='^')//彈出比'-'運算子優先順序高和相等的運算子,依次加入字尾表示式
{
OPS[j++] = OPE.pop();
i--;
}
else
OPE.push(S[i]);
}
else//負數
{
while ((S[i] >= '0'&&S[i] <= '9' ) || S[i] == '.' || ('-'==S[i]&&(S[i-1]<'0'||S[i-1]>'9')))
{
OPS[j++] = S[i];
if('-'==S[i])
OPS[j++]='@';
i++;
}
i--;
OPS[j++] = '#'; //數字中的間隔符
}
break;
case '^':
OPE.push(S[i]);
break;
case'*':
if(OPE.isEmpty())//棧為空
OPE.push(S[i]);
else if (OPE.top() == '^')
{
OPS[j++] = OPE.pop();//彈出比'/'運算子優先順序高和相等的運算子,依次加入字尾表示式
i--;
}
else
OPE.push(S[i]);
break;
case'/':
if(OPE.isEmpty())//棧為空
OPE.push(S[i]);
else if (OPE.top() == '^')
{
OPS[j++] = OPE.pop();//彈出比'/'運算子優先順序高和相等的運算子,依次加入字尾表示式
i--;
}
else
OPE.push(S[i]);
break;
case '%':
if(OPE.isEmpty())//棧為空
OPE.push(S[i]);
else if (OPE.top() == '^'|| OPE.top()=='*' || OPE.top()=='/')
{
OPS[j++] = OPE.pop();//彈出比'%'運算子優先順序高和相等的運算子,依次加入字尾表示式
i--;
}
else
OPE.push(S[i]);
break;
case'(':
OPE.push(S[i]);
break;
case')':
while (OPE.top() != '(')//依次把棧中的運算子加入字尾表示式並將其出棧
{
OPS[j++] = OPE.pop();
}
OPE.pop();//從棧中彈出'('
break;
default:
while ((S[i] >= '0'&&S[i] <= '9') || S[i] == '.' || ('-'==S[i]&&S[i-1]<'0'&&S[i-1]>'9'))
{
OPS[j++] = S[i];
i++;
}
i--;
OPS[j++] = '#'; //數字中的間隔符
break;
}
}
while (!OPE.isEmpty())
{
OPS[j++] = OPE.pop();
}
len = j;
}
void EXchange(char B[], int len, double &result,bool &flag)//用字尾表示式計算結果
{
int i;
double a;
double b;
double c;
QStack<double>SZ;
for (i = 0; i < len; i++)
{
switch (B[i])
{
case'^':
{
a = SZ.pop();
b = SZ.pop();
c = pow(b,a);
SZ.push(c);
}
break;
case'+':
{
a = SZ.pop();
b = SZ.pop();
c = b + a;
SZ.push(c);
}
break;
case'-':
{
if('@'!=B[i+1])
{
a = SZ.pop();
b = SZ.pop();
c = b - a;
SZ.push(c);
}
else
{
int jx = 0;
double dx;
char *stx = new char[10];
while (B[i] != '#')
{
if('@'!=B[i])
stx[jx++] = B[i];
i++;
}
dx = atof(stx);//把字串轉換成浮點數
SZ.push(dx);
delete stx;
}
}
break;
case'*':
{
a = SZ.pop();
b = SZ.pop();
c = b*a;
SZ.push(c);
}
break;
case'/':
{
a = SZ.pop();
b = SZ.pop();
if (a == 0)
{
flag = false;
return;
}
c = b / a;
SZ.push(c);
}
break;
case'%':
{
a = SZ.pop();
b = SZ.pop();
if (a == 0)
{
flag = false;
return;
}
c = (int)b % (int)a;
SZ.push(c);
}
break;
default:
int j = 0;
double d;
char *st = new char[10];
while (B[i] != '#')
{
st[j++] = B[i];
i++;
}
d = atof(st);//把字串轉換成浮點數
SZ.push(d);
delete st;
break;
}
}
result=SZ.top();
}
介面檔案:mycalculator.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MyCalculator</class>
<widget class="QMainWindow" name="MyCalculator">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>302</width>
<height>546</height>
</rect>
</property>
<property name="windowTitle">
<string>MyCalculator</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="Button_time">
<property name="geometry">
<rect>
<x>80</x>
<y>450</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Time</string>
</property>
</widget>
<widget class="QPushButton" name="Button_6">
<property name="geometry">
<rect>
<x>130</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>6</string>
</property>
</widget>
<widget class="QPushButton" name="Button_0">
<property name="geometry">
<rect>
<x>30</x>
<y>450</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
<widget class="QPushButton" name="Button_7">
<property name="geometry">
<rect>
<x>30</x>
<y>330</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>7</string>
</property>
</widget>
<widget class="QPushButton" name="Button_point">
<property name="geometry">
<rect>
<x>130</x>
<y>450</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>.</string>
</property>
</widget>
<widget class="QPushButton" name="Button_C">
<property name="geometry">
<rect>
<x>180</x>
<y>290</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>C</string>
</property>
</widget>
<widget class="QPushButton" name="Button_1">
<property name="geometry">
<rect>
<x>30</x>
<y>410</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
<widget class="QPushButton" name="Button_8">
<property name="geometry">
<rect>
<x>80</x>
<y>330</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>8</string>
</property>
</widget>
<widget class="QPushButton" name="Button_5">
<property name="geometry">
<rect>
<x>80</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
<widget class="QPushButton" name="Button_remain">
<property name="geometry">
<rect>
<x>230</x>
<y>330</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>%</string>
</property>
</widget>
<widget class="QPushButton" name="Button_mul">
<property name="geometry">
<rect>
<x>180</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>*</string>
</property>
</widget>
<widget class="QPushButton" name="Button_4">
<property name="geometry">
<rect>
<x>30</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>4</string>
</property>
</widget>
<widget class="QPushButton" name="Button_XY">
<property name="geometry">
<rect>
<x>230</x>
<y>290</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>X^Y</string>
</property>
</widget>
<widget class="QPushButton" name="Button_3">
<property name="geometry">
<rect>
<x>130</x>
<y>410</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>3</string>
</property>
</widget>
<widget class="QPushButton" name="Button_add">
<property name="geometry">
<rect>
<x>180</x>
<y>450</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>+</string>
</property>
</widget>
<widget class="QPushButton" name="Button_equal">
<property name="geometry">
<rect>
<x>230</x>
<y>410</y>
<width>41</width>
<height>61</height>
</rect>
</property>
<property name="text">
<string>=</string>
</property>
</widget>
<widget class="QPushButton" name="Button_backward">
<property name="geometry">
<rect>
<x>230</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>1/X</string>
</property>
</widget>
<widget class="QPushButton" name="Button_sub">
<property name=
相關推薦
QT實現簡單計算器
一、模組圖
二、核心演算法 —– 中綴表示式轉化為字尾表示式
1、將中綴表示式轉換為字尾表示式的演算法思想:
1)計算機實現轉換:
·開始掃描;
·數字時,加入字尾表示式;
·運算子:
a. 若為 ‘(‘,入棧;
b. 若為 ‘)
QT C++實現簡單計算器(仿windows計算器普通模式)
寫的過程很痛,網上找不到相關的詳細的分析,所以想著發一個吧。
一
二
版本看自己喜好了,我的是5.4.2。
三
類名首字母大寫。
四
//calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include
QT實現簡單的計算器
效果圖:
新建工程,建立類MainWindow,基類是QMainWindow,宣告變數和函式、槽
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#i
Struts1應用、實現簡單計算器、使用DispatchAction、顯示友好的報錯信息、使用動態Form簡化開發
實體類 ica setattr sources 建立 -s number asc rlogin
實現簡單的支持加、減、乘、除的計算器
復制一份Struts1Demo改動:Struts1Calc
方案1: Struts1Calc
創建ActionForm:
CalcFor
Shell 實現簡單計算器功能
shell 計算器Shell 實現簡單計算器功能,腳本如下:[[email protected]/* */ scripts]# cat jisuan.sh
#!/bin/bash
print_usage(){
printf $"USAGE:$0 NUM1 {+|-|*|/} NUM2\n"
$.ajax()實現簡單計算器
ctype clas text bmi sub javascrip val numeric put 1、html頁面 a.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta c
Qt Creator簡單計算器的Demo
用法 乘除 .cpp ble 數據結構 特意 mark bre temp 小編在期末數據結構課設中遇到要做可視化界面的問題,特意去學習了一下Qt的用法,今天就來給大家分享一下。
我用的是Qt5.80,當然這只是一個簡易的計算器Demo,,請大家勿噴。
首先我創建了一個Qt
qt 實現簡單聊天(一個服務器和多個客服端)
qt源碼地址:https://github.com/haidragon/easyChat思路 :
一個服務器一直接聽某個 ip 的某個端口listen(QHostAddress::Any,port);2.一個服務器有一個容器保存所有各客服端的鏈接(每個鏈接都是一個類)。QList<TcpClient
QT實現簡單驗證碼
ase 窗口 事件 實現 draw date() res pragma init 主要思路:
在QT designer 中繪制QLabel控件
自定義類繼承QLabel類,並提升至控件
提升至
生成隨機數
重寫paintEvent繪制圖形
重寫mousePressEvent
qt 之簡單計算器
在這裡插入圖片描述 工程檔案,的說明如上圖, 設計的視窗和計算演示如下: 其中test.h的程式碼如下:
#ifndef TEST1_H
#define TEST1_H
#include <QtWidgets/QMainWindow>
#include "ui_test
【python/qt】Python+Qt實現簡單的視訊監控介面
DATE: 2018.12.9
1、前言
這個介面是之前讀研時候學習QT時寫的一個簡單的介面,主要實現了人臉檢測部分的功能,比較簡單。
從今年3月份就開始寫這個視訊監控的功能,一直拖到了11月份。找工作結束後,可以好好研究一下Python和Qt以及兩者的混合程式設計了。
JS 實現簡單計算器功能
// if(symbol != null){
console.log(aNum, bNum);
getResult(symbol, aNum, bNum);
show.innerHTML = result;
java GUI 實現簡單計算器
package calclutor;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame {
// 這一句的
利用tkinter實現簡單計算器功能(不使用eval函式)
利用tkinter實現簡單計算器功能(不使用eval函式)
一、思路
tkinter:
佈置主介面;
上部為數字顯示介面;
下部為數字鍵與功能鍵介面;
邏輯:
程式只考慮兩個運算元進行計算的情況,不考慮複雜情況
展示:
c++棧實現簡單計算器
/* 棧實現計算器,主要思路就是設定一個符號棧和一個數字棧,在字串首尾各加一個'#',然後掃描字串,
* 如果是數字進數字棧,如果是運算子號先判斷符號優先順序,若棧外符號優先順序大於棧內符號優先順序則進棧,
* 小於棧內優先順序則符號棧出棧一位,數字棧出棧兩位進行計算,
Struts2.5.17--實現簡單計算器
如果第一次接觸struts2可以參考文章初次使用Struts2.5.17 1.實現Action物件(業務邏輯處理) result.java
package jisuanqi;
import com.opensymphony.xwork2.ActionContext;
import c
Python 藉助逆波蘭表示式(字尾表示式)實現簡單計算器
Python 藉助逆波蘭表示式(字尾表示式)實現簡單計算器
文章目錄
Python 藉助逆波蘭表示式(字尾表示式)實現簡單計算器
0. 參考資料
1. 中綴表示式轉字尾表示式
2. 字尾表示式的求值
3. Python
Java實現簡單計算器、抽票程式
計算器:
1 import java.awt.BorderLayout;
2 import java.awt.Container;
3 import java.awt.Font;
4 import java.awt.GridLayout;
5 import java.awt.eve
用Qt實現的計算器
需求分析
模組圖
類圖
核心演算法
中綴表示式轉化為字尾表示式
規則:從左到右遍歷中綴表示式(表示式運算子在兩數字之間,比如(2+1)3)的每個數字和符號,若是數字就輸出,即成為字尾表示式(表示式運算子在數字之後,不包含括號
python tkinter實現簡單計算器
功能分析
基礎功能
需要有顯示區,可以使用label,text,entry
顯示按鍵0-9以及運算子
滑鼠點選按鍵時,按鍵的值或者運算結果能夠在顯示區顯示
能夠清空顯示區以備下一次運算輸入