• Qt creator实现一个简单计算器


    目录

    1 界面设计

    2 思路简介

    3 代码

    目录

    1 界面设计

    ​2 思路简介

    3 代码

    3.1 widget.h

    3.2 widget.c

    4 完整代码

            在这里主要记载了如何使用Qt creator完成一个计算器的功能。该计算器可以实现正常的加减乘除以及括号操作,能实现简单的计算器功能。

    1 界面设计

    计算器的界面设计如下所示:

    2 思路简介

            1. 中缀表达式的构建

            将按键输入字符转换为字符串的形式。

            比如:中缀表达式3+2+34+(6-3*5);

            那么可以定义一个字符数组char Chars[1024];存储,之后使用QString::fromUtf8(this->Chars);将该字符数组转换为LineEdit可以显示的类型。

            2. 中缀表达式转变为后缀表达式

            直接按照字符将中缀转变为后缀表达式,在进行计算时,再转换为数据和符号。这里在将中缀表达式转变为后缀表达式时,把每个数都使用&进行分割,方便后续计算。比如32+21转换为后缀表达式就是32&&21+。

            3. 使用后缀表达式进行计算

    3 代码

    3.1 widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. QT_BEGIN_NAMESPACE
    5. namespace Ui { class Widget; }
    6. QT_END_NAMESPACE
    7. class Widget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. Widget(QWidget *parent = nullptr);
    12. ~Widget();
    13. void Fb_Change(); // 中缀转换为后缀表达式
    14. void Clear();
    15. public:
    16. int i,j;
    17. private slots:
    18. void on_but_one_clicked();
    19. void on_but_zero_clicked();
    20. void on_but_two_clicked();
    21. void on_but_three_clicked();
    22. void on_but_four_clicked();
    23. void on_but_five_clicked();
    24. void on_but_six_clicked();
    25. void on_but_seven_clicked();
    26. void on_but_eight_clicked();
    27. void on_but_nine_clicked();
    28. void on_but_add_clicked();
    29. void on_but_sub_clicked();
    30. void on_but_mul_clicked();
    31. void on_but_div_clicked();
    32. void on_but_leftbrk_clicked();
    33. void on_but_rightbrk_clicked();
    34. void on_but_cls_clicked();
    35. void on_but_bit_clicked();
    36. void on_but_eql_clicked();
    37. void on_but_close_clicked();
    38. private:
    39. Ui::Widget *ui;
    40. int Data[128]; // data stack
    41. char suffix[128]; // 后缀表达式 stack
    42. char sign[128]; // 符号栈
    43. char Chars[1024]; // zhong缀表达式字符数组
    44. int data_sp = 0; // 数据栈顶指针
    45. int suffix_sp = 0; // 后缀栈顶指针
    46. int sign_sp = 0; // 符号栈顶指针
    47. int char_sp = 0; //
    48. int Operation_end = 0; // 运算结束标志位,1:运算结束
    49. };
    50. #endif // WIDGET_H

    3.2 widget.c

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include "qdebug.h"
    4. Widget::Widget(QWidget *parent)
    5. : QWidget(parent)
    6. , ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9. }
    10. Widget::~Widget()
    11. {
    12. delete ui;
    13. }
    14. void Widget::on_but_one_clicked()
    15. {
    16. this->Chars[char_sp++] = '1';
    17. Chars[char_sp] ='\0';
    18. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    19. }
    20. void Widget::on_but_zero_clicked()
    21. {
    22. this->Chars[char_sp++] = '0';
    23. Chars[char_sp] ='\0';
    24. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    25. }
    26. void Widget::on_but_two_clicked()
    27. {
    28. this->Chars[char_sp++] = '2';
    29. Chars[char_sp] ='\0';
    30. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    31. }
    32. void Widget::on_but_three_clicked()
    33. {
    34. this->Chars[char_sp++] = '3';
    35. Chars[char_sp] ='\0';
    36. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    37. }
    38. void Widget::on_but_four_clicked()
    39. {
    40. this->Chars[char_sp++] = '4';
    41. Chars[char_sp] ='\0';
    42. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    43. }
    44. void Widget::on_but_five_clicked()
    45. {
    46. this->Chars[char_sp++] = '5';
    47. Chars[char_sp] ='\0';
    48. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    49. }
    50. void Widget::on_but_six_clicked()
    51. {
    52. this->Chars[char_sp++] = '6';
    53. Chars[char_sp] ='\0';
    54. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    55. }
    56. void Widget::on_but_seven_clicked()
    57. {
    58. this->Chars[char_sp++] = '7';
    59. Chars[char_sp] ='\0';
    60. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    61. }
    62. void Widget::on_but_eight_clicked()
    63. {
    64. this->Chars[char_sp++] = '8';
    65. Chars[char_sp] ='\0';
    66. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    67. }
    68. void Widget::on_but_nine_clicked()
    69. {
    70. this->Chars[char_sp++] = '9';
    71. Chars[char_sp] ='\0';
    72. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    73. }
    74. void Widget::on_but_add_clicked()
    75. {
    76. this->Chars[char_sp++] = '+';
    77. Chars[char_sp] ='\0';
    78. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    79. }
    80. void Widget::on_but_sub_clicked()
    81. {
    82. this->Chars[char_sp++] = '-';
    83. Chars[char_sp] ='\0';
    84. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    85. }
    86. void Widget::on_but_mul_clicked()
    87. {
    88. this->Chars[char_sp++] = '*';
    89. Chars[char_sp] ='\0';
    90. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    91. }
    92. void Widget::on_but_div_clicked()
    93. {
    94. this->Chars[char_sp++] = '/';
    95. Chars[char_sp] ='\0';
    96. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    97. }
    98. void Widget::on_but_leftbrk_clicked()
    99. {
    100. this->Chars[char_sp++] = '(';
    101. Chars[char_sp] ='\0';
    102. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    103. }
    104. void Widget::on_but_rightbrk_clicked()
    105. {
    106. this->Chars[char_sp++] = ')';
    107. Chars[char_sp] ='\0';
    108. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    109. }
    110. void Widget::on_but_cls_clicked()
    111. {
    112. Clear();
    113. }
    114. void Widget::on_but_bit_clicked()
    115. {
    116. if(Operation_end == 0) // 没有运算结束
    117. {
    118. this->Chars[--char_sp] = ' ';
    119. Chars[char_sp] ='\0';
    120. ui->lineEdit->setText(QString::fromUtf8(this->Chars));
    121. }else {
    122. // 运算结束
    123. Clear();
    124. }
    125. }
    126. void Widget::on_but_eql_clicked()
    127. {
    128. Fb_Change();
    129. }
    130. void Widget::Clear()
    131. {
    132. this->char_sp = 0;
    133. ui->lineEdit->clear();
    134. for(i=0; i
    135. {
    136. this->sign[i] = ' ';
    137. }
    138. sign[0] ='\0';
    139. this->sign_sp = 0;
    140. for(i=0; i
    141. {
    142. this->suffix[i] = ' ';
    143. }
    144. this->suffix_sp = 0;
    145. suffix[0] ='\0';
    146. for(i=0; i
    147. {
    148. this->Data[i] = 0;
    149. }
    150. this->data_sp = 0;
    151. Operation_end = 0; // 运算结束标志位
    152. }
    153. // 中缀转换为后缀表达式
    154. void Widget::Fb_Change()
    155. {
    156. int font=0;
    157. int flag = 0;
    158. i = char_sp;
    159. j = 0;
    160. int count = 0;
    161. // 12*4+34/5-(56=67*4)+32 => 12 4 * 34 5 / 56 67 4 * + - 32 + +
    162. qDebug() << "1";
    163. while(i--) // 总共字符个数
    164. {
    165. if(isdigit(this->Chars[j])) // 如果是数字字符
    166. {
    167. if(flag == 0)
    168. {
    169. suffix[suffix_sp++] = this->Chars[j]; // !
    170. j++;
    171. }else{
    172. suffix[suffix_sp++] = '&'; // 主要是为了正确分割数据, 和flag配合
    173. suffix[suffix_sp++] = this->Chars[j]; // !
    174. j++;
    175. flag = 0;
    176. }
    177. continue;
    178. }
    179. if((sign_sp == 0 || sign[sign_sp-1] == '(') && this->Chars[j] != ')') // 如果符hao栈为空或者栈顶元素为'('
    180. {
    181. qDebug() << "null" <
    182. suffix[suffix_sp++] = '&'; // 主要是为了正确分割数据, 和flag配合
    183. sign[sign_sp++] = this->Chars[j]; // !
    184. j++;
    185. flag = 1; // 主要是为了正确分割数据
    186. continue;
    187. }
    188. //
    189. if((this->Chars[j] == '*' || this->Chars[j] == '/') && (sign[sign_sp-1] == '-' || sign[sign_sp-1] == '+'))
    190. {
    191. sign[sign_sp++] = this->Chars[j]; // !
    192. j++;
    193. flag = 1; //
    194. continue;
    195. }
    196. if((this->Chars[j] == '*' || this->Chars[j] == '/') && (sign[sign_sp-1] == '*' || sign[sign_sp-1] == '/'))
    197. {
    198. suffix[suffix_sp++] = '&'; // 主要是为了正确分割数据, 和flag配合
    199. suffix[suffix_sp++] = sign[--sign_sp];
    200. sign[sign_sp++] = this->Chars[j];
    201. j++;
    202. flag = 1; // 主要是为了正确分割数据
    203. continue;
    204. }
    205. if(this->Chars[j] == '+' || this->Chars[j] == '-')
    206. {
    207. suffix[suffix_sp++] = '&'; // 主要是为了正确分割数据, 和flag配合
    208. suffix[suffix_sp++] = sign[--sign_sp];
    209. sign[sign_sp++] = this->Chars[j];
    210. j++;
    211. flag = 1; //
    212. continue;
    213. }
    214. if(this->Chars[j] == '(')
    215. {
    216. sign[sign_sp++] = this->Chars[j]; // !
    217. j++;
    218. flag = 1; //
    219. continue;
    220. }
    221. if(this->Chars[j] == ')')
    222. {
    223. while(sign[--sign_sp] != '(')
    224. {
    225. suffix[suffix_sp++] = '&'; // 主要是为了正确分割数据, 和flag配合
    226. suffix[suffix_sp++] = sign[sign_sp];
    227. }
    228. sign[sign_sp] = ' ';
    229. j++;
    230. flag = 1; //
    231. continue;
    232. }
    233. }
    234. while(sign_sp--){
    235. suffix[suffix_sp++] = sign[sign_sp];
    236. }
    237. suffix[suffix_sp] = '\0';
    238. qDebug()<< suffix;
    239. // suffix compute
    240. int arg_01, arg_02;
    241. for(i=0; i < suffix_sp; i++)
    242. {
    243. if(suffix[i] >= '0' && suffix[i] <= '9')
    244. {
    245. font = font*count*10 + ((int)suffix[i] - 48); // 252
    246. count = 1;
    247. continue;
    248. }
    249. if(suffix[i] == '&')
    250. {
    251. if(count == 1) // 说明取过数
    252. {
    253. Data[data_sp++] = font;
    254. font = 0;
    255. }
    256. count = 0;
    257. continue;
    258. }
    259. if(suffix[i] == '+')
    260. {
    261. if(count == 1) // 说明取过数
    262. {
    263. Data[data_sp++] = font;
    264. font = 0;
    265. }
    266. arg_01 = Data[--data_sp]; //
    267. arg_02 = Data[--data_sp]; //
    268. Data[data_sp++] = arg_01+arg_02;
    269. qDebug() << Data[data_sp - 1];
    270. count = 0;
    271. continue;
    272. }
    273. if(suffix[i] == '-')
    274. {
    275. if(count == 1) // 说明取过数
    276. {
    277. Data[data_sp++] = font;
    278. font = 0;
    279. }
    280. arg_01 = Data[--data_sp]; //
    281. arg_02 = Data[--data_sp]; //
    282. Data[data_sp++] = arg_02 - arg_01;
    283. count = 0;
    284. continue;
    285. }
    286. if(suffix[i] == '*')
    287. {
    288. if(count == 1) // 说明取过数
    289. {
    290. Data[data_sp++] = font;
    291. font = 0;
    292. }
    293. arg_01 = Data[--data_sp]; //
    294. arg_02 = Data[--data_sp]; //
    295. Data[data_sp++] = arg_01 * arg_02;
    296. count = 0;
    297. continue;
    298. }
    299. if(suffix[i] == '/')
    300. {
    301. if(count == 1) // 说明取过数
    302. {
    303. Data[data_sp++] = font;
    304. font = 0;
    305. }
    306. arg_01 = Data[--data_sp]; //
    307. arg_02 = Data[--data_sp]; //
    308. Data[data_sp++] = arg_02 / arg_01;
    309. count = 0;
    310. continue;
    311. }
    312. }
    313. qDebug() << Data[0];
    314. ui->lineEdit->clear();
    315. this->Chars[char_sp++] = '=';
    316. Chars[char_sp] ='\0';
    317. ui->lineEdit->setText(QString::fromUtf8(this->Chars)+QString::number(Data[0]));
    318. Operation_end = 1; // 运算结束标志位
    319. }
    320. // 237 238 236
    321. void Widget::on_but_close_clicked()
    322. {
    323. this->close(); // close window
    324. }

    4 完整代码

    https://download.csdn.net/download/qq_51458770/89492760

  • 相关阅读:
    齐博x1 你们的 上一页 下一页 还好么?
    人生第一个项目(学生管理系统)(进阶版)
    为什么void CreateListR(LinkNode*&S,String a[],int n)这里有问题
    零食食品经营小程序商城的作用是什么
    linux 终端美化教程
    QMI8658芯片I2C驱动开发指南
    python元组与列表的区别
    VScode多文件编译/调试配置
    【activiti】activiti与springboot整合
    6 面向对象
  • 原文地址:https://blog.csdn.net/qq_51458770/article/details/140042795