#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.
UIView *containerView = [[UIView
alloc]initWithFrame:CGRectMake(0,
0,
320, 568)];
containerView.backgroundColor = [UIColor
blackColor];
[self.window
addSubview:containerView];
[containerView
release];
self.currentFirst
= @"";
//显示数字
self.display
= [[UILabel
alloc]initWithFrame:CGRectMake(0,
80,
320,
63)];
self.display.backgroundColor
= [UIColor
whiteColor];
[containerView
addSubview:self.display];
[self.display
release];
self.display.text =
@"";
//回删键
UIButton *space = [UIButton
buttonWithType:UIButtonTypeSystem];
space.frame =
CGRectMake(0,
163,
235, 65);
space.backgroundColor = [UIColor
grayColor];
space.layer.cornerRadius =
10;
[space
setTitle:@"回删"
forState:UIControlStateNormal];
[containerView
addSubview:space];
[space
addTarget:self
action:@selector(deleteOne:)
forControlEvents:UIControlEventTouchUpInside];
//删除键
UIButton *delete = [UIButton
buttonWithType:UIButtonTypeSystem];
delete.frame =
CGRectMake(255,163 ,
65,
65);
delete.backgroundColor = [UIColor
grayColor];
[delete
setTitle:@"删除"
forState:UIControlStateNormal];
delete.titleLabel.font
= [UIFont
systemFontOfSize:20];
delete.layer.cornerRadius =
10;
[containerView
addSubview:delete];
[delete
addTarget:self
action:@selector(deletText:)
forControlEvents:UIControlEventTouchUpInside];
NSArray * buttonDisplay =
@[@"7",@"8",@"9",@"/",@"4",@"5",@"6",@"*",@"1",@"2",@"3",@"-",@".",@"0",@"=",@"+"];
int k =
0;
for (int i =
0; i <
4; i++) {
for (int j =
0; j <
4; j++) {
self.button = [UIButton
buttonWithType:UIButtonTypeSystem];
self.button.frame =
CGRectMake(85 * j,
248 +
85 * i, 65,
65);
self.button.backgroundColor = [UIColor
grayColor];
self.button.layer.cornerRadius
= 10;
self.button.layer.borderColor
= [UIColor
orangeColor].CGColor;
self.button.titleLabel.font
= [UIFont
systemFontOfSize:30];
[self.button
setTitle:buttonDisplay[k]
forState:UIControlStateNormal];
[containerView
addSubview:self.button];
[self.button
addTarget:self
action:@selector(display:)
forControlEvents:UIControlEventTouchUpInside];
[self.button
addTarget:self
action:@selector(currentSymbol:)
forControlEvents:UIControlEventTouchUpInside];
[self.button
addTarget:self
action:@selector(calculate:)
forControlEvents:UIControlEventTouchUpInside];
k++;
}
}
self.window.backgroundColor
= [UIColor
whiteColor];
[self.window
makeKeyAndVisible];
return
YES;
}
//获取当前值和前一个值
- (void)display:(UIButton
*)button
{
NSArray *arry =
@[@"7",@"8",@"9",@"4",@"5",@"6",@"1",@"2",@"3",@".",@"0"];
for (int i =
0; i <
11; i++) {
if ([button.titleLabel.text
isEqualToString:@"." ]) {
if ([self.display.text
length] ==
0) {
self.display.text = [@"0"
stringByAppendingString:button.titleLabel.text];
self.display.text = [self.display.text
substringToIndex:[self.display.text
length] -
1];
}else{
int k =
0;
for (int i =
0; i < [self.display.text
length]; i++)
{
if ([self.display.text
characterAtIndex:i] ==
‘.‘) {
k++;
}
}
if (k >
0) {
;
}else{
self.display.text = [self.display.text
stringByAppendingString:button.titleLabel.text];
}
}
}
else
if ([self.display.text
isEqualToString:@"0" ] && [button.titleLabel.text
compare:@"."] !=
0)
{
self.display.text = button.titleLabel.text;
self.display.text = [self.display.text
substringToIndex:[self.display.text
length] -
1];
}
else
{
if ([arry[i]
isEqualToString:button.titleLabel.text ]) {
self.display.text = [self.display.text
stringByAppendingString:button.titleLabel.text];
break;
}
}
}
}
//获取当前符号
- (void)currentSymbol:(UIButton
*)button
{
NSArray *arry =
@[@"+",@"-",@"*",@"/"];
for (int i =
0; i <
4; i++) {
if ([button.titleLabel.text
isEqualToString:arry[i]]) {
self.currentSymbol = button.titleLabel.text;
self.currentSecond =
self.display.text;
self.display.text =
@"";
break;
}
}
}
//计算
-(void)calculate:(UIButton
*)button
{
if ([button.titleLabel.text
isEqualToString:@"="]) {
if ([self.currentSymbol
isEqualToString:@"/"]) {
self.currentFirst =
_display.text;
float a = [self.currentSecond
floatValue] / [self.currentFirst
floatValue];
self.display.text = [NSString
stringWithFormat:@"%g",a];
}
else
if ([self.currentSymbol
isEqualToString:@"*"]) {
self.currentFirst =
self.display.text;
double a = [self.currentSecond
floatValue] * [self.currentFirst
floatValue];
self.display.text = [NSString
stringWithFormat:@"%g",a];
}
else
if ([self.currentSymbol
isEqualToString:@"-"]) {
self.currentFirst =
_display.text;
float a = [self.currentSecond
floatValue] - [self.currentFirst
floatValue];
self.display.text = [NSString
stringWithFormat:@"%g",a];
}
else
if ([self.currentSymbol
isEqualToString:@"+"]) {
self.currentFirst =
self.display.text;
float a = [self.currentSecond
floatValue] + [self.currentFirst
floatValue];
self.display.text = [NSString
stringWithFormat:@"%g",a];
}
}
}
//回删
- (void)deleteOne:(UIButton
*)button
{
if ([self.display.text
length] >
0) {
self.display.text = [self.display.text
substringToIndex:[self.display.text
length] -
1];
}
}
//删除
- (void)deletText:(UIButton
*)button
{
self.display.text =
@"";
}