【学习中的分享】 iOS利用UILabel和UIButton制作一个简单的计算器

本文介绍了一个简单的iOS平台计算器应用程序的实现过程,使用Objective-C语言创建了用户界面,并实现了基本的数学运算功能,如加减乘除等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学者,效果一般 ,不要见笑

#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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值