calculate2
#if 0
#import <UIKit/UIKit.h>
@interface test8AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>{
UILabel *label;
UITextField *textField;
UIButton *button1,*button2,*button3,*button4,*button5,*button6,*button7,*button8,*button9,*button11,*button12,*button13,*button14,*button15,*button16, *button01,*button02,*button0,*button,*buttona,*buttonb;
NSMutableString *s,*s1,*string,*s2,*s3,*s9;
int a;
int m,mm,nn,x,y,z,k;
float add;
}
-(void)qu;
-(void)add;
-(void)plus;
-(void)cheng;
-(void)chu;
-(void)qingwan;
-(void)er;
-(void)shi;
-(int)xiaocifang:(int)x;
@property (strong, nonatomic) UIWindow *window;
@property (retain,nonatomic)NSMutableString *s,*s1,*s2,*s3,*string,*s5,*s6,*s7,*s8,*s9;
@end
#endif
#import "test8AppDelegate.h"
@implementation test8AppDelegate
@synthesize s,s1,s2,s3,string,s5,s6,s7,s8;
@synthesize window = _window;
- (void)dealloc
{
[_window release];
[super dealloc];
}
#pragma -----creatControl-------
-(void)creatControl{//创建界面元素。
UIImageView *imagev = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"bg.png"]];
imagev.frame=CGRectMake(0, 20, 320, 460);
[self.window addSubview:imagev];
//初始化字符串。
s=[[NSMutableStringalloc]init];
[sappendFormat:@""];
s1=[[NSMutableStringalloc]init];
[s1appendFormat:@""];
s2=[[NSMutableStringalloc]init];
[s2appendFormat:@""];
s3=[[NSMutableStringalloc]init];
[s3appendFormat:@""];
string=[[NSMutableStringalloc]init];
[stringappendFormat:@""];
s5=[[NSMutableStringalloc]init];
[s5appendFormat:@""];
s6=[[NSMutableStringalloc]init];
[s6appendFormat:@""];
s7=[[NSMutableStringalloc]init];
[s7appendFormat:@""];
s8=[[NSMutableStringalloc]init];
[s8appendFormat:@""];
s9=[[NSMutableStringalloc]init];
[s9appendFormat:@""];
//UITextField练习。
textField =[[UITextFieldalloc]initWithFrame:CGRectMake(0, 40, 320, 50)];
UIImageView *image1=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"半透明层.png"]];
image1.frame=CGRectMake(0, 40, 320, 50);
[self.window addSubview:image1];
textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
textField.textAlignment=UITextAlignmentRight;
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.font=[UIFontsystemFontOfSize:55];
textField.enabled = NO;
[textFieldsetDelegate:self];
[self.windowaddSubview:textField];
//UIButton练习。
button01=[UIButtonbuttonWithType:6];
button02=[UIButtonbuttonWithType:6];
buttona=[UIButtonbuttonWithType:1];
buttonb=[UIButtonbuttonWithType:1];
button11=[UIButtonbuttonWithType:6];
button12=[UIButtonbuttonWithType:6];
button13=[UIButtonbuttonWithType:6];
button14=[UIButtonbuttonWithType:6];
button15=[UIButtonbuttonWithType:6];
button16=[UIButtonbuttonWithType:6];
[buttonasetTitle:@"二进制"forState:NO];
[buttonbsetTitle:@"十进制"forState:NO];
[button01setBackgroundImage:[UIImageimageNamed:@"c.png"] forState:UIControlStateNormal];
[button02setBackgroundImage:[UIImageimageNamed:@"back.png"] forState:UIControlStateNormal];
[button11setBackgroundImage:[UIImageimageNamed:@"+.png"] forState:UIControlStateNormal];
[button12setBackgroundImage:[UIImageimageNamed:@"-.png"] forState:UIControlStateNormal];
[button13setBackgroundImage:[UIImageimageNamed:@"X.png"] forState:UIControlStateNormal];
[button14setBackgroundImage:[UIImageimageNamed:@"除.png"] forState:UIControlStateNormal];
[button15setBackgroundImage:[UIImageimageNamed:@"。.png"] forState:UIControlStateNormal];
[button16setBackgroundImage:[UIImageimageNamed:@"=.png"] forState:UIControlStateNormal];
button01.frame=CGRectMake(10, 120, 50, 40);
button02.frame=CGRectMake(260, 120, 50, 40);
buttona.frame=CGRectMake(0, 430, 70, 50);
buttonb.frame=CGRectMake(250, 430, 70, 50);
#pragma ====for循环出圆形。====
for (int i=0; i<=9; i++) {
NSString *abc=[NSString stringWithFormat:@"%d.png",i];
button=[UIButton buttonWithType:6];
[buttonsetBackgroundImage:[UIImageimageNamed:abc] forState:UIControlStateNormal];
button.frame=CGRectMake(125+sin(i*36*3.14/180)*130, 125+(130-cos(i*36*3.14/180)*130), 70, 70);
button.tag=i;
[buttonaddTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button];
}
button11.frame=CGRectMake(95, 215, 40, 40);
button12.frame=CGRectMake(179, 215, 40, 40);
button13.frame=CGRectMake(69, 299, 40, 40);
button14.frame=CGRectMake(199, 299, 40, 40);
button15.frame=CGRectMake(135, 335, 30, 30);
button16.frame=CGRectMake(120, 260, 70, 70);
button11.tag=12;
button12.tag=13;button13.tag=14;button14.tag=15;button15.tag=16;
button16.tag=17;button01.tag=31;button02.tag=32;
buttona.tag=97;buttonb.tag=98;
[button11addTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[button12addTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[button13addTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[button14addTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[button15addTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[button16addTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[button01addTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[button02addTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[buttonaaddTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
[buttonbaddTarget:selfaction:@selector(doCalculate:) forControlEvents:UIControlEventTouchUpInside];
//button长按事件。
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(btnLong:)];
longPress.minimumPressDuration=0.8;//chang an shijian.
[button01addGestureRecognizer:longPress];
// button01.alpha=0.5//tou ming du.
[self.windowaddSubview:button01];
[self.windowaddSubview:button02];
[self.windowaddSubview:buttona];
[self.windowaddSubview:buttonb];
[self.windowaddSubview:button11];
[self.windowaddSubview:button12];
[self.windowaddSubview:button13];
[self.windowaddSubview:button14];
[self.windowaddSubview:button15];
[self.windowaddSubview:button16];
if ([sisEqualToString:@""]) {
[s setString:@"0"];
NSLog(@"zui chu de s===%@",s);
textField.text = s;//s 永远是结果。
}
}
#pragma ====doCalculate=====
-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(@"长按事件");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"消息" message:@"确定删除该模式吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
[alert show];
}
}
-(void)doCalculate:(id)context{
a=[context tag];
[textFieldresignFirstResponder];
if (textField.text==NULL) {
textField.text=@"";
}
//判断是数字时候
if (a>=0&&a<=9) {
if (k==1) {
[self qingwan];
}
if ([string hasPrefix:@"0."]) {
[stringappendFormat:@"%d",a];//string 是点完数字时候存放的值。
}
else if ([string intValue]==0) {
[string setString:@""];
[stringappendFormat:@"%d",a];//string 是点完数字时候存放的值。
}
else{
[stringappendFormat:@"%d",a];//string 是点完数字时候存放的值。
}
s=string;//s 为结果。
// NSLog(@"s===%@",s);
// [self qu];
if (textField.text.length<9) {
textField.text = s;//s 永远是结果。
}
k=0;
[s9 setString:@""];
[s9 appendFormat:string];//连等于。
}
if (a==97) {
[s setString:textField.text];
[s2 setString:s];
[s3 setString:@""];
[s6 appendFormat:@""];
[s6 setString:s];
[s5 setString:@""];
if ([s6 intValue]==1) {
[s7 setString:@"1"];
}
if ([s6 intValue]==0) {
[s7 setString:@"0"];
}
while ([s6 intValue]>1) {
[ s5 appendFormat:@"%d",([s6 intValue]%2)];//每次的余数
[s3 setString:@""];
[ s3 appendFormat:@"%d",([s6 intValue]/2)];
[s6 setString:s3];
[s7 setString:@"1"];
}
if ([s5 isEqualToString:@""]) {
mm=2;
nn=mm;
}
mm=[s5 length];
nn=mm;
for (int i=0; i<nn; i++) {
[s8 setString:@""];
[s8setString:[s5substringFromIndex:mm-1]];
[s7 appendFormat:@"%@",s8];
[s5deleteCharactersInRange:NSMakeRange(mm-1, 1)];
mm--;
}
textField.text=s7;
}
if (a==98) {
[self shi];
}
if (a==16) {
if ([string isEqualToString:@""]) {
[stringappendFormat:@"0."];//string 是点完数字时候存放的值。
}
if ([string rangeOfString:@"."].length>0 ) {//判断含有点没有。
[string appendFormat:@""];
}
else{
[stringappendFormat:@"."];//string 是点完数字时候存放的值。
}
s=string;//s 为结果。
NSLog(@"s===%@",s);
// [self qu];
textField.text = s;//s 永远是结果。
}
//判断是加号的时候。
if (a==12) {//点加号的时候。把s1赋值为s,同时把string赋值为0;
[self add];
}
//判断是减号的时候。
if (a==13) {//点加号的时候。把s1赋值为s,同时把string赋值为0;
[self plus];
}
//判断是乘号的时候。
if (a==14) {//点乘号的时候。把s1赋值为s,同时把string赋值为0;
[self cheng];
}
//判断是除号的时候。
if (a==15) {//点加号的时候。把s1赋值为s,同时把string赋值为0;
[self chu];
}
if (a==17) {
if (m==1) {
[self add];
}
if (m==2) {
[self plus];
}
if (m==3) {
[self cheng];
}
if (m==4) {
[self chu];
}
k=1;
}
//删除一个:
if (a==32) {
if (k==1) {
[self qingwan];
}
NSLog(@"sshan=%@",s);
[s setString:textField.text];
NSLog(@"sshansss=%@",s);
if ([s length]!=1) {
[sdeleteCharactersInRange:NSMakeRange([slength]-1, 1)];
textField.text=s;
}
else
{
[s setString:@"0"];
NSLog(@"wo d s==%@",s);
[self qu];
textField.text=s;
}
}
//删除全部;
if (a==31) {
[s setString:@""];
[s1 setString:@""];
[s2 setString:@""];
[s3 setString:@""];
[string setString:@""];
NSLog(@"string==%@",string);
[s setString:@"0"];
[self qu];
textField.text = s;//s 永远是结果。
[string setString:@"0"];
}
}
-(void)qingwan{
[s setString:@""];
[s1 setString:@""];
[s2 setString:@""];
[s3 setString:@""];
[s6 setString:@""];
[s7 setString:@""];
[s8 setString:@""];
[string setString:@""];
NSLog(@"string==%@",string);
[s setString:@"0"];
[self qu];
textField.text = s;//s 永远是结果。
[string setString:@"0"];
}
#pragma =====qu=====
//在textField.text=s的前面判断多余的0去掉
-(void)qu{
for (int i=0; i<6; i++) {
if ([s rangeOfString:@"."].length>0) {
if ([s hasSuffix:@"0"]) {
[sdeleteCharactersInRange:NSMakeRange([slength]-1, 1)];
}
if ([s hasSuffix:@"."]) {
[sdeleteCharactersInRange:NSMakeRange([slength]-1, 1)];
}
}
}
}
-(void)er{
[s setString:textField.text];
[s2 setString:s];
[s3 setString:@""];
[s6appendFormat:@""];
[s6 setString:s];
[s5 setString:@""];
if ([s6 intValue]==1) {
[s7 setString:@"1"];
}
if ([s6 intValue]==0) {
[s7 setString:@"0"];
}
while ([s6 intValue]>1) {
[ s5 appendFormat:@"%d",([s6 intValue]%2)];//每次的余数
[s3 setString:@""];
[ s3 appendFormat:@"%d",([s6 intValue]/2)];
[s6 setString:s3];
[s7 setString:@"1"];
}
if ([s5isEqualToString:@""]) {
mm=2;
nn=mm;
}
mm=[s5 length];
nn=mm;
for (int i=0; i<nn; i++) {
[s8 setString:@""];
[s8setString:[s5substringFromIndex:mm-1]];
[s7 appendFormat:@"%@",s8];
[s5deleteCharactersInRange:NSMakeRange(mm-1, 1)];
mm--;
}
textField.text=s7;
}
-(void)shi{//十进制;
[s setString:textField.text];
[s8 setString:@""];
[s8 setString:s];
[s5 setString:@""];
[s5 setString:s];
mm=[s length]-1;
[s7 setString:@""];
[s7 setString:s];
x=0;
for (int i=0; i<[s length]; i++) {
nn=[self xiaocifang:i];
NSLog(@"nn==%d",nn);
// [s8 substringFromIndex:mm];
[s5 setString:@""];
[s5 setString:s7];
[s5deleteCharactersInRange:NSMakeRange(0, mm)];
[s8 setString:@""];
[s8 setString:s5];
NSLog(@"s8===%@",s8);
x=[s8 intValue]*nn+x;
[s7deleteCharactersInRange:NSMakeRange(mm, 1)];
mm--;
}
[s8 setString:@""];
[s8appendFormat:@"%d",x];
NSLog(@"x===%d",x);
textField.text=s8;
}
//2 的x 次方。
-(int)xiaocifang:(int)h{
int p=1;
for (int i=0; i<h; i++) {
p=2*p;
}
return p;
}
#pragma =====add=====
-(void)add{
if (m==2) {
[self plus];
}
if (m==3) {
[self cheng];
}
if (m==4) {
[self chu];
}
if (k==1) {
add=[s9floatValue]+[s1floatValue];
}
else if(k==0){
add=[stringfloatValue]+[s1floatValue];
}
[s setString:@""];
[sappendFormat:@"%f",add];
[s1 setString:@""];
[s1appendFormat:@"%@",s];//s1 为真正的结果。
NSLog(@"s===%@",s);
[self qu];
textField.text = s;//s 永远是结果。这里有个疑问,顺序的疑问。一会在解决。
[string setString:@""];
m=1;
k=0;
}
#pragma =======plus========
-(void)plus{
if (m==1) {
[self add];
}
if (m==3) {
[self cheng];
}
if (m==4) {
[self chu];
}
if ([s1isEqualToString: @""]) {
add=[s floatValue];
}
else{
if (k==1) {
add=[s1 floatValue]-[s9 floatValue];
}
else{
add=[s1 floatValue]-[string floatValue];
}
}
[s setString:@""];
[sappendFormat:@"%f",add];
[s1 setString:@""];
[s1appendFormat:@"%@",s];//s1 为真正的结果。
NSLog(@"s===%@",s);
[self qu];
textField.text = s;//s 永远是结果。
[string setString:@""];
m=2;
k=0;
}
#pragma =======cheng=====
-(void)cheng{
if (m==1) {
[self add];
}
if (m==2) {
[self plus];
}
if (m==4) {
[self chu];
}
if ([s1isEqualToString:@""]) {
add=[textField.text floatValue];
}
else{
if (k==1) {
add=[s9 floatValue]*[s1 floatValue];
}
else{
add=[s1 floatValue];
}
}
[s setString:@""];
[sappendFormat:@"%f",add];
[s1 setString:@""];
[s1appendFormat:@"%@",s];//s1 为真正的结果。
NSLog(@"s=xiaos==%@",s);
[self qu];
textField.text = s;//s 永远是结果。
[string setString:@""];
m=3;
k=0;
}
#pragma =======chu=======
-(void)chu{
if (m==1) {
[self add];
}
if (m==3) {
[self cheng];
}
if (m==2) {
[self plus];
}
if ([s1isEqualToString:@""]) {
if ([s9 isEqualToString:@""]) {
add=[textField.text floatValue];
NSLog(@"aa111aa");
}
else if(s1.length>0){
if (k==1) {
add=[s1 floatValue]/[s9 floatValue];
}
else{
add=[s9 floatValue];
}
}else{
add=[s9 floatValue];
}
NSLog(@"aaaaaa");
}
else{
if (k==1) {
if ([s9 isEqualToString:@""]) {
add=[s1 floatValue];
NSLog(@"s999fg===%@",s9);
}
else{
add=[s1 floatValue]/[s9 floatValue];
NSLog(@"s555sdf===%@",s9);
}
}
else{
NSLog(@"ssssssdd");
if ([string isEqualToString:@""]) {
add=[s1 floatValue];
}
else{
add=[s1 floatValue]/[s9 floatValue];
}
}
}
[s setString:@""];
[sappendFormat:@"%f",add];
[s1 setString:@""];
[s1appendFormat:@"%@",s];//s1 为真正的结果。
NSLog(@"s===%@",s);
[s setString:s1];
NSLog(@"sssss===%@",s);
[self qu];
textField.text = s;//s 永远是结果。
[string setString:@""];
m=4;
k=0;
// [s9 setString:@""];
// [s9 appendFormat:@"1"];
}
#pragma ====系统提供。=======
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
[selfcreatControl];
returnYES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location>1) {
return NO;
}
returnYES;
}
@end