[C++] 基于Qt的计算器,能实现基本的加减乘除乘方开放等功能。 →→→→→进入此内容的聊天室

来自 , 2019-10-17, 写在 C++, 查看 133 次.
URL http://www.code666.cn/view/e74c0d42
  1. #include"caldlg.h"
  2. #include<math.h>
  3.  
  4. CALDLG::CALDLG(QWidget *parent)
  5. {
  6.   ui=new Ui_calculate;
  7.   ui->setupUi(this);
  8.   first = 0;
  9.   second = 0;
  10.   result = 0;
  11.   flag1 = 0;
  12.   b=1;
  13. }
  14. CALDLG::~CALDLG()
  15. {
  16.   delete ui;
  17. }
  18. /*-------------------处理数字键-------------------*/
  19. void CALDLG::on_pushButton_1_clicked()
  20. {
  21.   str+=ui->pushButton_1->text();
  22.   //转换为double
  23.   temp=str.toDouble();
  24.   //把和转换为文本显示到标签框
  25.   ui->lineEdit->setText(QString::number(temp));
  26. }
  27. void CALDLG::on_pushButton_2_clicked()
  28. {
  29.   str+=ui->pushButton_2->text();
  30.   temp=str.toDouble();
  31.   ui->lineEdit->setText(QString::number(temp));
  32. }
  33. void CALDLG::on_pushButton_3_clicked()
  34. {
  35.   str+=ui->pushButton_3->text();
  36.   temp=str.toDouble();
  37.   ui->lineEdit->setText(QString::number(temp));
  38. }
  39. void CALDLG::on_pushButton_4_clicked()
  40. {
  41.   str+=ui->pushButton_4->text();
  42.   temp=str.toDouble();
  43.   ui->lineEdit->setText(QString::number(temp));
  44. }
  45. void CALDLG::on_pushButton_5_clicked()
  46. {
  47.   str+=ui->pushButton_5->text();
  48.   temp=str.toDouble();
  49.   ui->lineEdit->setText(QString::number(temp));
  50. }
  51. void CALDLG::on_pushButton_6_clicked()
  52. {
  53.   str+=ui->pushButton_6->text();
  54.   temp=str.toDouble();
  55.   ui->lineEdit->setText(QString::number(temp));
  56. }
  57. void CALDLG::on_pushButton_7_clicked()
  58. {
  59.   str+=ui->pushButton_7->text();
  60.   temp=str.toDouble();
  61.   ui->lineEdit->setText(QString::number(temp));
  62. }
  63. void CALDLG::on_pushButton_8_clicked()
  64. {
  65.   str+=ui->pushButton_8->text();
  66.   temp=str.toDouble();
  67.   ui->lineEdit->setText(QString::number(temp));
  68. }
  69. void CALDLG::on_pushButton_9_clicked()
  70. {
  71.   str+=ui->pushButton_9->text();
  72.   temp=str.toDouble();
  73.   ui->lineEdit->setText(QString::number(temp));
  74. }
  75. void CALDLG::on_pushButton_10_clicked()
  76. {
  77.   str+=ui->pushButton_10->text();
  78.   temp=str.toDouble();
  79.   ui->lineEdit->setText(QString::number(temp));
  80. }
  81. void CALDLG::on_pushButton_20_clicked()
  82. {
  83.   str+=ui->pushButton_20->text();
  84.   temp=str.toDouble();
  85.   ui->lineEdit->setText(QString::number(temp));
  86. }
  87.  
  88. /*------------------处理运算符键--------------------*/
  89. void CALDLG::on_pushButton_11_clicked()
  90. {
  91.   flag = '+';
  92.     if(flag1 == 0)
  93.     {
  94.        first = temp;
  95.        temp = 0;
  96.     }
  97.     str = "";
  98. }
  99. void CALDLG::on_pushButton_12_clicked()
  100. {
  101.     flag = '-';
  102.     if(flag1 == 0)
  103.     {
  104.        first = temp;
  105.        temp = 0;
  106.     }
  107.     str = "";
  108. }
  109. void CALDLG::on_pushButton_13_clicked()
  110. {
  111.     flag = '*';
  112.     if(flag1 == 0)
  113.     {
  114.        first = temp;
  115.        temp = 0;
  116.     }
  117.     str = "";
  118. }
  119. void CALDLG::on_pushButton_14_clicked()
  120. {
  121.     flag = '/';
  122.     if(flag1 == 0)
  123.     {
  124.        first = temp;
  125.        temp = 0;
  126.     }
  127.     str = "";
  128. }
  129. void CALDLG::on_pushButton_15_clicked()
  130. {
  131.     flag = 's';
  132.     if(flag1 == 0)
  133.     {
  134.        first = temp;
  135.        temp = 0;
  136.     }
  137.     str = "";
  138. }
  139. void CALDLG::on_pushButton_16_clicked()
  140. {
  141.     flag = 'c';
  142.     if(flag1 == 0)
  143.     {
  144.        first = temp;
  145.        temp = 0;
  146.     }
  147.     str = "";
  148. }
  149. void CALDLG::on_pushButton_17_clicked()
  150. {
  151.     flag = 't';
  152.     if(flag1 == 0)
  153.     {
  154.        first = temp;
  155.        temp = 0;
  156.     }
  157.     str = "";
  158. }
  159. void CALDLG::on_pushButton_18_clicked()
  160. {
  161.     flag = 'o';
  162.     if(flag1 == 0)
  163.     {
  164.        first = temp;
  165.        temp = 0;
  166.     }
  167.     str = "";
  168. }
  169.  
  170. void CALDLG::on_pushButton_21_clicked()
  171. {
  172.     flag = 'l';
  173.     if(flag1 == 0)
  174.     {
  175.        first = temp;
  176.        temp = 0;
  177.     }
  178.     str = "";
  179. }
  180. void CALDLG::on_pushButton_22_clicked()
  181. {
  182.     flag = '#';
  183.     if(flag1 == 0)
  184.     {
  185.        first = temp;
  186.        temp = 0;
  187.     }
  188.     str = "";
  189. }
  190. void CALDLG::on_pushButton_23_clicked()
  191. {
  192.     flag = '^';
  193.     if(flag1 == 0)
  194.     {
  195.        first = temp;
  196.        temp = 0;
  197.     }
  198.     str = "";
  199. }
  200.  
  201. /*-------------------清零操作--------------------*/
  202. void CALDLG::on_pushButton_24_clicked()
  203. {
  204.     first =0;
  205.     second = 0;
  206.     result = 0;
  207.     temp = 0;
  208.     flag1 = 0;
  209.     str = "";
  210.     ui->lineEdit->setText(QString::number(result));
  211. }
  212.  
  213. /*-------------------绛夊彿鎿嶄綔--------------------*/
  214. void CALDLG::on_pushButton_19_clicked()
  215. {
  216.    second=temp;
  217.    switch(flag)
  218.    {
  219.      case '+':result=first+second;break;
  220.      case '-':result=first-second;break;
  221.      case '*':result=first*second;break;
  222.      case '/':result=first/second;break;
  223.      case '#':result = pow(first,(1.0/second));break;
  224.      case '^':
  225.        for(i=0;i<second;i++)
  226.          b*=first;
  227.        result=b;break;
  228.      case 's':result=sin(first*(Pi/180));break;
  229.      case 'c':result=cos(first*(Pi/180));break;
  230.      case 't':result=tan(first*(Pi/180));break;
  231.      case 'o':result=1/(first*(Pi/180));break;
  232.      case 'l':result=log(first);break;
  233.   }
  234.   ui->lineEdit->setText(QString::number(result));
  235.   first=result;
  236.   flag1=1;
  237.   temp=0;
  238.   b=1;
  239. }
  240.  

回复 "基于Qt的计算器,能实现基本的加减乘除乘方开放等功能。"

这儿你可以回复上面这条便签

captcha