[iOS] 計算器

         可以連續運算,有小數點、百分號。

AppDelegate.h

 



#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

{
    UILabel*_label;
}
@property (strong, nonatomic) UIWindow *window;
//存儲操作符
@property(assign,nonatomic)int oper;
//存儲第一個操作數
@property(assign,nonatomic)float one;
//判斷小數點
@property(assign,nonatomic)BOOL point;
//判斷連續運算
@property(assign,nonatomic)BOOL con;
//是否剛按過等於號
@property(assign,nonatomic)BOOL equal;
@end

 

//
//  AppDelegate.m
//

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    UIView *view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    [self.window makeKeyAndVisible];
    [self.window addSubview:view];
    
    //結果輸出label
    _label=[[UILabel alloc]initWithFrame:CGRectMake(20, 30, 280, 55)];
    _label.textColor=[UIColor whiteColor];
    _label.textAlignment=NSTextAlignmentRight;
    _label.backgroundColor=[UIColor blackColor];
    _label.font=[UIFont systemFontOfSize:30];
    _label.text=@"0";
    [view addSubview:_label];
    [_label release];
    
    //循環產生除了最後一行的button
    int x=20;
    int y=95;
    for (int i=0; i<16; i++) {
        UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
        
        button.frame=CGRectMake(x, y, 55, 55);
        NSString*picName=[NSString stringWithFormat:@"%d",i];
        [button setImage:[UIImage imageNamed:picName] forState:UIControlStateNormal];
        button.tag=i+1001;
//      button.backgroundColor=[UIColor cyanColor];
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        x=x+75;
        if ((i+1)%4==0) {
            y=y+75;
        }
        if(x>=320)x=20;
    }
    //最後一行
    //0按鍵
    UIButton*button0=[UIButton buttonWithType:UIButtonTypeCustom];
    button0.frame=CGRectMake(20, 395, 130, 55);
    [button0 setImage:[UIImage imageNamed:@"0_btn_highlighted"] forState:UIControlStateNormal];
    [button0 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button0.tag=1017;
    [view addSubview:button0];
    //點按鍵
    UIButton*buttonPoint=[UIButton buttonWithType:UIButtonTypeCustom];
    buttonPoint.frame=CGRectMake(170, 395, 55, 55);
    [buttonPoint setImage:[UIImage imageNamed:@"dian_btn_highlighted"] forState:UIControlStateNormal];
    buttonPoint.tag=1018;
    [buttonPoint addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:buttonPoint];
    
    //等號
    UIButton*buttonDeng=[UIButton buttonWithType:UIButtonTypeCustom];
    buttonDeng.frame=CGRectMake(245, 395, 55, 55);
    [buttonDeng setImage:[UIImage imageNamed:@"dengyu_btn_highlighted"] forState:UIControlStateNormal];
    buttonDeng.tag=1019;
    [buttonDeng addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:buttonDeng];
    return YES;
}
//功能實現
-(void)click:(UIButton*)button{
    int tag=button.tag;
    NSMutableString*temp=[[NSMutableString alloc]init] ;
    //非數字鍵
    if (tag==1001||tag==1002||tag==1003||tag==1004||tag==1008||tag==1012||tag==1016) {
        switch (tag) {
            case 1004:{
                //判斷是不是連續運算
                if (_oper!=0) {
                    _con=YES;
                    switch (self.oper) {
                        case 1:
                            _one=_one+[_label.text floatValue];
                            break;
                        case 2:
                            _one=_one-[_label.text floatValue];
                            break;
                        case 3:
                            _one=_one*[_label.text floatValue];
                            break;
                        case 4:
                            _one=_one/[_label.text floatValue];                            break;
                    }
                    temp=[NSMutableString stringWithFormat:@"%g",_one];
                }
                //存儲本次操作符
                _oper=4;
                break;
            }
        //乘號
            case 1008:
                if (_oper!=0) {
                    _con=YES;
                    switch (self.oper) {
                        case 1:
                            _one=_one+[_label.text floatValue];
                            break;
                        case 2:
                            _one=_one-[_label.text floatValue];
                            break;
                        case 3:
                            _one=_one*[_label.text floatValue];
                            break;
                        case 4:
                            _one=_one/[_label.text floatValue];                            break;
                    }
                    
                    
                    temp=[NSMutableString stringWithFormat:@"%g",_one];
                }
                _oper=3;
                break;
                //加號
            case 1012:{
                if (_oper!=0) {
                   _con=YES;
                    switch (self.oper) {
                        case 1:
                            _one=_one+[_label.text floatValue];
                            break;
                        case 2:
                           _one=_one-[_label.text floatValue];
                            break;
                        case 3:
                            _one=_one*[_label.text floatValue];
                            break;
                        case 4:
                           _one=_one/[_label.text floatValue];                            break;
                    }
                temp=[NSMutableString stringWithFormat:@"%g",_one];
                }
                _oper=1;
                break;
            }
                //減號
            case 1016:
                if (_oper!=0) {
                    _con=YES;
                    switch (self.oper) {
                        case 1:
                            _one=_one+[_label.text floatValue];
                            break;
                        case 2:
                            _one=_one-[_label.text floatValue];
                            break;
                        case 3:
                            _one=_one*[_label.text floatValue];
                            break;
                        case 4:
                            _one=_one/[_label.text floatValue];                            break;
                    }
                    
                    
                    temp=[NSMutableString stringWithFormat:@"%g",_one];
                }
                _oper=2;
                break;
                //清除按鍵
            case 1001:
                _oper=0;
                temp=[NSMutableString stringWithString:@"0"];
                break;
                
               case 1002:
                temp=[NSMutableString stringWithFormat:@"%g",-[_label.text floatValue]];
                break;
            case 1003:
                temp=[NSMutableString stringWithFormat:@"%g",[_label.text floatValue]/100];
                break;
            default:
                break;
        }
           _point=NO;
        _one=[_label.text floatValue];
        [_label setText:temp];
    }
    //等號
    else if (tag==1019) {
        float two=[_label.text floatValue];
        float result = 0;
        switch (self.oper) {
            case 1:
                result=self.one+two;
                break;
            case 2:
                result=self.one-two;
                break;
            case 3:
                result=self.one*two;
                break;
            case 4:
                result=self.one/two;
                break;
        }
        _equal=YES;
        _point=NO;
        _oper=0;
        temp=[NSMutableString stringWithFormat:@"%g",result];
        [_label setText:temp];

    }//=
    
    //按下小數點
    else if (tag==1018) {
        if (_point) {
        }
        else {
            if ([_label.text floatValue]==0) {
                [temp appendString:@"0."];
                  _point=YES;
            }
            else{
                
            [temp setString:_label.text];
            [temp appendString:@"."];
            _point=YES;
            }
            [_label setText:temp];
        }
        
    }
    
    //數字鍵
    else{
        int number;
        switch (tag) {
            case 1005://7
                number=7;
                break;
            case 1006://8
                number=8;
                break;
            case 1007://9
                number=9;
                break;
            case 1009://4
                number=4;
                break;
            case 1010://5
                number=5;
                break;
            case 1011://6
                number=6;
                break;
            case 1013://1
                number=1;
                break;
            case 1014://2
                number=2;
                break;
            case 1015://3
                number=3;
                break;
            case 1017://0
                number=0;
                break;
                
            default:
                break;
        }
        
        if (_label.text!=nil&&number!=0) {
            [temp setString:_label.text];

        }
        if ([_label.text floatValue]!=0&&number==0) {
           [temp setString:_label.text];
        }
        if ([_label.text floatValue]==0&&number!=0&&[_label.text length]==1) {
            temp=[NSMutableString stringWithString:@""];
        }
        if (_con) {
            _one=[_label.text floatValue];
            temp=[NSMutableString stringWithString:@""];
            _con=NO;
        }
        if (_equal) {
            temp=[NSMutableString stringWithString:@""];
            _equal=NO;
        }
        [temp appendString:[NSString stringWithFormat:@"%d",number]];
            [_label setText:temp];
    }
}
- (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


效果圖:

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章