首页 > 代码库 > 计算器的实现代码
计算器的实现代码
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
UIView *containerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0, 320, 568)];
containerView.backgroundColor = [UIColorblackColor];
[self.windowaddSubview:containerView];
[containerViewrelease];
self.currentFirst = @"";
//显示数字
self.display = [[UILabelalloc]initWithFrame:CGRectMake(0,80,320,63)];
self.display.backgroundColor = [UIColorwhiteColor];
[containerViewaddSubview:self.display];
[self.displayrelease];
self.display.text =@"";
//回删键
UIButton *space = [UIButtonbuttonWithType:UIButtonTypeSystem];
space.frame =CGRectMake(0,163, 235, 65);
space.backgroundColor = [UIColorgrayColor];
space.layer.cornerRadius =10;
[space setTitle:@"回删"forState:UIControlStateNormal];
[containerViewaddSubview:space];
[space addTarget:selfaction:@selector(deleteOne:)forControlEvents:UIControlEventTouchUpInside];
//删除键
UIButton *delete = [UIButtonbuttonWithType:UIButtonTypeSystem];
delete.frame =CGRectMake(255,163 ,65, 65);
delete.backgroundColor = [UIColorgrayColor];
[delete setTitle:@"删除"forState:UIControlStateNormal];
delete.titleLabel.font = [UIFont systemFontOfSize:20];
delete.layer.cornerRadius =10;
[containerViewaddSubview:delete];
[delete addTarget:selfaction:@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 = [UIButtonbuttonWithType:UIButtonTypeSystem];
self.button.frame =CGRectMake(85 * j,248 + 85 * i, 65,65);
self.button.backgroundColor = [UIColorgrayColor];
self.button.layer.cornerRadius = 10;
self.button.layer.borderColor = [UIColor orangeColor].CGColor;
self.button.titleLabel.font = [UIFont systemFontOfSize:30];
[self.buttonsetTitle:buttonDisplay[k]forState:UIControlStateNormal];
[containerViewaddSubview:self.button];
[self.buttonaddTarget:selfaction:@selector(display:)forControlEvents:UIControlEventTouchUpInside];
[self.buttonaddTarget:selfaction:@selector(currentSymbol:)forControlEvents:UIControlEventTouchUpInside];
[self.buttonaddTarget:selfaction:@selector(calculate:)forControlEvents:UIControlEventTouchUpInside];
k++;
}
}
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
returnYES;
}
//获取当前值和前一个值
- (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.textsubstringToIndex:[self.display.textlength] - 1];
}else{
int k = 0;
for (int i =0; i < [self.display.textlength]; i++)
{
if ([self.display.textcharacterAtIndex:i] ==‘.‘) {
k++;
}
}
if (k > 0) {
;
}else{
self.display.text = [self.display.textstringByAppendingString:button.titleLabel.text];
}
}
}
else if ([self.display.text isEqualToString:@"0" ] && [button.titleLabel.textcompare:@"."] !=0)
{
self.display.text = button.titleLabel.text;
self.display.text = [self.display.textsubstringToIndex:[self.display.textlength] - 1];
}
else
{
if ([arry[i]isEqualToString:button.titleLabel.text ]) {
self.display.text = [self.display.textstringByAppendingString:button.titleLabel.text];
break;
}
}
}
}
//获取当前符号
- (void)currentSymbol:(UIButton *)button
{
NSArray *arry =@[@"+",@"-",@"*",@"/"];
for (int i =0; i < 4; i++) {
if ([button.titleLabel.textisEqualToString:arry[i]]) {
self.currentSymbol = button.titleLabel.text;
self.currentSecond =self.display.text;
self.display.text =@"";
break;
}
}
}
//计算
-(void)calculate:(UIButton *)button
{
if ([button.titleLabel.textisEqualToString:@"="]) {
if ([self.currentSymbolisEqualToString:@"/"]) {
self.currentFirst =_display.text;
float a = [self.currentSecondfloatValue] / [self.currentFirstfloatValue];
self.display.text = [NSStringstringWithFormat:@"%g",a];
}
else if ([self.currentSymbolisEqualToString:@"*"]) {
self.currentFirst =self.display.text;
double a = [self.currentSecondfloatValue] * [self.currentFirstfloatValue];
self.display.text = [NSStringstringWithFormat:@"%g",a];
}
else if ([self.currentSymbolisEqualToString:@"-"]) {
self.currentFirst =_display.text;
float a = [self.currentSecondfloatValue] - [self.currentFirstfloatValue];
self.display.text = [NSStringstringWithFormat:@"%g",a];
}
else if ([self.currentSymbolisEqualToString:@"+"]) {
self.currentFirst =self.display.text;
float a = [self.currentSecondfloatValue] + [self.currentFirstfloatValue];
self.display.text = [NSStringstringWithFormat:@"%g",a];
}
}
}
//回删
- (void)deleteOne:(UIButton *)button
{
if ([self.display.textlength] > 0) {
self.display.text = [self.display.textsubstringToIndex:[self.display.textlength] - 1];
}
}
//删除
- (void)deletText:(UIButton *)button
{
self.display.text =@"";
}
计算器的实现代码