代码
widget.h
文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <vector>
#include <stack>
using namespace std;
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_oneButton_clicked();
void on_twoButton_clicked();
void on_threeButton_clicked();
void on_fourButton_clicked();
void on_fiveButton_clicked();
void on_sixButton_clicked();
void on_sevenButton_clicked();
void on_eightButton_clicked();
void on_nineButton_clicked();
void on_addButton_clicked();
void on_subButton_clicked();
void on_mulButton_clicked();
void on_divButton_clicked();
void on_zeroButton_clicked();
void on_CButton_clicked();
void on_backspaceButton_clicked();
void on_equalButton_clicked();
void on_dotButton_clicked();
private:
Ui::Widget *ui;
QString expression;
stack<string> nums,op;
QString eval();
string add(string& a,string& b),sub(string& a,string& b),mul(string& a,string& b),div(string& a,long long b,long long& r);
vector<int> To_vector(string& s);
string To_string(vector<int>& A);
bool cmp(string& a, string& b);
};
#endif // WIDGET_H
main
文件
#include "widget.h"
#include <QApplication>
#include <QLocale>
#include <QTranslator>
#include <QStyleFactory>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle(QStyleFactory::create("Fusion"));
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "calculator_1_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
Widget w;
w.show();
return a.exec();
}
widget.cpp
文件
#include "widget.h"
#include "./ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("计算器");
QFont f("仿宋", 14);
ui->lineEdit->setFont(f);
ui->equalButton->setShortcut(Qt::Key_Return); //设置按钮关联到Enter键
ui->equalButton->setShortcut(Qt::Key_Enter);//将小键盘回车键与登录按钮绑定在一起
ui->backspaceButton->setShortcut(Qt::Key_Backspace);
ui->zeroButton->setShortcut(Qt::Key_0);
ui->oneButton->setShortcut(Qt::Key_1);
ui->twoButton->setShortcut(Qt::Key_2);
ui->threeButton->setShortcut(Qt::Key_3);
ui->fourButton->setShortcut(Qt::Key_4);
ui->fiveButton->setShortcut(Qt::Key_5);
ui->sixButton->setShortcut(Qt::Key_6);
ui->sevenButton->setShortcut(Qt::Key_7);
ui->eightButton->setShortcut(Qt::Key_8);
ui->nineButton->setShortcut(Qt::Key_9);
ui->addButton->setShortcut(Qt::Key_Plus);
ui->subButton->setShortcut(Qt::Key_Minus);
ui->mulButton->setShortcut(Qt::Key_Asterisk);
ui->divButton->setShortcut(Qt::Key_Slash);
ui->lineEdit->setReadOnly(true); // 设置为只读状态
// ui->lineEdit->setFont(QFont("0",16));
}
Widget::~Widget()
{
delete ui;
}
vector<int> Widget::To_vector(string& s) {
vector<int> A;
for (int i = s.size() - 1; i >= 0; i--) A.push_back(s[i] - '0');
return A;
}
string Widget::To_string(vector<int>& A) {
string s;
for (int i = A.size() - 1; i >= 0; i--) s += '0' + A[i];
return s;
}
// 比较a,b大小
bool Widget::cmp(string& a, string& b) {
if (a.size() == b.size()) {
for (int i = 0; i <= a.size() - 1; i++) {
if (a[i] != b[i]) return a[i] > b[i];
}
return true;
}
else return a.size() > b.size();
}
string Widget::sub(string& a, string& b) {
if (!cmp(a, b)) return ("-" + sub(b, a));
vector<int> A = To_vector(a), B = To_vector(b), C;
int t = 0;
for (int i = 0; i < A.size(); i++) {
t = t + A[i];
if (i < B.size()) t = t - B[i];
C.push_back((t + 10) % 10);
if (t < 0) t = -1;
else t = 0;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return To_string(C);
}
string Widget::add(string& a, string& b) {
vector<int> A = To_vector(a), B = To_vector(b);
vector<int> C(max(A.size(), B.size()) + 10, 0);
for (int i = 0; i < A.size(); i++) C[i] += A[i];
for (int i = 0; i < B.size(); i++) C[i] += B[i];
for (int i = 0; i < C.size(); i++) {
C[i + 1] += C[i] / 10;
C[i] = C[i] % 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return To_string(C);
}
string Widget::mul(string& a, string& b) {
vector<int> A = To_vector(a), B = To_vector(b);
vector<int> C(A.size() + B.size() + 10, 0);
for (int i = 0; i < A.size(); i++) {
for (int j = 0;j < B.size();j++) {
C[i + j] += A[i] * B[j]; //+=
}
}
for (int i = 0; i + 1 < C.size(); i++) {
C[i + 1] += C[i] / 10; //+=
C[i] = C[i] % 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return To_string(C);
}
// r是余数
string Widget::div(string& a, long long b, long long& r) {
vector<int> A = To_vector(a), C;
r = 0;
for (int i = A.size() - 1; i >= 0; i--) {
r = r * 10 + A[i];
C.push_back(r / b);
r = r % b;
}
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0) C.pop_back();
return To_string(C);
}
QString Widget::eval(){
string s=expression.toStdString(),firstnum,secondnum,outcome,option;
long long r;
for(char c:s){
if(c=='+'||c=='-'||c=='*'||c=='/'){
option += c;
continue;
}
if(option.size()) secondnum += c;
else firstnum += c;
}
if(secondnum.size()==0) return QString::fromStdString(firstnum);
if(option == "+") outcome=Widget::add(firstnum,secondnum);
if(option == "-") outcome=Widget::sub(firstnum,secondnum);
if(option == "*") outcome=Widget::mul(firstnum,secondnum);
if(option == "/") outcome=Widget::div(firstnum,stoll(secondnum),r);
return QString::fromStdString(outcome);
}
void Widget::on_zeroButton_clicked()
{
expression += "0";
ui->lineEdit->setText(expression);
}
void Widget::on_oneButton_clicked()
{
expression += "1";
ui->lineEdit->setText(expression);
}
void Widget::on_twoButton_clicked()
{
expression += "2";
ui->lineEdit->setText(expression);
}
void Widget::on_threeButton_clicked()
{
expression += "3";
ui->lineEdit->setText(expression);
}
void Widget::on_fourButton_clicked()
{
expression += "4";
ui->lineEdit->setText(expression);
}
void Widget::on_fiveButton_clicked()
{
expression += "5";
ui->lineEdit->setText(expression);
}
void Widget::on_sixButton_clicked()
{
expression += "6";
ui->lineEdit->setText(expression);
}
void Widget::on_sevenButton_clicked()
{
expression += "7";
ui->lineEdit->setText(expression);
}
void Widget::on_eightButton_clicked()
{
expression += "8";
ui->lineEdit->setText(expression);
}
void Widget::on_nineButton_clicked()
{
expression += "9";
ui->lineEdit->setText(expression);
}
void Widget::on_dotButton_clicked()
{
expression += ".";
ui->lineEdit->setText(expression);
}
void Widget::on_addButton_clicked()
{
expression = eval() + "+";
ui->lineEdit->setText(expression);
}
void Widget::on_subButton_clicked()
{
expression = eval() + "-";
ui->lineEdit->setText(expression);
}
void Widget::on_mulButton_clicked()
{
expression = eval() + "*";
ui->lineEdit->setText(expression);
}
void Widget::on_divButton_clicked()
{
expression = eval() + "/";
ui->lineEdit->setText(expression);
}
void Widget::on_CButton_clicked()
{
expression.clear();
ui->lineEdit->setText(expression);
}
void Widget::on_backspaceButton_clicked()
{
expression.chop(1);
ui->lineEdit->setText(expression);
}
void Widget::on_equalButton_clicked()
{
expression = eval();
ui->lineEdit->setText(expression);
}
calculator_1_zh_CN.ts
文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN"></TS>
widget
文件