导航:
#simComputerView.xib
#AppDelegate.h
#AppDelegate.m
#VIewController.h
#VIewController.m
simComputerView.xib文件中未拖放控件
#AppDelegate.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property(strong,nonatomic)ViewController *vc;
@property (strong, nonatomic) UIWindow *window;
@end
#AppDelegate.m
#import "ViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//窗口加载前的准备工作
CGRect rect=[[UIScreen mainScreen]bounds];
self.window=[[UIWindow alloc]initWithFrame:rect];
self.vc=[[ViewController alloc]initWithNibName:@"simComputerView" bundle:nil];
self.window.rootViewController=self.vc;
[self.window makeKeyAndVisible];
return YES;
}
#ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
//用来存放输入的数字和操作
@property(nonatomic,retain) NSMutableArray *array;
//用来存放数字和操作符
@property(nonatomic,retain)NSMutableString * tempString;
//用来存放数字
@property(nonatomic,retain)NSMutableString * numString;
//代码生成的label用来显示数字操作符
@property(strong,nonatomic)IBOutlet UILabel *label;
//生成最终要显示的结果
-(NSString *)showLable;
@end
#VIewController.m
#import "ViewController.h"
@interface ViewController (){
NSString *str1;
NSString *str2;
NSString *str3;
NSString *str4;
NSString *str5;
NSString *str6;
NSString *str7;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//相关属性的初始化
//无法存入,若self.array不初始化
self.array=[[NSMutableArray alloc]init];
//点击的过程中label都是显示null,若不初始化
self.tempString=[[NSMutableString alloc]init];
//object cannot be nil,会报错
self.numString=[[NSMutableString alloc]init];
//赋值
str1=@"*";
str2=@"/";
str3=@"del";
str4=@"0";
str5=@"+";
str6=@"-";
str7=@"=";
//计算器界面的生成
for (int i=0;i<4;i++) {
for (int j=0; j<4; j++) {
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor=[UIColor darkGrayColor];
button.frame=CGRectMake((j+1)*50+20,(i+1)*50+70, 40, 40);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
int num=i*4+j+1;
switch (num) {
case 4:
[button setTitle:str1 forState:UIControlStateNormal];
break;
case 8:
[button setTitle:str2 forState:UIControlStateNormal];
break;
case 12:
[button setTitle:str3 forState:UIControlStateNormal];
break;
case 13:
[button setTitle:str4 forState:UIControlStateNormal];
break;
case 14:
[button setTitle:str5 forState:UIControlStateNormal];
break;
case 15:
[button setTitle:str6 forState:UIControlStateNormal];
break;
case 16:
[button setTitle:@"=" forState:UIControlStateNormal];
break;
default:
[button setTitle:[NSString stringWithFormat:@"%i",(i*3+j)+1] forState:UIControlStateNormal];
break;
}
//添加button的响应事件
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
//创建用于显示的label
self.label=[[UILabel alloc]init];
self.label.frame=CGRectMake(70, 70, 190, 40);
self.label.backgroundColor=[UIColor lightGrayColor];
[self.view addSubview:self.label];
}
//响应的操作
-(void)clickButton:(id)sender{
UIButton *btn=(UIButton *)sender;
NSString *str=[btn titleForState:UIControlStateNormal];
[self.tempString appendString:str];
//判断 “+”,“-”,“*”,“/”
if ([str isEqualToString:str1]||[str isEqualToString:str2]||[str isEqualToString:str5]||[str isEqualToString:str6]){
NSString *num1str=[self.numString mutableCopy];
[self.array addObject:num1str];
NSLog(@"%@",self.array);
[self.array addObject:str];
NSLog(@"%@",self.array);
[self.numString setString:@""];
self.label.text=[NSString stringWithFormat:@"%@",self.tempString];
}
//判断 “=”
if ([str isEqualToString:str7]) {
NSString *num2str=[self.numString mutableCopy];
[self.array addObject:num2str];
//
NSLog(@"%@",self.array);
self.label.text=[self showLable];
//removeAllobject reself.array
[self.array removeAllObjects];
NSLog(@"%@",self.array);
}
//点击是否时数字
if ([str intValue]<9&&[str intValue]>0){
[self.numString appendString:str];
self.label.text=[NSString stringWithFormat:@"%@",self.tempString];
}
//点击是否del
if ([str isEqualToString:str3]) {
self.label.text=@"";
[self.tempString setString:@""];
[self.numString setString:@""];
}
}
-(NSString *)getStringFromArray{
NSMutableString *restr=[NSMutableString stringWithString:@""];
for (id str in self.array) {
NSString *substring=(NSString *)str;
[restr appendString:substring];
}
return restr;
}
//
-(NSString *)showLable{
float num1=[[self.array objectAtIndex:0]floatValue];
char op=[[self.array objectAtIndex:1]characterAtIndex:0];
float num2=[[self.array objectAtIndex:2]floatValue];
double sum=0;
switch (op) {
case '+':
sum=num1+num2;
break;
case '-':
sum=num1-num2;
break;
case '*':
sum=num1*num2;
break;
case '/':
sum=num1/num2;
break;
}
return [NSString stringWithFormat:@"%.f%c%.f=%.2f",num1,op,num2,sum];
}