基于QT实现的计算器(只需要简单的栈知识,不仅仅是四则运算,接近手机内置计算器功能)
參考文獻:Qt 5.9 C++開發指南 (王維波等 著)及眾多網上資料
1.問題定義及需求分析
課題目的:
由輸入的四則算術表達式字符串,動態生成算術表達式所對應的后綴式,通過后綴式求值并輸出;
輸出的形式:界面上:以QT中的Text Browser進行輸出結果。后臺:通過suffix文件輸出表達式的后綴表達式。
程序的功能:在布局和功能上,我們以小米內置計算器為標準。在運算上:我們實現了三角函數,反三角函數,次方,根號,ln,log,1/x,階乘的在允許括號和簡寫的復合運算。在輸出上:我們實現了退格,多次運算換行輸出,一次ac一次運算的功能。
測試數據:通過圖形界面輸入多個表達式,多次操作檢驗每個按鈕的功能。
2.概要設計
抽象數據類型的定義:起初我們通過數組實現了一個char-stack,實現pop,push,top操作。
主程序的流程:創造圖形界面,打開文件suffix,通過pushbutoon進行交互,輸出結果。
各程序模塊之間的調用關系:main函數里調用qt內置的圖形界面創建程序,接著在圖形界面的模塊上(我們采用了dialog為基礎窗口)調用我們自己寫bds_qz.h模塊。
3.詳細設計
3.1.圖形界面的設計:我們通過QT的designer設計模式設計了像小米內置計算器的界面。
3.2. 運算算法:我們參考了網上的四則運算的算法,但同時又進行了一些自己的創新。
首先,我們處理的是一個中綴運算表達式,我們從左向右開始遍歷,分為運算符和數字的判斷。
數字的判斷及存儲:
存儲:我們沒有采取棧的結構,我們用了一個double數組加一個int numpos代表最前面的數字位置,實現存儲和增減操作。 代碼:double num[50] ;int numpos = 0;判斷:整數部分:碰到一個數字則往后遍歷找直到不為數字,期間遍歷的字符通過ASCII碼值減去0的ASCII碼值映射,再通過10為基的乘法映射求出整數。
小數部分:在遍歷完整數部分后,立即判斷后面有沒有小數點,如果有以整數的方法不過以0.1為基求出小數部分。
符號部分:當我們遍歷時碰到第一個數字立即往前找前面是不是(——,如果是則在最終轉化完后乘以負一。
數字特殊情況分為π和e,π和e則采用了判斷等效替代的方法(如如果按了π按鈕,則用3.1415926代替)。
代碼:if (bds[i] <= '9'&&bds[i] >= '0')//為數字的情況,轉換為1個數,如234??????????? {
??????????????? num[++numpos] = 0;
??????????????? while (bds[i] <= '9'&&bds[i] >= '0')
??????????????? {
??????????????????? num[numpos] *= 10;
????????? ??????????num[numpos] += (bds[i] - '0');
??????????????????? ++i;
??????????????? }
??????????????? if (bds[i] == '.')
??????????????? {
??????????????????? double f_car=0.1;//定義基數
??????????????????? ++i;
??????????????????? while (bds[i] <= '9'&&bds[i] >= '0')
??????????????????? {
??????????????????????? num[numpos] += ((bds[i] - '0')*f_car);
??????????????????????? f_car *= 0.1;
??????????????????????? ++i;
??????????????????? }
??????????????? }//計算小數點
?? else if(bds[i]=='('&&bds[i+1]=='-')
?????????? ?{
??????????????? i+=2;
??????????????? num[++numpos] = 0;
??????????????? while (bds[i] <= '9'&&bds[i] >= '0')
??????????????? {
??????????????????? num[numpos] *= 10;
??????????????????? num[numpos] += (bds[i] - '0');
??????????????????? ++i;
????????? ??????}
??????????????? if (bds[i] == '.')
??????????????? {
??????????????????? double f_car=0.1;//定義基數
??????????????????? ++i;
??????????????????? while (bds[i] <= '9'&&bds[i] >= '0')
??????????????????? {
??????????????????????? num[numpos] += ((bds[i] - '0')*f_car);
??????????????????????? f_car *= 0.1;
??????????????????????? ++i;
?????????????? ?????}
??????????????? }//計算小數點
??????????????? num[numpos]*=-1;
??????????????? suffix<<num[numpos]<<' ';
??????????????? i+=1;
??????????? }//
??????????? else if(bds[i]=='p')
??????????? {
??????????????? num[++numpos]=3.14159265358;
???????????????? suffix<<"Π"<<' ';
??????????????? ++i;
??????????? }
??????????? else if(bds[i]=='e')
??????????? {
??????????????? num[++numpos]=2.71828182845;
??????????????? suffix<<'e'<<' ';
??????????????? ++i;
??????????? }
運算符的判斷,映射,存儲,操作,:
判斷:不為數字且不為括號且不為π和e及負號。
映射:因為有的操作運算符比如ln,log等,它不如+-等占一個字節,但我們遍歷時一個個字節遍歷字符串的,所以我們采用了映射技術。
代碼:std::string char_change(char s){
??????? if(s=='*')
??????????? return "x";
??????? else if(s=='/')
?????????? return? "÷";
??????? else if(s=='n')
??????????? return? "ln";
??????? else if(s=='g')
?????????? return? "lg";
??????? else if(s=='p')
??????????? return "Π";
??????? else if(s=='s')
?????????? return? "sin";
??????? else if(s=='c')
??????????? return? "cos";
??????? else if(s=='t')
?????????? return? "tan";
??????? else if(s=='a')
????????? return? "arcsin";
??????? else if(s=='b')
??????????? return? "arccos";
??????? else if(s=='d')
?????????? return? "arctan";
??????? else if(s=='\n')
??????????? return "\r\n";
??????? else if(s=='u')
?????????? return? "°";
??????? else
??????? {
??????????? std::string ss(1,s);
??????????? return ss;
??????? }
}
存儲:采取棧的結構。
?????? 代碼:char pop(stack *p){
??? if (p->size == 0)
??? {
??????? printf("空棧");
??????? return '\0';//拋出/0字符代表空
??? }
??? else
??? {
??????? --(p->size);
??????? return p->a[p->size];
??? }
}
char top(stack *p)
{
??? if (p->size == 0)
??????? return '\0';//拋出/0字符代表空
??? else
??? {
??????? return p->a[p->size - 1];
??? }
}
int empty(stack *p)
{
??? return p->size==0;
}
void push(stack *p, char b)
{
??? p->a[p->size] = b;
??? ++p->size;
}
操作:對于一個運算符,我們先判斷棧是否為空,為空直接入棧,如果棧不為空,我們則通過一個優先級函數分別求出棧頂運算符的優先級,和當前運算符的優先級,并將他們進行比較。如果棧頂的元素優先級大于等于當前運算符,執行出棧操作,直到棧為空或者棧頂元素優先級小于當前運算符,后將當前運算符入棧。注意的是()會改變運算順序,我們只將(入棧,如果碰到)就將左括號到棧頂的元素全部彈出。
出棧操作代碼:else
??????????? {
?
?????????????? if (empty(ysf))//棧為空的情況
??????????????????? push(ysf, bds[i]);
??????????????? else
??? ????????????{
??????????????????? if (bds[i] == '(')
??????????????????????? push(ysf, bds[i]);
??????????????????? else if (bds[i] == ')')//右括號的情況
??????????????????? {
??????????????????????? while (top(ysf) != '(')
??????????????????????? {
??????????? ????????????????operat(num,numpos,top(ysf));//調用對數據運算的函數
??????????????????????????? suffix<<char_change(top(ysf))<<' ';
?????????????????????????? pop(ysf);
??????????????????????? }
??????????????????????? pop(ysf);//彈出右括號
??????????????????? }
???????? ???????????else
??????????????????? {
??????????????????????? while (compare(bds[i])<=compare(top(ysf)))//優先級映射函數
??????????????????????? {
??????????????????????????? operat(num,numpos,top(ysf));
????????????????????????? suffix<<char_change(top(ysf))<<' ';
???????????????????????????? pop(ysf);
??????????????????????? }
??????????????????????? push(ysf, bds[i]);
??????????????????? }
??????????????? }
??????????????? ++i;
??????????? }
??????? }
??????? while (!empty(ysf))//最后的出棧
??????? {
???????????? operat(num,numpos,top(ysf));
????????????? suffix<<char_change(top(ysf))<<' ';
???????????? pop(ysf);
??????? }
優先級函數代碼:int compare(char a)//用于比較優先級{
??? int i;
??? switch (a)
??? {
??? case '+':
??????? i = 1;
??????? break;
??? case '-':
??????? i = 1;
? ??????break;
??? case '*':
??????? i = 2;
??????? break;
??? case '/':
??????? i = 2;
??????? break;
??? case '^':
??????? i = 3;
??????? break;
??? case 'v':
??????? i =4;
??????? break;
??? case 's':
??????? i =4;
??????? break;
??? case 'c':
??????? i =4;
??????? break;
??? case 't':
??????? i =4;
??????? break;
??? case 'g':
??????? i =4;
??????? break;
??? case 'n':
??????? i =4;
??????? break;
??? case 'a':
??????? i =4;
??????? break;
??? case 'b':
??????? i =4;
??????? break;
??? case 'd':
??????? i =4;
??????? break;
??? case '!':
??????? i =5;
??????? break;
??? case '%':
??????? i = 5;
??????? break;
??? case 'u':
??????? i = 9;
??????? break;
??? default:
??????? i = 0;
??????? break;
??? }
??? return i;
}
數字運算代碼:void merge(double *a, double b, char c)//用于將兩數字合并,注意前面的數字傳地址{
??? if (c == '-')
??? {
??????? (*a) -= b;
??? }
??? else if (c == '+')
??? {
??????? (*a) += b;
??? }
??? else if (c == '*')
??? {
??????? (*a) *= b;
??? }
??? else if(c=='/')
??????? (*a) /= b;
??? else if(c=='^')
???????? {*a=pow(*a,b);}
}
void operat(double *num,int &numpos,char c)
{
??? if (c == '%')
??? {
??????? num[numpos] /= 100;
??? }
??? else if(c=='v')
??? {
??????? num[numpos] = sqrt(num[numpos]);
??? }
??? else if(c=='g')
??? {
??????? num[numpos] = log10(num[numpos]);
??? }
??? else if(c=='n')
??? {
??????? num[numpos] = log1p(num[numpos]-1);
??? }
??? else if(c=='s')
??? {
??????? num[numpos] = sin(num[numpos]);
??? }
??? else if(c=='a')
??? {
??????? num[numpos] = asin(num[numpos]);
??? }
??? else if(c=='c')
?? ?{
??????? num[numpos] = cos(num[numpos]);
??? }
??? else if(c=='b')
??? {
??????? num[numpos] = acos(num[numpos]);
??? }
??? else if(c=='t')
??? {
??????? num[numpos] = tan(num[numpos]);
??? }
??? else if(c=='d')
??? {
??????? num[numpos] = atan(num[numpos]);
??? }
??? else if(c=='u')
??? {
??????? num[numpos] = num[numpos]*3.14159265358/180;
??? }
??? else if (c == '!')
??? {
??????? long sum=1;
??????? int nums=num[numpos];
??????? for(int i=2;i<=nums;++i)
??????? {
??????????? sum*=i;
??????? }
??????? num[numpos] =sum;
??? }
??? else
??? {
??????? merge(&num[numpos - 1], num[numpos], c);
??????? --numpos;
??? }
}
4.調試分析:
4.1對所遇問題的解決方法及分析:
碰到的問題和改進較多。
首先輸出框上:
例如√運算符,我希望在在輸出框顯示根號,但QString確實寫入了√,但在insetrhtml時,卻加載不進去,迫不得已如圖 我們顯示了用v代替了√。
還有就是換行ac,因為實際在使用小米計算器時,它的ac時,一次ac一條運算。還有退格運算,你要讓它可以退格當前運算,而不影響前一次運算。這其中就涉及了一些字符串的處理,還有QString和string還有char*(因為我們最初寫的四則運算是純C的)轉換,花了一些時間。
運算功能上:
第一張圖為小米的,第二張為我們,仔細對比會發現,小米上是有deg和rad這個按鈕的,即它可以通過一個按鈕,來調整運算是采用弧度制還是角度制。而且它的文本框,還要隨del和ac時改變。考慮到開發難度,我們的創新就是添加一個新的運算符°(它擁有最高的優先級,將一個數?π*180),實現了相同的功能。
其次就是小米的計算器是實時運算的,既不用按=也會計算結果,但我們這個不是,我們放棄了這個功能,因為如果這樣,就必須實時調用函數,而不是按一次=調用(占用了更多的資源),而且我們沒有非法檢測,如果中途表達式錯了(如sin(90°+1)!)我們無法檢驗出錯,故放棄了。
額外:還有一個問題就是發布程序的問題,希望自己寫的東西在別人的電腦也能運行。起初單純的一位按照release編譯就行,然后 ,經過上網查閱,我們知道是程序的依賴關系的問題,它缺乏相應的dll,而且吧因為QT裝的是VS的編譯器,還需要msvc的dll。在經歷了諸多查閱和實踐后,我們解決了這個問題,也讓我們意識到程序的發布其實也是一個復雜的問題。
4.2算法的時空分析及改進思想
由于算法分為了很多部分,對于一次=運算核心部分是遍歷一遍表達式字符數組復雜度為O(n)。但在循環里面,涉及一層while循環的出棧操作,不過仔細分析,每個運算符入棧和出棧操作是唯一的,即它們的復雜度一定小于O(2n),故一次=運算總復雜度為O(n)。
整個程序的復雜度將是你執行運算的次數*O(n)。
QT版:
?
控制臺四則運算版:
7.附錄
Main.c
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
?
??? QApplication a(argc, argv);
??? Dialog w;
??? w.show();
??? return a.exec();
}
bds_qz.h
#ifndef BDS_QZ_H
#define BDS_QZ_H
#include<string>
#include<sstream>
#include <stdio.h>
#include <stdlib.h>
#include<cmath>
#include<fstream>
#define n 50
typedef struct
{
??? char a[n];
??? int size;//表示棧中含有的元素數量
} stack;
char pop(stack *p);
char top(stack *p);
int empty(stack *p);
void push(stack *p, char b);
int compare(char a);//用于比較優先級
void merge(double *a, double b, char c);//用于將兩數字合并,注意前面的數字傳地址
std::string qiuzhi(const char *bds);
std::string char_change(char s);
#endif // BDS_QZ_H
?
bds_qz.cpp
#include"bds_qz.h"
char pop(stack *p)
{
??? if (p->size == 0)
??? {
??????? printf("空棧");
??????? return '\0';//拋出/0字符代表空
??? }
??? else
??? {
??????? --(p->size);
??????? return p->a[p->size];
??? }
}
char top(stack *p)
{
??? if (p->size == 0)
??????? return '\0';//拋出/0字符代表空
??? else
??? {
??????? return p->a[p->size - 1];
??? }
}
int empty(stack *p)
{
??? return p->size==0;
}
void push(stack *p, char b)
{
??? p->a[p->size] = b;
??? ++p->size;
}
int compare(char a)//用于比較優先級
{
??? int i;
??? switch (a)
??? {
??? case '+':
??????? i = 1;
??????? break;
??? case '-':
??????? i = 1;
??????? break;
??? case '*':
??????? i = 2;
??????? break;
??? case '/':
??????? i = 2;
??????? break;
??? case '^':
??????? i = 3;
??????? break;
??? case 'v':
??????? i =4;
??????? break;
??? case 's':
??????? i =4;
??????? break;
??? case 'c':
??????? i =4;
??????? break;
??? case 't':
??????? i =4;
??????? break;
??? case 'g':
??????? i =4;
??????? break;
??? case 'n':
??????? i =4;
??????? break;
??? case 'a':
??????? i =4;
??????? break;
??? case 'b':
??????? i =4;
??????? break;
??? case 'd':
??????? i =4;
??????? break;
??? case '!':
??????? i =5;
??????? break;
??? case '%':
??????? i = 5;
??????? break;
??? case 'u':
??????? i = 9;
??????? break;
??? default:
??????? i = 0;
??????? break;
??? }
??? return i;
}
std::string char_change(char s)
{
??????? if(s=='*')
??????????? return "x";
??????? else if(s=='/')
?????????? return? "÷";
??????? else if(s=='n')
??????????? return? "ln";
??????? else if(s=='g')
?????????? return? "lg";
??????? else if(s=='p')
??????????? return "Π";
??????? else if(s=='s')
?????????? return? "sin";
??????? else if(s=='c')
??????????? return? "cos";
??????? else if(s=='t')
?????????? return? "tan";
??????? else if(s=='a')
????????? return? "arcsin";
??????? else if(s=='b')
??????????? return? "arccos";
??????? else if(s=='d')
?????????? return? "arctan";
??????? else if(s=='\n')
??????????? return "\r\n";
??????? else if(s=='u')
?????????? return? "°";
??????? else
??????? {
??????????? std::string ss(1,s);
??????????? return ss;
??????? }
}
void merge(double *a, double b, char c)//用于將兩數字合并,注意前面的數字傳地址
{
??? if (c == '-')
??? {
??????? (*a) -= b;
??? }
??? else if (c == '+')
??? {
??????? (*a) += b;
??? }
??? else if (c == '*')
??? {
??????? (*a) *= b;
??? }
??? else if(c=='/')
??????? (*a) /= b;
??? else if(c=='^')
???????? {*a=pow(*a,b);}
}
void operat(double *num,int &numpos,char c)
{
??? if (c == '%')
??? {
??????? num[numpos] /= 100;
??? }
??? else if(c=='v')
??? {
??????? num[numpos] = sqrt(num[numpos]);
??? }
??? else if(c=='g')
??? {
??????? num[numpos] = log10(num[numpos]);
??? }
??? else if(c=='n')
??? {
??????? num[numpos] = log1p(num[numpos]-1);
??? }
??? else if(c=='s')
??? {
??????? num[numpos] = sin(num[numpos]);
??? }
??? else if(c=='a')
??? {
??????? num[numpos] = asin(num[numpos]);
??? }
??? else if(c=='c')
??? {
?? ?????num[numpos] = cos(num[numpos]);
??? }
??? else if(c=='b')
??? {
??????? num[numpos] = acos(num[numpos]);
??? }
??? else if(c=='t')
??? {
??????? num[numpos] = tan(num[numpos]);
??? }
??? else if(c=='d')
??? {
??????? num[numpos] = atan(num[numpos]);
??? }
??? else if(c=='u')
??? {
??????? num[numpos] = num[numpos]*3.14159265358/180;
??? }
??? else if (c == '!')
??? {
??????? long sum=1;
??????? int nums=num[numpos];
??????? for(int i=2;i<=nums;++i)
??????? {
??????????? sum*=i;
??????? }
??????? num[numpos] =sum;
??? }
??? else
??? {
??????? merge(&num[numpos - 1], num[numpos], c);
??????? --numpos;
??? }
}
std::string qiuzhi(const char *bds)
{
??? std::ofstream suffix;
??? suffix.open("suffix.txt",std::ios::out|std::ios::binary|std::ios::app);
??? int i = 0;
??????? stack *ysf = (stack*)malloc(sizeof(stack));//為表達式開辟一個stack
??????? ysf->size = 0;
??????? double num[50] ;//用于求值的數組
??????? int numpos = 0;//用于求值的數組位置//ps因為num這個棧用的操作非常的少,而stack沒有泛化,所以沒有采用棧
??????? while (bds[i] != '=')
??????? {
??????????? if (bds[i] <= '9'&&bds[i] >= '0')//為數字的情況,轉換為1個數,如234
?? ?????????{
??????????????? num[++numpos] = 0;
??????????????? while (bds[i] <= '9'&&bds[i] >= '0')
??????????????? {
??????????????????? num[numpos] *= 10;
??????????????????? num[numpos] += (bds[i] - '0');
??????????????????? ++i;
??????????????? }
????? ??????????if (bds[i] == '.')
??????????????? {
??????????????????? double f_car=0.1;//定義基數
??????????????????? ++i;
??????????????????? while (bds[i] <= '9'&&bds[i] >= '0')
??????????????????? {
??????????????????????? num[numpos] += ((bds[i] - '0')*f_car);
??????????????????????? f_car *= 0.1;
??????????????????????? ++i;
?????????????? ?????}
??????????????? }//計算小數點
??????????????? suffix<<num[numpos]<<' ';
??????????? }
??????????? else if(bds[i]=='('&&bds[i+1]=='-')
??????????? {
??????????????? i+=2;
??????????????? num[++numpos] = 0;
??????????????? while (bds[i] <= '9'&&bds[i] >= '0')
??????????????? {
??????????????????? num[numpos] *= 10;
??????????????????? num[numpos] += (bds[i] - '0');
??????????????????? ++i;
??????????????? }
??????????????? if (bds[i] == '.')
??????????????? {
??????????????????? double f_car=0.1;//定義基數
??? ????????????????++i;
??????????????????? while (bds[i] <= '9'&&bds[i] >= '0')
??????????????????? {
??????????????????????? num[numpos] += ((bds[i] - '0')*f_car);
??????????????????????? f_car *= 0.1;
??????????????????????? ++i;
??????????????????? }
??? ????????????}//計算小數點
??????????????? num[numpos]*=-1;
??????????????? suffix<<num[numpos]<<' ';
??????????????? i+=1;
??????????? }//
??????????? else if(bds[i]=='p')
??????????? {
??????????????? num[++numpos]=3.14159265358;
???????????????? suffix<<"Π"<<' ';
??????? ????????++i;
??????????? }
??????????? else if(bds[i]=='e')
??????????? {
??????????????? num[++numpos]=2.71828182845;
??????????????? suffix<<'e'<<' ';
??????????????? ++i;
??????????? }
??????????? else
??????????? {
?
?????????????? if (empty(ysf))//棧為空的情況
??????????????????? push(ysf, bds[i]);
??????????????? else
??????????????? {
??????????????????? if (bds[i] == '(')
??????????????????????? push(ysf, bds[i]);
??????????????????? else if (bds[i] == ')')//右括號的情況
??????????????????? {
?????????????????? ?????while (top(ysf) != '(')
??????????????????????? {
??????????????????????????? operat(num,numpos,top(ysf));//調用對數據運算的函數
??????????????????????????? suffix<<char_change(top(ysf))<<' ';
?????????????????????????? pop(ysf);
??????????????????????? }
???? ???????????????????pop(ysf);//彈出右括號
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? while (compare(bds[i])<=compare(top(ysf)))//優先級映射函數
??????????????????????? {
??????????????????????????? operat(num,numpos,top(ysf));
????????????????????????? suffix<<char_change(top(ysf))<<' ';
???????????????????????????? pop(ysf);
??????????????????????? }
??????????????????????? push(ysf, bds[i]);
??????????????????? }
??????????????? }
??????????????? ++i;
??????????? }
?? ?????}
??????? while (!empty(ysf))//最后的出棧
??????? {
???????????? operat(num,numpos,top(ysf));
????????????? suffix<<char_change(top(ysf))<<' ';
???????????? pop(ysf);
??????? }
??????? suffix<<'\n';
??????? suffix.close();
??????? std::stringstream ss;
?????????? ss<<num[1];
?????????? free (ysf);
?????????? return ss.str();
}
dialog.h
??#ifndef DIALOG_H#define DIALOG_H
#include <QDialog>
#include<string>
namespace Ui {
class Dialog;
}
?
class Dialog : public QDialog
{
??? Q_OBJECT
??? std::string bds;//用于運算的bds
??? QString out_bds;//用于輸出的bds
public:
??? void init_();//對圖形界面預處理
??? explicit Dialog(QWidget *parent = nullptr);
??? QString string_change(std::string s);//把表達式轉化為輸出的表達式
??? ~Dialog();
?
private slots:
?
?? void on_pushButton_sin_clicked();
?
?? void on_pushButton_2nd_clicked();
?
?? void on_pushButton_rad_clicked();
?
?? void on_pushButton_cos_clicked();
?
?? void on_pushButton_tan_clicked();
?
?? void on_pushButton_pow_clicked();
?
?? void on_pushButton_lg_clicked();
?
?? void on_pushButton_left_clicked();
?
?? void on_pushButton_ln_clicked();
?
?? void on_pushButton_right_clicked();
?
?? void on_pushButton_sqrt_clicked();
?
?? void on_pushButton_ac_clicked();
?
?? void on_pushButton_del_clicked();
?
?? void on_pushButton_div_clicked();
?
?? void on_pushButton_mul_clicked();
?
?? void on_pushButton_fac_clicked();
?
?? void on_pushButton_num7_clicked();
?
?? void on_pushButton_num8_clicked();
?
?? void on_pushButton_num9_clicked();
?
?? void on_pushButton_sub_clicked();
?
?? void on_pushButton_rec_clicked();
?
?? void on_pushButton_num4_clicked();
?
?? void on_pushButton_num5_clicked();
?
?? void on_pushButton_num6_clicked();
?
?? void on_pushButton_add_clicked();
?
?? void on_pushButton_pi_clicked();
?
?? void on_pushButton_num1_clicked();
?
?? void on_pushButton_num2_clicked();
?
?? void on_pushButton_num3_clicked();
?
?? void on_pushButton_equal_clicked();
?
?? void on_pushButton_e_clicked();
?
?? void on_pushButton_per_clicked();
?
?? void on_pushButton_num0_clicked();
?
?? void on_pushButton_point_clicked();
?
?? void on_textBrowser_textChanged();
?
private:
??? Ui::Dialog *ui;
};
?
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include<bds_qz.h>
Dialog::Dialog(QWidget *parent) :
??? QDialog(parent),
??? ui(new Ui::Dialog)
{
??? ui->setupUi(this);
?????? init_();
}
?
Dialog::~Dialog()
{
??? delete ui;
}
void Dialog::init_()
{//ui->textBrowser->setStyleSheet("QTextBrowser{border-width:0;border-style:outset}");
???? ui->textBrowser->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
??? ui->textBrowser->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//關閉滾動條
?? // ui->pushButton_2nd->setFlat(true);
}
?? QString Dialog:: string_change(std::string s)
? {
?? int i=0;
? std:: string new_bds;
?? while(s[i]!='\0')
?? {
?????? new_bds+=char_change(s[i]);
?????? ++i;
?? }
??? QString qn_bds=QString(QString::fromLocal8Bit(new_bds.c_str()));
??? return qn_bds;
?? }
void Dialog::on_pushButton_sin_clicked()
{
??? if(ui->pushButton_sin->text()=="sin")
?? { bds+='s';
????? ui->textBrowser->insertHtml("sin");}
??? else
??? {
??????? bds+='a';
??? ui->textBrowser->insertHtml("arcsin");}
}
?
void Dialog::on_pushButton_2nd_clicked()
{
if(ui->pushButton_sin->text()=="sin")
{ui->pushButton_sin->setText("arcsin");
??? ui->pushButton_cos->setText("arccos");
??????? ui->pushButton_tan->setText("arctan");
}
else
{
??? ui->pushButton_sin->setText("sin");
???? ui->pushButton_cos->setText("cos");
???? ui->pushButton_tan->setText("tan");
}
}
?
void Dialog::on_pushButton_rad_clicked()
{
??? bds+='u';
??? QString str = QString::fromLocal8Bit("°");//為了顯示中文的°
?ui->textBrowser->insertHtml(str);
}
?
void Dialog::on_pushButton_cos_clicked()
{
??? if(ui->pushButton_sin->text()=="sin")
?? { bds+='c';
????? ui->textBrowser->insertHtml("cos");}
??? else
??? {
??????? bds+='b';
??? ui->textBrowser->insertHtml("arccos");}
}
?
void Dialog::on_pushButton_tan_clicked()
{
??? if(ui->pushButton_sin->text()=="sin")
?? { bds+='t';
????? ui->textBrowser->insertHtml("tan");}
??? else
??? {
??????? bds+='d';
??? ui->textBrowser->insertHtml("arctan");}
}
?
void Dialog::on_pushButton_pow_clicked()
{
??? bds+='^';
??? ui->textBrowser->insertHtml("^");
}
?
void Dialog::on_pushButton_lg_clicked()
{
??? bds+='g';
??? ui->textBrowser->insertHtml("lg");
}
?
void Dialog::on_pushButton_left_clicked()
{
??? bds+='(';
??? ui->textBrowser->insertHtml("(");
}
?
void Dialog::on_pushButton_ln_clicked()
{
??? bds+='n';
??? ui->textBrowser->insertHtml("ln");
}
?
void Dialog::on_pushButton_right_clicked()
{
??? bds+='c';
??? ui->textBrowser->insertHtml("cos");
}
?
void Dialog::on_pushButton_sqrt_clicked()
{
??? bds+='v';
???? QString str = QString::fromLocal8Bit("√");
ui->textBrowser->insertHtml(str);
}
?
void Dialog::on_pushButton_ac_clicked()
{
??? if(!bds.empty())
??? { int? star=0;
???? if(bds[bds.length()-1]!='\n')//如果不是下一行的話
????? {for(int i=0;bds[i]!='\0';++i)
????? {if(bds[i]=='\n')
????????????? star=i+1;}}
???? else
??????????? {? for(int i=0;bds[i]!='\r';++i)
????????????????? if(bds[i]=='\n')
????????????????????? star=i+1;?? }
????? bds.erase(star);
????? QString s=string_change(bds);
??? ui->textBrowser->setPlainText(s);}
}
?
void Dialog::on_pushButton_del_clicked()
{
??? if(!bds.empty())
?? { int bds_end=bds.length()-1;
??? if(bds[bds_end]!='\n')
??? bds.erase(bds.length()-1);//如果不是上一行的話就刪除一個字符
?? //QString s= QString::fromStdString(bds);
? QString s=string_change(bds);
ui->textBrowser->setPlainText(s);}
}
?
void Dialog::on_pushButton_div_clicked()
{
??? bds+='/';
??? QString str = QString::fromLocal8Bit("÷");
??? ui->textBrowser->insertHtml(str);
}
?
void Dialog::on_pushButton_mul_clicked()
{
??? bds+='*';
??? QString str = QString::fromLocal8Bit("x");
??? ui->textBrowser->insertHtml(str);
}
?
void Dialog::on_pushButton_fac_clicked()
{
??? bds+='!';
??? ui->textBrowser->insertHtml("!");
}
?
void Dialog::on_pushButton_num7_clicked()
{
??? bds+='7';
??? ui->textBrowser->insertHtml("7");
}
?
void Dialog::on_pushButton_num8_clicked()
{
??? bds+='8';
??? ui->textBrowser->insertHtml("8");
}
?
void Dialog::on_pushButton_num9_clicked()
{
??? bds+='9';
??? ui->textBrowser->insertHtml("9");
}
?
void Dialog::on_pushButton_sub_clicked()
{
??? bds+='-';
??? ui->textBrowser->insertHtml("-");
}
?
void Dialog::on_pushButton_rec_clicked()
{
??? bds+="^(-1)";
??? ui->textBrowser->insertHtml("^(-1)");
}
?
void Dialog::on_pushButton_num4_clicked()
{
??? bds+='4';
??? ui->textBrowser->insertHtml("4");
}
?
void Dialog::on_pushButton_num5_clicked()
{
??? bds+='5';
??? ui->textBrowser->insertHtml("5");
}
?
void Dialog::on_pushButton_num6_clicked()
{
??? bds+='6';
??? ui->textBrowser->insertHtml("6");
}
?
void Dialog::on_pushButton_add_clicked()
{
??? bds+='+';
??? ui->textBrowser->insertHtml("+");
}
?
void Dialog::on_pushButton_pi_clicked()
{
??? bds+='p';
??? ui->textBrowser->insertHtml("Π");
}
?
void Dialog::on_pushButton_num1_clicked()
{
??? bds+='1';
??? ui->textBrowser->insertHtml("1");
}
?
void Dialog::on_pushButton_num2_clicked()
{
??? bds+='2';
??? ui->textBrowser->insertHtml("2");
}
?
void Dialog::on_pushButton_num3_clicked()
{
??? bds+='3';
??? ui->textBrowser->insertHtml("3");
}
?
void Dialog::on_pushButton_equal_clicked()
{
??? bds+='=';
??? const char *c_bds=bds.data();
???? std::string result=qiuzhi(c_bds);
???? bds+=result;
???? bds+="\r\n";
???? bds+=result;
??? QString qresult=QString::fromStdString(result);
??? ui->textBrowser->insertHtml("=");
???? ui->textBrowser->insertHtml(qresult);
????? ui->textBrowser->insertHtml("<br>");
????? ui->textBrowser->insertHtml(qresult);
}
?
void Dialog::on_pushButton_e_clicked()
{
??? bds+='e';
??? ui->textBrowser->insertHtml("e");
}
?
void Dialog::on_pushButton_per_clicked()
{
??? bds+='%';
??? ui->textBrowser->insertHtml("%");
}
?
void Dialog::on_pushButton_num0_clicked()
{
??? bds+='0';
??? ui->textBrowser->insertHtml("0");
}
?
void Dialog::on_pushButton_point_clicked()
{
??? bds+='.';
??? ui->textBrowser->insertHtml(".");
}
?
void Dialog::on_textBrowser_textChanged()
{
???? ui->textBrowser->moveCursor(QTextCursor::End);
}
總結
以上是生活随笔為你收集整理的基于QT实现的计算器(只需要简单的栈知识,不仅仅是四则运算,接近手机内置计算器功能)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java计算机毕业设计腾讯网游辅助小助手
- 下一篇: 微信小程序| Ngork内网传统+后台A