Advertisement
marquessbr

main.cpp; myCalc.cpp, myCalc.h

Nov 13th, 2012
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*------------------------------------------*/
  2. /* this is the my main.cpp file */
  3.  
  4. #include <QApplication>
  5. #include "qtCalc.h"
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     QApplication app(argc, argv);
  10.     Calc *dialog = new Calc();
  11.     dialog­>show();
  12.     return app.exec();
  13. }
  14.  
  15. /*------------------------------------------*/
  16. /* this is th my myCalc.cpp file */
  17.  
  18. #include <QtGui>
  19. #include "myCalc.h"
  20.  
  21. Calc::Calc( QWidget *parent )
  22.     : str( "" ), opCod( '0' )
  23. {
  24.     setupUi(this); // this sets up GUI
  25.  
  26. // signals/slots mechanism in action
  27.     connect(pushButton_CE,SIGNAL(clicked()),this,SLOT(clear()));
  28.     connect(pushButton_Igual,SIGNAL(clicked()),this,SLOT(igualClick()));
  29.     connect(pushButton_0,SIGNAL(clicked()),this,SLOT(digitClick()));
  30.     connect(pushButton_1,SIGNAL(clicked()),this,SLOT(digitClick()));
  31.     connect(pushButton_2,SIGNAL(clicked()),this,SLOT(digitClick()));
  32.     connect(pushButton_3,SIGNAL(clicked()),this,SLOT(digitClick()));
  33.     connect(pushButton_4,SIGNAL(clicked()),this,SLOT(digitClick()));
  34.     connect(pushButton_5,SIGNAL(clicked()),this,SLOT(digitClick()));
  35.     connect(pushButton_6,SIGNAL(clicked()),this,SLOT(digitClick()));
  36.     connect(pushButton_7,SIGNAL(clicked()),this,SLOT(digitClick()));
  37.     connect(pushButton_8,SIGNAL(clicked()),this,SLOT(digitClick()));
  38.     connect(pushButton_9,SIGNAL(clicked()),this,SLOT(digitClick()));
  39.     connect(pushButton_Soma,SIGNAL(clicked()),this,SLOT(operationClick()));
  40.     connect(pushButton_Sub,SIGNAL(clicked()),this,SLOT(operationClick()));
  41.     connect(pushButton_Div,SIGNAL(clicked()),this,SLOT(operationClick()));
  42.     connect(pushButton_Mult,SIGNAL(clicked()),this,SLOT(operationClick()));
  43.     connect(pushButton_Exp,SIGNAL(clicked()),this,SLOT(operationClick()));
  44.     connect(pushButton_Mod,SIGNAL(clicked()),this,SLOT(operationClick()));
  45.    
  46.     void Calc::clear()
  47.     {
  48.         resultArea­>clear();
  49.         resultArea­>setText( "0" );
  50.         str = "";
  51.         opCod = '0';
  52.     }
  53.  
  54.     void Calc::digitClick()
  55.     {
  56.         QPushButton *clickedButton = qobject_cast<QPushButton*>(sender());
  57.         if ( clickedButton == pushButton_0 && str == "" )
  58.             ;
  59.         else
  60.            str += clickedButton­>text().toStdString();
  61.  
  62.         resultArea­>setText( QString::fromStdString(str));
  63.     }
  64.  
  65.     void Calc::operationClick()
  66.     {
  67.         QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
  68.         if(opCod == '0')
  69.         {
  70.             string res = clickedButton­>text().toStdString();
  71.             opCod = res[0];
  72.             istringstream temp( str );
  73.             temp >> op1;
  74.             str = "";
  75.         }
  76.     }
  77.  
  78.     void Calc::igualClick()
  79.     {
  80.         if (str.length() > 256)
  81.             resultArea­>setText("Caracter limit exceded");
  82.         else
  83.         {
  84.             istringstream temp ( str );
  85.             temp >> op2;
  86.             QString res;
  87.             bool valid = true;
  88.             switch(opCod)
  89.         {
  90.             case '+':
  91.                 res.setNum( op1 + op2 );
  92.                 break;
  93.             case '­':
  94.                res.setNum( op1 + op2 );
  95.                break;
  96.             case '*':
  97.                res.setNum( op1 * op2 );
  98.                break;
  99.             case '/':
  100.                res.setNum( op1 / op2 );
  101.                break;
  102.             case '^':
  103.                res.setNum( pow(op1, op2) );
  104.                break;
  105.             case '%':
  106.                res.setNum(op1 & op2);
  107.                break;
  108.                default:
  109.                valid = false;
  110.           }
  111.           if(!valid)
  112.               resultArea­>setText("Erro: Invalid Operator.");
  113.           else
  114.               resultArea­>setText( res );
  115.               str = "";
  116.               opCod = '0';
  117.     }
  118. }
  119.  
  120. /*------------------------------------------*/
  121. /* this is the my myCalc.h file */
  122.  
  123. #ifndef QTCALC_H
  124. #define QTCALC_H
  125. #include "ui_myCalc.h"
  126. #include <string>
  127. using std::string;
  128. class Calc : public QWidget, private Ui::qtCalc
  129. {
  130.     Q_OBJECT
  131.     public: Calc( QWidget *parent = 0);
  132.     public slots:
  133.         void clear();
  134.         void igualClick();
  135.         void digitClick();
  136.         void operationClick();
  137.     private:
  138.         string str;
  139.         char opCod;
  140.         int op1,op2;
  141. };
  142. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement