OC xcode 计算器

  • 格式:docx
  • 大小:140.98 KB
  • 文档页数:9

//.m文件// EDViewController.m// jiSuanQi2//// Created by TY on 14-8-25.// Copyright (c) 2014年FengHua. All rights reserved.//#import "EDViewController.h"@interface EDViewController ()@end@implementation EDViewControllerint opert; //操作符的tag值NSString *oneString; //前字符串声明NSString *twoString;//后字符串声明double oneNumber ;//前操作数声明double twoNumber ;//后操作数的声明bool opertDeal = false;//是否按下操作符double equ = 0;//结果BOOL zero;//判断是否输入‘0’NSString *btnString;//操作的标志int oneLenth;//int twoLenth;//前后俩个字符的的长度标志bool ac = false;//.符号的标志int one,two,three;//用于计算闰年的三个常量- (void)viewDidLoad{[super viewDidLoad];oneString=@"";twoString=@"";oneNumber=0;twoNumber=0;opert = 0;//将各个变量初始化// Do any additional setup after loading the view, typically from a nib. }- (void)didReceiveMemoryWarning{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}- (IBAction)poin:(id)sender { //点得处理,包含‘0’的处理在内UIButton *btn = (UIButton*) sender;NSString *send = btn.titleLabel.text;NSRange oneRange = [oneString rangeOfString:@"."];//做字符串内是否有.NSRange twoRange = [twoString rangeOfString:@"."];//右字符串内是否有. int oneLenght = oneRange.length;//做字符串的长度int twoLengh =twoRange.length;//右字符串的长度//字符0的处理if([send isEqualToString:@"0"]){ //如果找到‘0’if (ac) { //没有找到‘.’//右字符串处理if ([twoString isEqualToString:send]) {twoString = [twoString stringByAppendingString:send];[belDisplay setText:twoString];}else{[belDisplay setText:send];twoString = send;[belDisplay setText:twoString];}}else{ //找到‘0’,同时找到‘.’if ([oneString isEqualToString:send]) {oneString = send;[belDisplay setText:oneString];}else{oneString = [oneString stringByAppendingString:send];[belDisplay setText:oneString];}}}else{//字符点的处理if (ac) {if ([oneString isEqualToString:@""]) { //如果左字符串为空,按下‘.’oneString = @"0.";//输出‘0.’[belDisplay setText:@"0."];}else{if (twoLengh == 0) { //如果右字符串长度为0,那么twoString = [twoString stringByAppendingString:send];[belDisplay setText:twoString];}else{[belDisplay setText:send];twoString = send;}}}else{if ([oneString isEqualToString:@""]) {oneString = @"0.";[belDisplay setText:@"0."];}else{if (oneLenght ==0) {oneString = [oneString stringByAppendingString:send];[belDisplay setText:oneString];}else{oneString = belDisplay.text;[belDisplay setText:oneString];}}}}}//小数点和‘0’的处理- (IBAction)btnNumber:(id)sender {belDisplay.text=@"0";UIButton *btn=(UIButton *)sender;NSString *btntext =btn.titleLabel.text; //按钮上的TXT值NSInteger shuzi =btn.tag;//获取按钮的tag值switch (shuzi) {case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:[self btnNumberDeal:btntext];//调用btnNumber方法break;default:break;}} //数字键的处理- (void)btnNumberDeal:(NSString *)text//操作数的处理{if (opertDeal){twoString = [twoString stringByAppendingString:text];twoNumber = [twoString doubleValue];[belDisplay setText:twoString];}else{oneString = [oneString stringByAppendingString:text];oneNumber = [oneString doubleValue];[belDisplay setText:oneString];}}- (void)biaozhi:(NSString *)arg//操作符的标识{if([arg isEqualToString:@"+"]){btnString = @"Add";}if([arg isEqualToString:@"-"]){btnString = @"Reduction";}if([arg isEqualToString:@"*"]){btnString =@"Multip";}if ([arg isEqualToString:@"/"]){btnString = @"Addlition";}}- (void)caozuofuDeal:(UIButton *)btn //操作符的处理{if(opertDeal){switch (opert) {//在按下操作符之后,通过判断收到的tag值,进行对应的运算操作case 11:equ = oneNumber + twoNumber;belDisplay.text=[NSString stringWithFormat:@"%g",equ];break;case 12:equ = oneNumber - twoNumber;belDisplay.text=[NSString stringWithFormat:@"%g",equ];break;case 13:equ = oneNumber * twoNumber;belDisplay.text=[NSString stringWithFormat:@"%g",equ];break;case 14:equ = oneNumber / twoNumber;belDisplay.text=[NSString stringWithFormat:@"%g",equ];if ((twoNumber ==0)&&[btnString isEqualToString:@"Division"]) {[belDisplay setText:@"0"];opert = 0;opertDeal=false;}break;default:break;}oneNumber=equ; //把计算结果传给oneNumber,然后作为下一次按下操作符之后的前操作数twoNumber=0;//把twoNumber的值归零,以便于再次按下操作符的时候作为后操作数twoString=@"";//同上,清空,存放接下来收到的字符串}else{opertDeal=true;opert=btn.tag;}[self biaozhi:btnString];}- (IBAction)opert:(UIButton *)sender//操作符按钮{UIButton *btn=(UIButton *)sender;NSInteger caozuo =btn.tag;//获取按钮的tag值switch (caozuo){case 11:case 12:case 13:case 14:[self caozuofuDeal:btn];break;}}- (IBAction)dengYu:(UIButton *)sender //等于按钮{UIButton *btn=(UIButton *)sender;NSInteger caozuo =btn.tag;//获取按钮的tag值switch (caozuo){case 15:[self caozuofuDeal:btn];break;}oneNumber = equ;twoNumber = 0;twoString = @"";opert = 0;opertDeal=false;}- (IBAction)xPin:(UIButton *)sender {UIButton * btn = (UIButton *)sender;NSInteger kexue = btn.tag;oneNumber = [oneString doubleValue];switch (kexue) {case 19:equ = log(oneNumber);[belDisplay setText:[NSString stringWithFormat:@"%g",equ]];break;case 20:equ = pow (oneNumber, 2);[belDisplay setText:[NSString stringWithFormat:@"%g",equ]];break;case 21:equ = pow(oneNumber, 3);[belDisplay setText:[NSString stringWithFormat:@"%g",equ]];break;case 22:equ = exp(oneNumber);[belDisplay setText:[NSString stringWithFormat:@"%g",equ]];break;case 23:equ = sqrt(oneNumber);[belDisplay setText:[NSString stringWithFormat:@"%g",equ]];break;case 24:equ = 1/oneNumber;[belDisplay setText:[NSString stringWithFormat:@"%g",equ]];break;case 25:one = fmod(oneNumber, 4);two = fmod(oneNumber, 100);three = fmod(oneNumber, 400);if ((one==0&&two!=0)||three==0) {[belDisplay setText:@"闰年"];} else {[belDisplay setText:@"非闰年"];}break;default:NSLog(@"test");break;}oneNumber = equ;twoNumber = 0;twoString = @"";opert = 0;opertDeal=false;}//科学计算按钮- (IBAction)tuiGe:(id)sender//退格按钮的处理{NSString *lableLength =[NSString stringWithFormat:@"%i",belDisplay.text.length ];//现在只输入了一位字符,按下退格键直接初始化显示器if ([lableLength isEqualToString:@"1"]) {[belDisplay setText:@"0"];oneString=@"";twoString=@"";oneNumber=0;twoNumber=0;opert = 0;opertDeal=false;}else{//当输入了多个字符时if (![lableLength isEqualToString:@"0"]) {//如果是前字符串if ([btnString isEqualToString:@""]) {NSString *lableValue = [belDisplay.text substringToIndex:belDisplay.text.length -1];oneString =lableValue;[belDisplay setText:lableValue];//右字符串处理}else {if ([twoString isEqualToString:@""]&&![btnString isEqualToString:@""]) {NSString *lableValue = [belDisplay.text substringToIndex:belDisplay.text.length -1];[belDisplay setText:lableValue];btnString = @"";}if (![twoString isEqualToString:@""]) {NSString *lableValue = [belDisplay.text substringToIndex:belDisplay.text.length -1];twoString =[twoString substringToIndex:twoString.length-1];[belDisplay setText:lableValue];}}}}}- (IBAction)cleabtn:(UIButton *)sender {[belDisplay setText:@"0"];oneString=@"";twoString=@"";oneNumber=0;twoNumber=0;opert = 0;opertDeal=false;}//清除按钮@end.h文件//// EDViewController.h// jiSuanQi2//// Created by TY on 14-8-25.// Copyright (c) 2014年FengHua. All rights reserved.//#import <UIKit/UIKit.h>#import <math.h>#define Add 11//加#define Reduction 12//减#define Multiplication 13//乘#define Addlition 14//除#define Equ 15//等于按钮#define point 16//操作符#define logx 19//log#define xpinfang 20//平方#define lifang 21//立方#define ex 22#define kaifang 23//开方#define daoshu 24//倒数#define runnian 25//闰年@interface EDViewController : UIViewController- (IBAction)xPin:(UIButton *)sender; //科学计算- (IBAction)tuiGe:(UIButton *)sender; //退格键- (IBAction)cleabtn:(UIButton *)sender; //清除@property (weak, nonatomic) IBOutlet UILabel *labelDisplay;//显示器- (IBAction)dengYu:(UIButton *)sender;//等于- (IBAction)poin:(id)sender;//点- (IBAction)opert:(UIButton *)sender; //操作符- (IBAction)btnNumber:(id)sender;//数字键@end。