iphoneアプリの開発
電卓アプリを作っています。
今困っているのは
1.割り算で小数点以下の計算ができない。
2.3つ以上の計算が(2×3×4のような)足し算しかできない。
3.間違えて数値を入力した場合に使うバックスペース的なボタンの作り方。
です。
どれか一つでもいいのでアドバイスいただけたらありがたいです。
#import "myViewController.h"
@implementation myViewController
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
startInput = YES;
currentValue = 0;
}
-(IBAction)numberButtonPressed:(id)sender
{
UIButton *b = (UIButton *)sender;
if( startInput ){
// 最初の1桁目が0なら表示しない
if( b.tag == 0 ) return;
// 新しく表示する文字列を作成
label.text = [NSString stringWithFormat:@"%d", b.tag];
startInput = NO;
} else {
// すでに表示されている文字列に連結
label.text = [NSString stringWithFormat:@"%@%d", label.text, b.tag];
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio play];
}
-(IBAction)equalButtonPressed:(id)sender
{
currentValue = sum;
sum = sum-sum;
// 直前に押された演算子で場合分け
if( operation == 0 ){
currentValue += [label.text intValue];
} else if( operation == 1 ){
currentValue -= [label.text intValue];
} else if( operation ==2){
currentValue *= [label.text intValue];
} else if (operation ==3){
currentValue /= [label.text intValue];
}
// 表示の更新
label.text = [NSString stringWithFormat:@"%d", currentValue];
startInput = YES;
label2.text =@"=";
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio play];
}
-(IBAction)opButtonPressed:(id)sender
{ UIButton *b = (UIButton *)sender;
// 現在値の保存
if( operation == 0 ){
currentValue= [label.text intValue];
sum +=currentValue;
label.text =[NSString stringWithFormat:@"%d", sum];
}
// 演算の保存
operation = b.tag;
startInput = YES;
if( operation == 0 ){
label2.text =@"+";
}
if( operation == 1 ){
label2.text =@"-";
}
if( operation == 2 ){
label2.text =@"×";
}
if( operation == 3 ){
label2.text =@"÷";
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio play];
}
-(IBAction)clearButtonPressed:(id)sender
{
label.text = @"0";
startInput = YES;
label2.text =@"";
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio play];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
お礼
talooさん 回答ありがとうございます。 今回の迷路は結構複雑な地形なので、当たり判定の方で作ってみます。 ありがとうございます。