计算器

简要说明:

    该计算器可以实现多位数的加减乘除,还可以连加减。不能实现算术的优先级,若有好的想法,可以告知一二。

 

 

 

#import "AppDelegate.h"

#import "ViewController.h"

 

@interfaceAppDelegate ()

 

@end

 

@implementation AppDelegate

 

 

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

    self.window=[[UIWindow alloc]init];

    self.window.frame=[[UIScreenmainScreen]bounds];

    [self.windowmakeKeyAndVisible];

    ViewController *vc=[[ViewControlleralloc]init];

    self.window.rootViewController=vc;

    returnYES;

}

#import "ViewController.h"

 

@interfaceViewController ()

{

    UIButton *button;

    UILabel *label;

    UILabel *label1;

}

@end

 

@implementation ViewController

int k;

- (void)viewDidLoad {

    [superviewDidLoad];

     [self.viewsetBackgroundColor:[UIColorcolorWithPatternImage:[UIImageimageNamed:@"3.jpg"]]];

    label=[[UILabelalloc]init];

    label.frame=CGRectMake(140, 30, 90, 60);

    label.text=@"计算器";

    label.textColor=[UIColorredColor];

    label.font=[UIFontsystemFontOfSize:30];

    [self.viewaddSubview:label];

    label1=[[UILabelalloc]init];

    label1.backgroundColor=[UIColorwhiteColor];

    label1.frame=CGRectMake(0, 120, 375, 90);

    label1.textAlignment=NSTextAlignmentRight;

    [self.viewaddSubview:label1];

    

    

    

    NSArray *arr=@[@"sin",@"cos",@"tan",@"/",@"7",@"8",@"9",@"*",@"4",@"5",@"6",@"-",@"1",@"2",@"3",@"+",@"c",@"0",@".",@"="];

    for (int i=0; i<5; i++) {

        for (int j=0; j<4; j++) {

            button=[UIButtonbuttonWithType:UIButtonTypeCustom];

            button.frame=CGRectMake(13+j*90, 275+i*80, 80, 70);

            button.backgroundColor=[UIColorblueColor];

            button.titleLabel.font=[UIFontsystemFontOfSize:30];

            [buttonsetTitle:arr[k++] forState:UIControlStateNormal];

            [buttonaddTarget:selfaction:@selector(event:) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:button];

        }

    }

    k=0;

}

-(void) event:(UIButton *) btn

{

    static NSString *str=@"";//接收数据

    static NSString *str1=@"";//用于当前显示

    static CGFloat number,result_v=0;;//最终的处理数据

    static CGFloat num[20]={0,0,0,0,0,0,0,0,0,0,0,0};//存放数据

    static int n=0;//数字数组下标

    static NSString *result=@"";//运算结果

    staticint i,sign=1;//符号标志位sign=1做加法运算,sign=-1做减法运算,sign=2做乘法运算,sign=3做除法运算

    

    

    str=btn.titleLabel.text;

   if([str hasSuffix:@"0"]||[str hasSuffix:@"1"]||[str hasSuffix:@"2"]||[str hasSuffix:@"3"]||[str hasSuffix:@"4"]||[str hasSuffix:@"5"]||[str hasSuffix:@"6"]||[str hasSuffix:@"7"]||[str hasSuffix:@"8"]||[str hasSuffix:@"9"]||[str hasSuffix:@"."]){

       

       {if ([str hasSuffix:@"."]) {

        i=1;

       }

      number=number*10+[str floatValue];

           if (i==1) {

               number=number/10;

           }

       }

    }

   elseif(![str hasSuffix:@"c"])//判断是否为运算符,且不为清除符

   { i=0;

       num[n++] = number;//将从Button获得的数据进行存储

       number=0;//取完数把数据清零

       /*****************数据处理******************/

       if(sign==1||sign==-1)

           result_v += num[n-1]*sign;//进行加减运算,并存储结果

       

       else if(sign==2)

           result_v *= num[n-1];      //进行乘运算,并存储结果

       else if(sign==3)

           result_v /= num[n-1];     //进行除运算,并存储结果

       if([str hasSuffix:@"+"])

           sign=1;

       else if([str hasSuffix:@"-"])

           sign=-1;

       else if([str hasSuffix:@"*"])

           sign=2;

       else if([str hasSuffix:@"/"])

           sign=3;

       /*****************数据处理******************/

       

       else if([str hasSuffix:@"="])

       {

           result = [NSString stringWithFormat:@"%g",result_v];//将计算结果转化为NSString类型,便于显示

           NSLog(@"result_v=%g",result_v);

           result_v = 0;//得出结果后对结果存储变量清零

       }

   }

    if ([str hasSuffix:@"c"])    //撤销上一步的计算操作,再执行计算

    {

        str1=@"";

        str=@"";

        result=@"";

    }

    str1 = [NSString stringWithFormat:@"%@%@%@",str1,str,result];//粘帖字符串

    label1.text = str1;//送显示

    result = @"";//每次给结果字符串清零以免被粘帖

 

    

  

 

}

原文地址:https://www.cnblogs.com/lcl15/p/4954815.html