Use iOS as a simple calculator (with complete functions)

Source: Internet
Author: User

Use iOS as a simple calculator (with complete functions)

Source code (. m file)

# Import "ZKJAppDelegate. h"



@ Interface ZKJAppDelegate ()

@ Property (retain, nonatomic) UIView * containView;

@ Property (retain, nonatomic) UIButton * button;

@ Property (retain, nonatomic) UILabel * label;

@ Property (retain, nonatomic) NSMutableString * string; // Save the character

@ Property (assign, nonatomic) double num1, num2, num3, num4;

// Num1 Save the input value, 2, save the value before the operation, 3 is the calculation result, and 4 is the calculation method.

@ End






@ Implementation ZKJAppDelegate


-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions

{

Self. window = [[uiappswalloc] initWithFrame: [UIScreenmainScreen] bounds];

// Override point for customization after application launch.

Self. containView = [[UIViewalloc] initWithFrame: CGRectMake (0,0, 320,568)];

Self. containView. backgroundColor = [UIColorclearColor];

[Self. windowaddSubview: self. containView];

[Self. containViewrelease];

Self. label = [[UILabelalloc] initWithFrame: CGRectMake (190, 50)];

Self. label. backgroundColor = [UIColorlightGrayColor];

Self. label. textAlignment = UITextAlignmentLeft;

Self. label. font = [UIFont systemFontOfSize: 32];

[Self. containViewaddSubview: self. label];

[Self. labelrelease];

UIButton * backButton = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

BackButton. backgroundColor = [UIColorlightGrayColor];

[BackButton setFrame: CGRectMake (225,40, 60, 50)];

[BackButton setTitle: @ "back" forState: UIControlStateNormal];

[BackButton addTarget: selfaction: @ selector (backClick :) forControlEvents: UIControlEventTouchUpInside];

[Self. containViewaddSubview: backButton];

// Add the 1-9 button

NSArray * array = [NSArrayarrayWithObjects: @ "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 ++ ){

Self. button = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

Self. button. frame = CGRectMake (30 + 65 * j, 150 + 65 * I, 60, 60 );

Self. button. backgroundColor = [UIColorlightGrayColor];

[Self. buttonsetTitle: array [n ++] forState: UIControlStateNormal];

[Self. containViewaddSubview: self. button];

[Self. buttonaddTarget: selfaction: @ selector (one :) forControlEvents: UIControlEventTouchUpInside];

}

}

// Add zero

UIButton * button0 = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

[Button0 setFrame: CGRectMake (30,345, 60, 60)];

Button0.backgroundColor = [UIColorlightGrayColor];

[Button0 setTitle: @ "0" forState: UIControlStateNormal];

[Button0 addTarget: selfaction: @ selector (one :) forControlEvents: UIControlEventTouchUpInside];

[Self. containViewaddSubview: button0];

// Add a vertex

UIButton * point = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

[Point setFrame: CGRectMake (95,345, 60, 60)];

Point. backgroundColor = [UIColorlightGrayColor];

[Point setTitle: @ "." forState: UIControlStateNormal];

[Point addTarget: selfaction: @ selector (one :) forControlEvents: UIControlEventTouchUpInside];

[Self. containViewaddSubview: point];

// [Point release];

// Add or delete

UIButton * deleteButton = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

[DeleteButton setFrame: CGRectMake (160,345, 60, 60)];

DeleteButton. backgroundColor = [UIColorlightGrayColor];

[DeleteButton setTitle: @ "delete" forState: UIControlStateNormal];

[DeleteButton addTarget: selfaction: @ selector (deleteClick :) forControlEvents: UIControlEventTouchUpInside];

[Self. containViewaddSubview: deleteButton];

// [BackButton release];

// Add Operators

NSArray * array1 = [NSArrayarrayWithObjects: @ "+", @ "-", @ "*", @ "/", nil];

For (int I = 0; I <4; I ++ ){

UIButton * button = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

[Button setFrame: CGRectMake (225,150 + 65 * I, 60, 60)];

Button. backgroundColor = [UIColorlightGrayColor];

[Button setTitle: array1 [I] forState: UIControlStateNormal];

[Self. containViewaddSubview: button];

[Button addTarget: selfaction: @ selector (two :) forControlEvents: UIControlEventTouchUpInside];

// [Button release];

}

// Add an equal sign

UIButton * istoButton = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

[IstoButton setFrame: CGRectMake (30,100,255, 40)];

IstoButton. backgroundColor = [UIColorlightGrayColor];

[IstoButton setTitle: @ "=" forState: UIControlStateNormal];

[IstoButton addTarget: selfaction: @ selector (isto:) forControlEvents: UIControlEventTouchUpInside];

[Self. containViewaddSubview: istoButton];

Self. string = [[NSMutableStringalloc] init];

Self. window. backgroundColor = [UIColorwhiteColor];

[Self. windowmakeKeyAndVisible];

Return YES;

}


// Enter a number on the keyboard

-(Void) one :( id) sender {

If ([self. stringhasPrefix: @ "+"] | [self. stringhasPrefix: @ "-"] | [self. stringhasPrefix: @ "*"] | [self. stringhasPrefix: @ "/"]) {

[Self. stringsetString: @ ""];

}

//*********

[Self. stringappendString: [sender currentTitle]; // continuous number input ;;;

Self. label. text = [NSStringstringWithString: self. string];

Self. num1 = [self. label. textdoubleValue]; // converts a string to a basic data type and assigns it to num1;

NSLog (@ "% f", self. num1 );

}

// Operator event

-(Void) two :( id) sender {

[Self. stringsetString: @ ""];

//*********

[Self. stringappendString: [sender currentTitle];

Self. label. text = self. string;

If ([self. stringhasPrefix: @ "+"]) {

Self. num2 = self. num1;

Self. num4 = 1;

} Else if ([self. stringhasPrefix: @ "-"]) {

Self. num2 = self. num1;

Self. num4 = 2;

} Else if ([self. stringhasPrefix: @ "*"]) {

Self. num2 = self. num1;

Self. num4 = 3;

} Else if ([self. stringhasPrefix: @ "/"]) {

Self. num2 = self. num1;

Self. num4 = 4;

}

}

// Equal sign event

-(Void) isto :( id) sender {

If (self. num4 = 1 ){

Self. num3 = self. num2 + [self. label. textdoubleValue];

Self. label. text = [NSStringstringWithFormat: @ "% f", self. num3];

// Self. num3 = 0;

[Self. stringsetString: @ ""];

} Else if (self. num4 = 2 ){

Self. num3 = self. num2-[self. label. textdoubleValue];

Self. label. text = [NSStringstringWithFormat: @ "% f", self. num3];

// Self. num3 = 0;

} Else if (self. num4 = 3 ){

Self. num3 = self. num2 * [self. label. textdoubleValue];

Self. label. text = [NSStringstringWithFormat: @ "% f", self. num3];

// Self. num3 = 0;

} Else if (self. num4 = 4 ){

Self. num3 = self. num2/[self. label. textdoubleValue];

Self. label. text = [NSStringstringWithFormat: @ "% f", self. num3];

// Self. num3 = 0;

}

}


// All deleted response events

-(Void) deleteClick :( id) sender {

[Self. stringsetString: @ ""];

// [Self. string appendString: [sender currentTitle];

Self. label. text = self. string;

}



// Delete a number of response events

-(Void) backClick :( id) sender {

// NSMutableString

If ([self. stringlength]> 0 ){

NSInteger n = [self. stringlength];

[Self. stringdeleteCharactersInRange: NSMakeRange (n-1, 1)];

Self. label. text = self. string;

}


}







-(Void) applicationWillResignActive :( UIApplication *) application

{

// Sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games shocould use this method to pause the game.

}


-(Void) applicationDidEnterBackground :( UIApplication *) application

{

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


-(Void) applicationWillEnterForeground :( UIApplication *) application

{

// Called as part of the transition from the background to the inactive state; here you can undo changes of the changes made on entering the background.

}


-(Void) applicationDidBecomeActive :( UIApplication *) application

{

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previusly in the background, optionally refresh the user interface.

}


-(Void) applicationWillTerminate :( UIApplication *) application

{

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground :.

}

-(Void) dealloc {

[Self. windowrelease];

[Super dealloc];

}

@ End



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.