【学习中的分享】 iOS利用UILabel和UIButton制作一个简单的计算器
2014-08-07 16:42
696 查看
初学者,效果一般 ,不要见笑
#import <UIKit/UIKit.h>
@interface AppDelegate :
UIResponder <UIApplicationDelegate>
{
UILabel *_lable;
NSMutableString *_string;
BOOL _flag;
}
@property (strong,
nonatomic) UIWindow *window;
@end
****************************************************************************************
#import "AppDelegate.h"
#import <Foundation/Foundation.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc]
initWithFrame:[[UIScreenmainScreen]
bounds]];
// Override point for customization after application launch.
UIImage *image= [UIImageimageNamed:@"123.png"];
UIImageView *imageview=[[UIImageViewalloc]
initWithImage:image];
imageview.frame=CGRectMake(0,20,
320, 480);
[self.window
addSubview:imageview];
[imageview release];
_string = [[NSMutableStringalloc]init];
_flag = YES;
_lable = [[UILabelalloc]initWithFrame:CGRectMake(10,20,
300,100)];
_lable.textAlignment =NSTextAlignmentRight;
_lable.textColor=[UIColorgreenColor];
_lable.font=[UIFontsystemFontOfSize:25];
_lable.backgroundColor = [UIColorclearColor];
[self.windowaddSubview:_lable];
NSArray *array = [NSArrayarrayWithObjects:@"MC",@"M+",@"M-",@"MR",@"清除",nil];
for(int i =
0;i < 5; i++)
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(i*60+11,120,
58, 30);
button.backgroundColor = [UIColorwhiteColor];
button.titleLabel.font = [UIFontsystemFontOfSize:20];
[button setTitleColor:[UIColorredColor]
forState:UIControlStateNormal];
[button setTitleColor:[UIColorgreenColor]
forState:UIControlStateHighlighted];
button.backgroundColor = [UIColorclearColor];
button.showsTouchWhenHighlighted=YES;
[button setTitle:array[i]forState:UIControlStateNormal];
[self.windowaddSubview:button];
if(i == 4)
[button addTarget:selfaction:@selector(subCharToString)forControlEvents:UIControlEventTouchUpInside];
}
array = [NSArrayarrayWithObjects:@"7",@"8",@"9",@"+",@"4",@"5",@"6",@"-",@"1",@"2",@"3",@"*",@"0",@".",@"=",@"/",nil];
int value[] = {7,8,9,10,4,5,6,10,1,2,3,10,0,10,10,10};
for(int i =
0;i < 4; i++){
for(int j =0;j <
4; j++){
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame =
CGRectMake(j*75+10, i*40+150,74,
39);
[button setTitle:array[4*i+j]forState:UIControlStateNormal];
if(i == 3 && j ==2)
[button addTarget:selfaction:@selector(calCharToString)forControlEvents:UIControlEventTouchUpInside];
else
[button addTarget:selfaction:@selector(addCharToString:)forControlEvents:UIControlEventTouchUpInside];
button.tag = value[4*i+j];
button.titleLabel.font = [UIFontsystemFontOfSize:25];
button.backgroundColor=[UIColorclearColor];
button.showsTouchWhenHighlighted =YES;
[self.windowaddSubview:button];
if(i == 3 && j ==2)
[button addTarget:selfaction:@selector(calCharToString)forControlEvents:UIControlEventTouchUpInside];
}
}
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
return YES;
}
- (void)calCharToString
{
//- (NSInteger)calcString:(NSString *)mathString
_lable.text = [NSStringstringWithFormat:@"%d",[selfcalcString:_string]];
//[_string deleteCharactersInRange:NSMakeRange(0, _string.length-1)];
[_stringreplaceCharactersInRange:NSMakeRange(0,_string.length)withString:_lable.text];
_flag = YES;
}
- (BOOL)isNumber:(unichar)c
{
if (c<='9'&&c>='0')
return YES;
else
return NO;
}
- (NSInteger)calcString:(NSString *)mathString
{
NSMutableArray * array = [[NSMutableArrayalloc]init];
NSMutableArray * array1 = [[NSMutableArrayalloc]init];
NSString * str = [NSStringstringWithFormat:@"%@ ",mathString];
int len = 0, loc =0;
for (int i =
0; i < str.length; i++) {
if (![self
isNumber:[str characterAtIndex:i]]) {
[array addObject:[str
substringWithRange:NSMakeRange(loc, len)]];
[array1 addObject:[strsubstringWithRange:NSMakeRange(i,1)]];
len = 0;
loc = i+1;
}else{
len++;
}
}
if (!([array[0]length])) {
[array removeObjectAtIndex:0];
[array insertObject:[NSStringstringWithFormat:@"%d",0]atIndex:0];
}
while ([array1 containsObject:@"*"]||[array1containsObject:@"/"]) {
NSUInteger index = [array1
indexOfObject:@"*"];
NSUInteger index2 = [array1
indexOfObject:@"/"];
if (index < index2) {
int a = [[array
objectAtIndex:index]intValue];
int b = [[array
objectAtIndex:index+1]intValue];
a = a * b;
[array1 removeObjectAtIndex:index];
[array removeObjectAtIndex:index];
[array removeObjectAtIndex:index];
[array insertObject:[NSStringstringWithFormat:@"%d",a]
atIndex:index];
}
else
{
int a = [[array
objectAtIndex:index2]intValue];
int b = [[array
objectAtIndex:index2+1]intValue];
a = a / b;
[array1 removeObjectAtIndex:index2];
[array removeObjectAtIndex:index2];
[array removeObjectAtIndex:index2];
[array insertObject:[NSStringstringWithFormat:@"%d",a]
atIndex:index2];
}
}
while ([array1 containsObject:@"+"]||[array1containsObject:@"-"]) {
NSUInteger index = [array1
indexOfObject:@"+"];
NSUInteger index2 = [array1
indexOfObject:@"-"];
if (index < index2) {
int a = [[array
objectAtIndex:index]intValue];
int b = [[array
objectAtIndex:index+1]intValue];
a = a + b;
[array1 removeObjectAtIndex:index];
[array removeObjectAtIndex:index];
[array removeObjectAtIndex:index];
[array insertObject:[NSStringstringWithFormat:@"%d",a]
atIndex:index];
}
else
{
int a = [[array
objectAtIndex:index2]intValue];
int b = [[array
objectAtIndex:index2+1]intValue];
a = a - b;
[array1 removeObjectAtIndex:index2];
[array removeObjectAtIndex:index2];
[array removeObjectAtIndex:index2];
[array insertObject:[NSStringstringWithFormat:@"%d",a]
atIndex:index2];
}
}
return [array[0]intValue];
}
- (void)subCharToString
{
if(_string.length >0)
{
[_stringdeleteCharactersInRange:NSMakeRange(0,_string.length)];
_lable.text =_string;
}
}
- (void)addCharToString:(UIButton *)button
{
if(_flag ==
YES){
if(button.tag !=10)
[_stringreplaceCharactersInRange:NSMakeRange(0,_string.length)withString:@""];
_flag = NO;
}
[_stringappendString:[button
titleForState:UIControlStateNormal]];
_lable.text =_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:.
}
@end
#import <UIKit/UIKit.h>
@interface AppDelegate :
UIResponder <UIApplicationDelegate>
{
UILabel *_lable;
NSMutableString *_string;
BOOL _flag;
}
@property (strong,
nonatomic) UIWindow *window;
@end
****************************************************************************************
#import "AppDelegate.h"
#import <Foundation/Foundation.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc]
initWithFrame:[[UIScreenmainScreen]
bounds]];
// Override point for customization after application launch.
UIImage *image= [UIImageimageNamed:@"123.png"];
UIImageView *imageview=[[UIImageViewalloc]
initWithImage:image];
imageview.frame=CGRectMake(0,20,
320, 480);
[self.window
addSubview:imageview];
[imageview release];
_string = [[NSMutableStringalloc]init];
_flag = YES;
_lable = [[UILabelalloc]initWithFrame:CGRectMake(10,20,
300,100)];
_lable.textAlignment =NSTextAlignmentRight;
_lable.textColor=[UIColorgreenColor];
_lable.font=[UIFontsystemFontOfSize:25];
_lable.backgroundColor = [UIColorclearColor];
[self.windowaddSubview:_lable];
NSArray *array = [NSArrayarrayWithObjects:@"MC",@"M+",@"M-",@"MR",@"清除",nil];
for(int i =
0;i < 5; i++)
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(i*60+11,120,
58, 30);
button.backgroundColor = [UIColorwhiteColor];
button.titleLabel.font = [UIFontsystemFontOfSize:20];
[button setTitleColor:[UIColorredColor]
forState:UIControlStateNormal];
[button setTitleColor:[UIColorgreenColor]
forState:UIControlStateHighlighted];
button.backgroundColor = [UIColorclearColor];
button.showsTouchWhenHighlighted=YES;
[button setTitle:array[i]forState:UIControlStateNormal];
[self.windowaddSubview:button];
if(i == 4)
[button addTarget:selfaction:@selector(subCharToString)forControlEvents:UIControlEventTouchUpInside];
}
array = [NSArrayarrayWithObjects:@"7",@"8",@"9",@"+",@"4",@"5",@"6",@"-",@"1",@"2",@"3",@"*",@"0",@".",@"=",@"/",nil];
int value[] = {7,8,9,10,4,5,6,10,1,2,3,10,0,10,10,10};
for(int i =
0;i < 4; i++){
for(int j =0;j <
4; j++){
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame =
CGRectMake(j*75+10, i*40+150,74,
39);
[button setTitle:array[4*i+j]forState:UIControlStateNormal];
if(i == 3 && j ==2)
[button addTarget:selfaction:@selector(calCharToString)forControlEvents:UIControlEventTouchUpInside];
else
[button addTarget:selfaction:@selector(addCharToString:)forControlEvents:UIControlEventTouchUpInside];
button.tag = value[4*i+j];
button.titleLabel.font = [UIFontsystemFontOfSize:25];
button.backgroundColor=[UIColorclearColor];
button.showsTouchWhenHighlighted =YES;
[self.windowaddSubview:button];
if(i == 3 && j ==2)
[button addTarget:selfaction:@selector(calCharToString)forControlEvents:UIControlEventTouchUpInside];
}
}
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
return YES;
}
- (void)calCharToString
{
//- (NSInteger)calcString:(NSString *)mathString
_lable.text = [NSStringstringWithFormat:@"%d",[selfcalcString:_string]];
//[_string deleteCharactersInRange:NSMakeRange(0, _string.length-1)];
[_stringreplaceCharactersInRange:NSMakeRange(0,_string.length)withString:_lable.text];
_flag = YES;
}
- (BOOL)isNumber:(unichar)c
{
if (c<='9'&&c>='0')
return YES;
else
return NO;
}
- (NSInteger)calcString:(NSString *)mathString
{
NSMutableArray * array = [[NSMutableArrayalloc]init];
NSMutableArray * array1 = [[NSMutableArrayalloc]init];
NSString * str = [NSStringstringWithFormat:@"%@ ",mathString];
int len = 0, loc =0;
for (int i =
0; i < str.length; i++) {
if (![self
isNumber:[str characterAtIndex:i]]) {
[array addObject:[str
substringWithRange:NSMakeRange(loc, len)]];
[array1 addObject:[strsubstringWithRange:NSMakeRange(i,1)]];
len = 0;
loc = i+1;
}else{
len++;
}
}
if (!([array[0]length])) {
[array removeObjectAtIndex:0];
[array insertObject:[NSStringstringWithFormat:@"%d",0]atIndex:0];
}
while ([array1 containsObject:@"*"]||[array1containsObject:@"/"]) {
NSUInteger index = [array1
indexOfObject:@"*"];
NSUInteger index2 = [array1
indexOfObject:@"/"];
if (index < index2) {
int a = [[array
objectAtIndex:index]intValue];
int b = [[array
objectAtIndex:index+1]intValue];
a = a * b;
[array1 removeObjectAtIndex:index];
[array removeObjectAtIndex:index];
[array removeObjectAtIndex:index];
[array insertObject:[NSStringstringWithFormat:@"%d",a]
atIndex:index];
}
else
{
int a = [[array
objectAtIndex:index2]intValue];
int b = [[array
objectAtIndex:index2+1]intValue];
a = a / b;
[array1 removeObjectAtIndex:index2];
[array removeObjectAtIndex:index2];
[array removeObjectAtIndex:index2];
[array insertObject:[NSStringstringWithFormat:@"%d",a]
atIndex:index2];
}
}
while ([array1 containsObject:@"+"]||[array1containsObject:@"-"]) {
NSUInteger index = [array1
indexOfObject:@"+"];
NSUInteger index2 = [array1
indexOfObject:@"-"];
if (index < index2) {
int a = [[array
objectAtIndex:index]intValue];
int b = [[array
objectAtIndex:index+1]intValue];
a = a + b;
[array1 removeObjectAtIndex:index];
[array removeObjectAtIndex:index];
[array removeObjectAtIndex:index];
[array insertObject:[NSStringstringWithFormat:@"%d",a]
atIndex:index];
}
else
{
int a = [[array
objectAtIndex:index2]intValue];
int b = [[array
objectAtIndex:index2+1]intValue];
a = a - b;
[array1 removeObjectAtIndex:index2];
[array removeObjectAtIndex:index2];
[array removeObjectAtIndex:index2];
[array insertObject:[NSStringstringWithFormat:@"%d",a]
atIndex:index2];
}
}
return [array[0]intValue];
}
- (void)subCharToString
{
if(_string.length >0)
{
[_stringdeleteCharactersInRange:NSMakeRange(0,_string.length)];
_lable.text =_string;
}
}
- (void)addCharToString:(UIButton *)button
{
if(_flag ==
YES){
if(button.tag !=10)
[_stringreplaceCharactersInRange:NSMakeRange(0,_string.length)withString:@""];
_flag = NO;
}
[_stringappendString:[button
titleForState:UIControlStateNormal]];
_lable.text =_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:.
}
@end
相关文章推荐
- 使用JSP制作一个超简单的网页计算器的实例分享
- 使用JSP制作一个超简单的网页计算器的实例分享
- 利用java做一个简单的计算器
- 利用QuickPart制作一个简单的webpart
- 【iOS-Cocos2d游戏开发之二十】精灵的基础知识点总汇(位图操作/贴图更换/重排z轴等)以及利用CCSprite与CCLayerColor制作简单遮盖层!
- 利用Blend制作一个简单的用户控件外观
- ios学习笔记block回调的应用(一个简单的例子)
- 实例学习SSIS(一)--制作一个简单的ETL包
- 利用java做一个简单的计算器
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- 一个简单的jQuery插件制作 学习过程及实例
- 实例学习SSIS(一)--制作一个简单的ETL包
- QuickPart应用系列之一——利用QuickPart制作一个简单的webpart
- 利用java做一个简单的计算器
- QuickPart应用(1)-----利用QuickPart制作一个简单的webpart
- 一个简单的jQuery插件制作,学习过程及实例
- WPF学习——制作一个简单的录入界面(1): 添加需要的控件
- IOS之利用Xcode创建一个简单的IOS应用程序
- 制作一个简单的计算器
- 实例学习SSIS(一)--制作一个简单的ETL包