Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*------------------------------------------*/
- /* this is the my main.cpp file */
- #include <QApplication>
- #include "qtCalc.h"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- Calc *dialog = new Calc();
- dialog>show();
- return app.exec();
- }
- /*------------------------------------------*/
- /* this is th my myCalc.cpp file */
- #include <QtGui>
- #include "myCalc.h"
- Calc::Calc( QWidget *parent )
- : str( "" ), opCod( '0' )
- {
- setupUi(this); // this sets up GUI
- // signals/slots mechanism in action
- connect(pushButton_CE,SIGNAL(clicked()),this,SLOT(clear()));
- connect(pushButton_Igual,SIGNAL(clicked()),this,SLOT(igualClick()));
- connect(pushButton_0,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_1,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_2,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_3,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_4,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_5,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_6,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_7,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_8,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_9,SIGNAL(clicked()),this,SLOT(digitClick()));
- connect(pushButton_Soma,SIGNAL(clicked()),this,SLOT(operationClick()));
- connect(pushButton_Sub,SIGNAL(clicked()),this,SLOT(operationClick()));
- connect(pushButton_Div,SIGNAL(clicked()),this,SLOT(operationClick()));
- connect(pushButton_Mult,SIGNAL(clicked()),this,SLOT(operationClick()));
- connect(pushButton_Exp,SIGNAL(clicked()),this,SLOT(operationClick()));
- connect(pushButton_Mod,SIGNAL(clicked()),this,SLOT(operationClick()));
- void Calc::clear()
- {
- resultArea>clear();
- resultArea>setText( "0" );
- str = "";
- opCod = '0';
- }
- void Calc::digitClick()
- {
- QPushButton *clickedButton = qobject_cast<QPushButton*>(sender());
- if ( clickedButton == pushButton_0 && str == "" )
- ;
- else
- str += clickedButton>text().toStdString();
- resultArea>setText( QString::fromStdString(str));
- }
- void Calc::operationClick()
- {
- QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
- if(opCod == '0')
- {
- string res = clickedButton>text().toStdString();
- opCod = res[0];
- istringstream temp( str );
- temp >> op1;
- str = "";
- }
- }
- void Calc::igualClick()
- {
- if (str.length() > 256)
- resultArea>setText("Caracter limit exceded");
- else
- {
- istringstream temp ( str );
- temp >> op2;
- QString res;
- bool valid = true;
- switch(opCod)
- {
- case '+':
- res.setNum( op1 + op2 );
- break;
- case '':
- res.setNum( op1 + op2 );
- break;
- case '*':
- res.setNum( op1 * op2 );
- break;
- case '/':
- res.setNum( op1 / op2 );
- break;
- case '^':
- res.setNum( pow(op1, op2) );
- break;
- case '%':
- res.setNum(op1 & op2);
- break;
- default:
- valid = false;
- }
- if(!valid)
- resultArea>setText("Erro: Invalid Operator.");
- else
- resultArea>setText( res );
- str = "";
- opCod = '0';
- }
- }
- /*------------------------------------------*/
- /* this is the my myCalc.h file */
- #ifndef QTCALC_H
- #define QTCALC_H
- #include "ui_myCalc.h"
- #include <string>
- using std::string;
- class Calc : public QWidget, private Ui::qtCalc
- {
- Q_OBJECT
- public: Calc( QWidget *parent = 0);
- public slots:
- void clear();
- void igualClick();
- void digitClick();
- void operationClick();
- private:
- string str;
- char opCod;
- int op1,op2;
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement