私はコンバーター アプリを作成していますが、変換された 5 単位を超えて動作させるには約 200 行のコードを入力する必要があることに気付いた後、より適切な変換計算が必要です。
私が現在持っているのは、ホイールから選択したユニットを見つけるifelse、答えるべきものを見つけるためのスティング、および計算するフロートです。このatmのように見えます:
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController;
@synthesize _convertFrom, _convertTo, _convertRates;
@synthesize inputText, picker, resultLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_convertFrom = @[@"MTPA", @"MMcf/day",
@"Mill.Sm3/day", @"MMBTU", @"Boe/day"];
_convertRates = @[ @1.0f, @2.0f, @3.0f,
@4.0f, @5.0f];
_convertTo = @[@"MTPA", @"MMcf/day",
@"Mill.Sm3/day", @"MMBTU", @"Boe/day"];
_convertRates = @[ @1.0f, @2.0f, @3.0f,
@4.0f, @5.0f];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
//dont forget to put something in here later
}
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTouched:(id)sender
{
[inputText resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if (component == 0) {
return [_convertFrom count];
}
return [_convertTo count];
}
- (NSString *) pickerView: (UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == 0) {
return [_convertFrom objectAtIndex:row];
}
return [_convertTo objectAtIndex:row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
float convertFrom = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
float convertTo = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue];
float input = [inputText.text floatValue];
float to = convertTo;
float from = convertFrom;
float convertValue = input;
float MTPATilMTPAFloat = convertValue * 1;
float MTPATilMMScfdayFloat = convertValue * 133.425;
float MTPATilMillSm3dayFloat = convertValue * 3.779735;
float MTPATilMMBTUFloat = convertValue * 4;
float MTPATilboedayFloat = convertValue * 5;
float MMScfdayTilMTPAFloat = convertValue * 0.5;
float MMScfdayTilMMScfdayFloat = convertValue * 1;
float MMScfdayTilMillSm3dayFloat = convertValue * 6;
float MMScfdayTilMMBTUFloat = convertValue * 7;
float MMScfdayTilboedayFloat = convertValue * 8;
float MillSm3dayTilMTPAFloat = convertValue / 1;
float MillSm3dayTilMMScfdayFloat = convertValue / 2;
float MillSm3dayTilMillSm3dayFloat = convertValue / 3;
float MillSm3dayTilMMBTUFloat = convertValue / 4;
float MillSm3dayTilboedayFloat = convertValue / 5;
float MMBTUTilMTPAFloat = convertValue * 2;
float MMBTUTilMMScfdayFloat = convertValue * 2;
float MMBTUTilMillSm3dayFloat = convertValue * 2;
float MMBTUTilMMBTUFloat = convertValue * 2;
float MMBTUTilboeday = convertValue * 2;
float boedayTilMTPAFloat = convertValue * 3;
float boedayTilMMScfdayFloat = convertValue * 3;
float boedayTilMillSm3dayFloat = convertValue * 3;
float boedayTilMMBTUFloat = convertValue * 3;
float boedayTilboeday = convertValue * 3;
NSString *MTPATilMTPA = [[NSString alloc ] initWithFormat:
@" %f MTPA = %f MTPA", convertValue, MTPATilMTPAFloat];
NSString *MTPATilMMScfday = [[NSString alloc ] initWithFormat:
@" %f MTPA = %f MMScf/day", convertValue, MTPATilMMScfdayFloat];
NSString *MTPATilMillSm3day = [[NSString alloc] initWithFormat:
@" %f MTPA = %f Mill.SM3/day", convertValue, MTPATilMillSm3dayFloat];
NSString *MTPATilMMBTU = [[NSString alloc] initWithFormat:
@" %f MTPA = %f MMBTU", convertValue, MTPATilMMBTUFloat];
NSString *MTPATilboeday = [[NSString alloc] initWithFormat:
@" %f MTPA = %f Boe/day", convertValue, MTPATilboedayFloat];
NSString *MMScfdayTilMTPA = [[NSString alloc] initWithFormat:
@" %f MMScfday = %f MTPA", convertValue, MMScfdayTilMTPAFloat];
NSString *MMScfdayTilMMScfday = [[NSString alloc] initWithFormat:
@" %f MMScfday = %f MMScfday", convertValue, MMScfdayTilMMScfdayFloat];
NSString *MMScfdayTilMillSm3day = [[NSString alloc] initWithFormat:
@" %f MMScfday = %f MillSm3day", convertValue, MMScfdayTilMillSm3dayFloat];
NSString *MMScfdayTilMMBTU = [[NSString alloc] initWithFormat:
@" %f MMScfday = %f MMBTU", convertValue, MMScfdayTilMMBTUFloat];
NSString *MMScfdayTilboeday = [[NSString alloc] initWithFormat:
@" %f MMScfday = %f Boe/day", convertValue, MMScfdayTilboedayFloat];
NSString *MillSm3dayTilMTPA = [[NSString alloc] initWithFormat:
@" %f MillSm3day = %f MTPA", convertValue, MillSm3dayTilMTPAFloat];
if (from == 1) {
if (to == 1) {
resultLabel.text = MTPATilMTPA;
}
else if (to == 2) {
resultLabel.text = MTPATilMMScfday;
}
else if (to == 3) {
resultLabel.text = MTPATilMillSm3day;
}
else if (to == 4) {
resultLabel.text = MTPATilMMBTU;
}
else if (to == 5) {
resultLabel.text = MTPATilboeday;
}
}
else if (from == 2) {
if (to == 1) {
resultLabel.text = MMScfdayTilMTPA;
}
else if (to == 2) {
resultLabel.text = MMScfdayTilMMScfday;
}
else if (to == 3) {
resultLabel.text = MMScfdayTilMillSm3day;
}
else if (to == 4) {
resultLabel.text = MMScfdayTilMMBTU;
}
else if (to == 5) {
resultLabel.text = MMScfdayTilboeday;
}
}
}
ご覧のとおり、さらに多くのユニットが必要な場合は、さらに多くのコードが必要になります。
それに気づいたとき、すべてを変換する共通変数を使用して計算し、出力単位に変換しようとしました。長さの計算で考えてください。すべてを最初に変換したい共通変数はメートルです。したがって、1 メートル = 1、1 cm = 0.01、1 mm = 0.001 です。したがって、計算したい場合は、使用します。
unitIwantToConvertToCompanyTo1Meter = 0.5 (1 メートル) 結果 = from * to * input * unitIwantToConvertToCompanyTo1Meter.
驚くべきことに、これは機能します。ここで使用されているように:
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController;
@synthesize _convertFrom, _convertTo, _convertRates;
@synthesize inputText, picker, resultLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_convertFrom = @[@"Kubikk M", @"Kubikk CM",
@"Kubikk MM", @"MMBTU", @"Boe/day"];
_convertRates = @[ @1.0f, @0.01f, @0.001f,
@4.0f, @5.0f];
_convertTo = @[@"Kubikk M", @"Kubikk CM",
@"Kubikk MM", @"MMBTU", @"Boe/day"];
_convertRates = @[ @1.0f, @0.01f, @0.001f,
@4.0f, @5.0f];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
//dont forget to put something in here later
}
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTouched:(id)sender
{
[inputText resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return [_convertFrom count];
}
return [_convertTo count];
}
- (NSString *) pickerView: (UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == 0) {
return [_convertFrom objectAtIndex:row];
}
return [_convertTo objectAtIndex:row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
float convertFrom = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
float convertTo = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue];
float input = [inputText.text floatValue];
float to = convertTo;
float from = convertFrom;
float convertValue = input;
float kubikkfot = 0.1;
float result = from * convertValue * kubikkfot;
NSString *resultString = [[NSString alloc ] initWithFormat:
@" %f MTPA = %f MTPA", convertValue, result];
resultLabel.text = resultString;
}
これに関する問題は、 1. 私の脳は、どのように変換したかを理解しようとしてメルトダウンを起こしています 2. すべての回答にカスタムラベルを付けたいと思います。どうすればこれを実装できますか?
それで、誰かが私にもっと良い計算コードを教えてくれませんか?