Simple calculator for OC implementation

Source: Internet
Author: User
<P> // Note: only simple addition, subtraction, multiplication, division, and continuous operation are allowed. </P> <p> # define kbutton_color [uicolor cyancolor] // defines the background color macro of the key, easy to change color </P> @ interface yjappdelegate () {uiview * _ containter; uibutton * _ numberbutton; uilabel * _ label; cgfloat num1, operators, result, digital; nsmutablestring * _ string; nsstring * _ symboltext;} @ end <PRE name = "code" class = "objc"> _ containter = [[uiview alloc] initwithframe: cgrectmake (0, 0,320,568)]; _ containter. backgroundcolor = [uicolor graycolor]; [self. window addsubview: _ containter]; [_ containter release]; // set the number nsarray * array = [nsarray arraywithobjects: @ "1", @ "2", @ "3 ", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", nil]; int n = 0; for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {_ numberbutton = [uibutton buttonwithtype: uibuttontypesystem]; _ numberbutton. frame = cgrectmake (J * 80), 180 + (I * 80), 78, 78); [_ numberbutton settitle: [array objectatindex: N ++] forstate: uicontrolstatenormal]; _ numberbutton. backgroundcolor = kbutton_color; _ numberbutton. titlelabel. font = [uifont systemfontofsize: 22]; _ numberbutton. layer. cornerradius = 30; [_ numberbutton addtarget: Self action: @ selector (number :) forcontrolevents: uicontroleventtouchupinside]; [_ containter addsubview: _ numberbutton];} // set the return key uibutton * backbutton = [uibutton buttonwithtype: uibuttontypesystem]; backbutton. frame = cgrectmake (240,180, 78, 78); [backbutton settitle: @ "back" forstate: uicontrolstatenormal]; backbutton. backgroundcolor = kbutton_color; backbutton. titlelabel. font = [uifont systemfontofsize: 22]; backbutton. layer. cornerradius = 30; [backbutton addtarget: Self action: @ selector (Back :) forcontrolevents: uicontroleventtouchupinside]; [_ containter addsubview: backbutton]; // set the symbol key nsarray * array2 = [nsarray arraywithobjects: @ "+", @ "-", @ "*", @ "/", nil]; for (INT I = 0; I <4; I ++) {uibutton * symbolbutton = [uibutton buttonwithtype: uibuttontypesystem]; symbolbutton. frame = cgrectmake (240,260 + (I * 80), 78, 78); [symbolbutton settitle: [array2 objectatindex: I] forstate: uicontrolstatenormal]; symbolbutton. backgroundcolor = kbutton_color; symbolbutton. titlelabel. font = [uifont systemfontofsize: 22]; symbolbutton. layer. cornerradius = 30; [symbolbutton addtarget: Self action: @ selector (symbol :) forcontrolevents: uicontroleventtouchupinside]; [_ containter addsubview: symbolbutton];} // set 0 and. nsarray * array3 = [nsarray arraywithobjects: @ "0 ",@". ", nil]; for (INT I = 0; I <2; I ++) {uibutton * button = [uibutton buttonwithtype: uibuttontypesystem]; button. frame = cgrectmake (80 + (I * 80), 420, 78, 78); button. backgroundcolor = kbutton_color; button. titlelabel. font = [uifont systemfontofsize: 22]; button. layer. cornerradius = 30; [Button settitle: [array3 objectatindex: I] forstate: uicontrolstatenormal]; [Button addtarget: Self action: @ selector (Button :) forcontrolevents: uicontroleventtouchupinside]; [_ containter addsubview: button];} // set uibutton * acbutton = [uibutton buttonwithtype: uibuttontypesystem]; acbutton. frame = cgrectmake (0,420, 78, 78); acbutton. backgroundcolor = kbutton_color; acbutton. layer. cornerradius = 30; acbutton. titlelabel. font = [uifont systemfontofsize: 22]; [acbutton settitle: @ "AC" forstate: uicontrolstatenormal]; [acbutton addtarget: Self action: @ selector (AC :) forcontrolevents: uicontroleventtouchupinside]; [_ containter addsubview: acbutton]; // set the uibutton * symbol button = [uibutton buttonwithtype: uibuttontypesystem]; symbol button. frame = cgrectmake (0,500,240, 68); reset button. backgroundcolor = kbutton_color; Specify button. titlelabel. font = [uifont systemfontofsize: 22]; then button. layer. cornerradius = 30; [using button settitle: @ "=" forstate: uicontrolstatenormal]; [using button addtarget: Self action: @ selector (equal :) forcontrolevents: uicontroleventtouchupinside]; [_ containter addsubview: export button]; // set the display window _ label = [[uilabel alloc] initwithframe: cgrectmake (10, 80,300, 60)]; _ label. backgroundcolor = [uicolor whitecolor]; _ label. textcolor = [uicolor bluecolor]; _ label. layer. cornerradius = 3; _ label. textalignment = nstextalignmentleft; _ label. font = [uifont systemfontofsize: 20]; [_ containter addsubview: _ label]; [_ label release]; // set the displayed text to string _ string = [[nsmutablestring alloc] init];

// Configure the number key trigger-(void) number :( uibutton *) number {[_ string appendstring: [number currenttitle]; _ label. TEXT = [nsstring stringwithstring: _ string]; num1 = [_ label. text doublevalue];} // configure 0 and. -(void) button :( uibutton *) button {[_ string appendstring: [Button currenttitle]; _ label. TEXT = [nsstring stringwithstring: _ string]; num1 = [_ label. text doublevalue];} // configure the trigger of the operator key-(void) symbol :( uibutton *) symbol {[_ St Ring setstring: @ ""]; [_ string appendstring: [Symbol currenttitle]; _ label. TEXT = [nsstring stringwithstring: _ string]; If ([_ string hasprefix: @ "+"]) {digital = num1; // store the operators = 1 number entered for the first time; // store the input operator [_ string setstring: @ ""]; // clear the interface after the input operator} else if ([_ string hasprefix: @ "-"]) {digital = num1; operators = 2; // [_ string setstring: @ ""];} else if ([_ string hasprefix: @ "*"]) {digital = num1; operato Rs = 3; [_ string setstring: @ ""];} else if ([_ string hassuffix: @ "/"]) {digital = num1; operators = 4; [_ string setstring: @ ""] ;}// configure the equal key trigger-(void) equal :( uibutton *) equal {If (operators = 1) {result = digital + [_ label. text doublevalue]; // [_ label. text doublevalue] is the second input number _ label. TEXT = [nsstring stringwithformat: @ "% G", result]; // display the result [_ string setstring: @ ""]; // clear the result} else if (operators = 2 ){ Result = digital-(-[_ label. text doublevalue]); _ label. TEXT = [nsstring stringwithformat: @ "% G", result]; [_ string setstring: @ ""];} else if (operators = 3) {result = digital * [_ label. text doublevalue]; _ label. TEXT = [nsstring stringwithformat: @ "% G", result]; [_ string setstring: @ ""];} else if (operators = 4) {result = digital/[_ label. text doublevalue]; _ label. TEXT = [nsstring stringwithfo Rmat: @ "% G", result]; [_ string setstring: @ ""] ;}// configure the trigger of the zeroth key-(void) AC :( uibutton *) AC {[_ string setstring: @ ""]; Result = 0; operators = 0; _ label. TEXT = @ "0";} // configure the trigger of the Return key-(void) Back :( uibutton *) back {If (! [_ Label. text isequal: @ ""]) {[_ string deletecharactersinrange: nsmakerange ([_ String Length]-1, 1)]; // Delete the last character _ label. TEXT = [nsstring stringwithstring: _ string]; // display the deleted string num1 = [_ label. text floatvalue]; // receives the deleted number }}

 

Related Article

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.