A simple calculator written with QT

Source: Internet
Author: User

The software design course has been held for three or four times. I feel that I have not done anything. I feel that this course is purely nonsense. Tomorrow is Monday. I should have handed in something anyway, so I wrote a calculator for myself over the weekend.

Widget. h:

 

/* <Br/> * copyright (c) 2011 @ hit footoo lab <br/> * Author: shiyanhui <br/> * Create Date: 2011-3-20 <br/> */<br/> # ifndef widget_h <br/> # define widget_h <br/> # include <qwidget> <br/> class qstring; <br/> class qlineedit; <br/> class qpushbutton; <br/> class qhboxlayout; <br/> class qvboxlayout; <br/> class Widget: public qwidget <br/>{< br/> q_object <br/> Public: <br/> widget (qwidget * parent = 0); <br/> private slots: <br/> void pushoperatorbutton (); <br/> void pushnumberbutton (); <br/> void pushbracketbutton (); <br/> void pushclearbutton (); <br/> PRIVATE: <br/> void declarevariables (); <br/> void createlayout (); <br/> void createconnect (); <br/> qlineedit * expressionlineedit; <br/> qpushbutton * numberpushbuttons [10]; <br/> qpushbutton * bracketpushbuttons [2]; <br/> qpushbutton * operatorpushbuttons [5]; <br/> qpushbutton * clearpushbutton; <br/> // For left down row pushbuttons <br/> qhboxlayout * leftdownrowlayout [4]; <br/> // For right down row pushbuttons <br/> qhboxlayout * rightdownrowlayout [3]; <br/> // For left layout <br/> qvboxlayout * leftdownlayout; <br/> // For right layout <br/> qvboxlayout * rightdownlayout; <br/> // For down layout <br/> qhboxlayout * downlayout; <br/> // For up layout <br/> qhboxlayout * uplayout; <br/> // for global layout <br/> qvboxlayout * globallayout; <br/> }; <br/> # endif // widget_h <br/>

Widget. cpp:

/* <Br/> * copyright (c) 2011 @ hit footoo lab <br/> * Author: shiyanhui <br/> * Create Date: 2011-3-20 <br/> */<br/> # include "calculate. H "<br/> # include" widget. H "<br/> # include <qtgui> <br/> Widget: widget (qwidget * parent): <br/> qwidget (parent) <br/>{< br/> declarevariables (); <br/> createlayout (); <br/> createconnect (); <br/>}< br/> void Widget: pushoperatorbutton () <br/> {<br/> qpushbutton * apushbutton = dynamic_cast <qpushbutton *> (sender ()); <br/> for (INT I = 0; I <4; ++ I) <br/>{< br/> If (apushbutton = operatorpushbuttons [I]) <br/>{< br/> expressionlineedit-> insert (apushbutton-> text (); <br/> return; <br/>}< br/> calculation acalculation (expressionlineedit-> text (). tostdstring (); <br/> acalculation. midfix_to_suffix (); <br/> acalculation. calculate (); <br/> expressionlineedit-> settext (qstring ("% 1 "). arg (acalculation. get_answer (); <br/>}< br/> void Widget: pushnumberbutton () <br/>{< br/> qpushbutton * apushbutton = dynamic_cast <qpushbutton *> (sender (); <br/> for (INT I = 0; I <10; ++ I) <br/>{< br/> If (apushbutton = numberpushbuttons [I]) <br/> expressionlineedit-> insert (qstring ("% 1 "). arg (I); <br/>}< br/> void Widget: pushbracketbutton () <br/>{< br/> qpushbutton * apushbutton = dynamic_cast <qpushbutton *> (sender (); <br/> for (INT I = 0; I <2; ++ I) <br/>{< br/> If (apushbutton = bracketpushbuttons [I]) <br/> expressionlineedit-> insert (apushbutton-> text ()); <br/>}< br/> void Widget: pushclearbutton () <br/>{< br/> expressionlineedit-> clear (); <br/>}< br/> void Widget: declarevariables () <br/>{< br/> // declare lineedit of expression <br/> expressionlineedit = new qlineedit (); <br/> // declare number pushbuttons <br/> for (INT I = 0; I <10; ++ I) <br/> {<br/> numberpushbuttons [I] = new qpushbutton (qstring ("% 1 "). arg (I); <br/> numberpushbuttons [I]-> setdefault (true ); <br/>}< br/> // declare brackets pushbuttons <br/> const qstring brackets [2] = {"(",")"}; <br/> for (INT I = 0; I <2; ++ I) <br/> {<br/> bracketpushbuttons [I] = new qpushbutton (brackets [I]); <br/>}< br/> // declare operator pushbuttons <br/> const qstring operators [5] = {"+ ","-","*", "/", "=" };< br/> for (INT I = 0; I <5; I ++) <br/> {<br/> operatorpushbuttons [I] = new qpushbutton (operators [I]); <br/>}< br/> // declare clear Pushbutton <br/> clearpushbutton = new qpushbutton ("clear "); <br/> // declare layout <br/> uplayout = new qhboxlayout; <br/> downlayout = new qhboxlayout; <br/> // declare leftdown row layout <br/> for (INT I = 0; I <4; I ++) <br/>{< br/> leftdownrowlayout [I] = new qhboxlayout; <br/>}< br/> leftdownlayout = new qvboxlayout; <br/> // declare rightdown row layout <br/> for (INT I = 0; I <3; I ++) <br/>{< br/> rightdownrowlayout [I] = new qhboxlayout; <br/>}< br/> rightdownlayout = new qvboxlayout; <br/> globallayout = new qvboxlayout; <br/>}< br/> // built the connect <br/> void Widget: createconnect () <br/>{< br/> for (INT I = 0; I <10; ++ I) <br/>{< br/> qobject :: connect (numberpushbuttons [I], signal (clicked (), this, slot (pushnumberbutton (); <br/>}< br/> for (INT I = 0; I <5; ++ I) <br/> {<br/> qobject: connect (operatorpushbuttons [I], signal (clicked (), this, slot (pushoperatorbutton (); <br/>}< br/> for (INT I = 0; I <2; ++ I) <br/>{< br/> qobject: connect (bracketpushbuttons [I], signal (clicked (), this, slot (pushbracketbutton ())); <br/>}< br/> qobject: connect (clearpushbutton, signal (clicked (), this, slot (pushclearbutton ())); <br/>}< br/> void Widget: createlayout () <br/>{< br/> uplayout-> addwidget (expressionlineedit ); <br/> // left down <br/> const int num1 [3] = {7, 4, 1}; <br/> for (INT I = 0; I <3; ++ I) <br/> {<br/> for (Int J = 0; j <3; ++ J) <br/> {<br/> leftdownrowlayout [I]-> addwidget (numberpushbuttons [J + num1 [I]); <br/>}< br/> leftdownrowlayout [3]-> addwidget (numberpushbuttons [0]); <br/> leftdownrowlayout [3]-> addwidget (bracketpushbuttons [0]); <br/> leftdownrowlayout [3]-> addwidget (bracketpushbuttons [1]); <br/> for (INT I = 0; I <4; ++ I) <br/>{< br/> leftdownlayout-> addlayout (leftdownrowlayout [I]); <br/>}< br/> // right down <br/> const int num2 [2] = {0, 2 }; <br/> for (INT I = 0; I <2; ++ I) <br/>{< br/> for (Int J = 0; j <2; + + J) <br/>{< br/> rightdownrowlayout [I]-> addwidget (operatorpushbuttons [J + num2 [I]); <br/>}< br/> rightdownrowlayout [2]-> addwidget (operatorpushbuttons [4]); <br/> rightdownrowlayout [2]-> addwidget (clearpushbutton); <br/> for (INT I = 0; I <3; ++ I) <br/>{< br/> rightdownlayout-> addlayout (rightdownrowlayout [I]); <br/>}< br/> downlayout-> addlayout (leftdownlayout ); <br/> downlayout-> addstretch (); <br/> downlayout-> addlayout (rightdownlayout); <br/> globallayout-> addlayout (uplayout ); <br/> globallayout-> addlayout (downlayout); <br/> This-> setlayout (globallayout); <br/>}< br/>

Calculate. h:

/* <Br/> * copyright (c) 2011 @ hit footoo lab <br/> * Author: shiyanhui <br/> * Create Date: 2011-3-20 <br/> */<br/> # ifndef calculate_h <br/> # define calculate_h <br/> # include <string> <br/> # include <sstream> <br/> # include <map> <br/> # define stack_size 100 <br/> using namespace STD; <br/> struct charstack <br/>{< br/> int size; <br/> char element [stack_size]; <br/> }; <br/> struct intstack <br/>{< br/> int size; <br/> int element [stack_size]; <br/> }; <br/> class calculation <br/> {<br/> Public: <br/> // constructor <br/> calculation (string ); <br/> // The infix expression is converted to a suffix expression <br/> void midfix_to_suffix (); <br/> // calculate <br/> void calculate (); <br/> // obtain the result <br/> int get_answer (); <br/> PRIVATE: <br/> string expression; // expression <br/> int answer; // result <br/> charstack operator_stack; // storage operator <br/> intstack operand_stack; // Storage Operations <br/> stringstream SS; <br/> Map <char, int> AMAP; // operator-to-priority ing <br/>}; <br/> # endif <br/>

Calculate. cpp:

/* <Br/> * copyright (c) 2011 @ hit footoo lab <br/> * Author: shiyanhui <br/> * Create Date: 2011-3-20 <br/> */<br/> # include "calculate. H "<br/> calculation: calculation (string expression) <br/>{< br/> This-> Expression = expression; <br/> answer = 0; <br/> operator_stack.size = operand_stack.size = 0; <br/> AMAP ['C'] = 0; <br/> AMAP ['+'] = AMAP ['-'] = 1; <br/> AMAP ['*'] = AMAP ['/'] = 2; <br/>}< br/> void calculation: midfix_to_suffix () <br/>{< br/> string TMP = ""; <br/> // scan the expression again <br/> for (INT I = 0; I <expression. size ();) <br/>{< br/> // if the character is a number, continue the loop until the number is fully searched, add '. ', separated from other numbers <br/> If (isdigit (expression [I]) <br/> {<br/> while (I <expression. size () <br/>{< br/> If (isdigit (expression [I]) <br/>{< br/> TMP + = expression [I ++]; <br/>}< Br/> else <br/> break; <br/>}< br/> TMP + = '. '; <br/>}< br/> // if it is a space, ignore <br/> else if (expression [I] = '') <br/>{< br/> I ++; <br/> continue; <br/>}< br/> // other operators can only be <br/> else <br/> {<br/> If (expression [I] = '( ') <br/>{ <br/> operator_stack.element [operator_stack.size + +] = '('; <br/> I ++; <br/>}< br/> else if (expression [I] = ') <br/>{< br/> while (operator_st Ack. Element [operator_stack.size-1]! = '(') <Br/>{< br/> TMP + = operator_stack.element [-- operator_stack.size]; <br/>}< br/> operator_stack.size --; <br/> I ++; <br/>}< br/> else <br/>{< br/> while (operator_stack.size> 0) <br/> {<br/> If (AMAP [expression [I] <= AMAP [operator_stack.element [operator_stack.size-1]) <br/>{< br/> TMP + = operator_stack.element [-- operator_stack.size]; <br/>}< br/> else <br/> Break; <Br/>}< br/> operator_stack.element [operator_stack.size ++] = expression [I ++]; <br/>}< br/> while (operator_stack.size> 0) <br/>{< br/> TMP + = operator_stack.element [-- operator_stack.size]; <br/>}< br/> Expression = TMP; <br/>}< br/> void calculation: Calculate () <br/>{< br/> int A = 0, B = 0, num = 0; <br/> for (INT I = 0; I <expression. size ();) <br/>{< br /> If (isdigit (expression [I]) <br/>{< br/> string TMP = ""; <br/> while (expression [I]! = '. ') <Br/>{< br/> TMP + = expression [I ++]; <br/>}< br/> I ++; <br/> SS <TMP; <br/> SS> num; <br/> SS. clear (); <br/> operand_stack.element [operand_stack.size ++] = num; <br/>}< br/> else <br/> {<br/> B = operand_stack.element [-- operand_stack.size]; <br/> A = operand_stack.element [-- operand_stack.size]; <br/> switch (expression [I]) <br/> {<br/> case '+': <br/> operand_stack.element [operand_stack.size ++] = A + B; <br/> break; <br/> case '-': <br/> operand_stack.element [operand_stack.size ++] = A-B; <br/> break; <br/> case '*': <br/> operand_stack.element [operand_stack.size ++] = a * B; <br/> break; <br/> case '/': <br/> operand_stack.element [operand_stack.size ++] = A * 1.0/B; <br/>}< br/> I ++; <br/>}< br/> answer = operand_stack.element [0]; <br/>}< br/> int calculation: get_answer () <br/>{< br/> return answer; <br/>}< br/>

Main. cpp:

/* <Br/> * copyright (c) 2011 @ hit footoo lab <br/> * Author: shiyanhui <br/> * Create Date: 2011-3-20 <br/> */<br/> # include <qtgui/qapplication> <br/> # include "widget. H "<br/> int main (INT argc, char * argv []) <br/>{< br/> qapplication A (argc, argv ); <br/> widget W; <br/> W. show (); <br/> return a.exe C (); <br/>}< br/>

Laizhang:

 

Note: Please indicate the source of your record. Otherwise, you are legally liable for your record!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.