用iOS 做一個簡易計算機 (功能完備)

來源:互聯網
上載者:User

用iOS 做一個簡易計算機 (功能完備)

原始碼(.m檔案)

#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;//儲存字元

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

//num1儲存輸入數值,2,儲存運算前數值,3是運算結果,4判斷進行何種運算

@end






@implementation ZKJAppDelegate


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

{

self.window = [[UIWindowalloc] 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(30,40, 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];

//添加1 - 9 的按鈕

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];

}

}

//添加零

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];

//添加點

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];

//添加刪除

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];

//添加運算子

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];

}

//添加等號

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;

}


//鍵盤輸入 數字事件

- (void)one:(id)sender {

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

[self.stringsetString:@""];

}

//*********

[self.stringappendString:[sender currentTitle]];//數字連續輸入;;;

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

self.num1 = [self.label.textdoubleValue];//將字串轉化為基礎資料型別 (Elementary Data Type)並賦給num1;

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

}

//運算子事件

- (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;

}

}

//等號事件

- (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;

}

}


//全部刪除的響應事件

- (void)deleteClick:(id)sender{

[self.stringsetString:@"" ];

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

self.label.text =self.string;

}



//消除一個數的響應事件

- (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 should 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 many 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 previously 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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.