改进计算代码

时间:2012-10-14 13:50:04

标签: objective-c ios math

我正在制作一个转换器应用程序,并且在意识到我必须键入大约200行代码才能使其使用超过5个单位转换后,我应该有更好的转换计算。

我目前所拥有的是一个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;
    }
}
}

正如你所看到的,如果我想要更多的单位,那么需要更多代码的混乱。

当我意识到这一点时,我试图通过使用我将所有内容转换为的commmon变量来计算它,然后转换回输出单元。用长度计算来想一想。公共变量我想要将所有东西都转换成第一个,就是米。所以1米= 1,1米= 0.01和1毫米= 0.001。所以,如果我想计算,我会用。

unitIwantToConvertToComparedTo1Meter = 0.5(1米) result = from * to * input * unitIwantToConvertToComparedTo1Meter。

令人惊讶的是,这是有效的。 如下所用:

#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;
}

这个问题是  我的大脑正在崩溃,试图找出方法  2.我想为每个答案都有一个自定义标签,它说明了我转换的单位。我该如何实现呢?

那么,任何人都可以帮我一些更好的计算代码吗?

1 个答案:

答案 0 :(得分:2)

Dude使用C数组创建一个nxn转换矩阵。然后只有一个包含单位名称的数组。做完了。如果你不知道矩阵是什么,那么你有更多的理论需要学习。

就是这样。 C是您的转换矩阵:

       inch      meter
      +-----------------
inch  | 1        0.0254
meter | 39.3701  1

因此,如果x以英寸为单位,则C[inch][meter] * x的长度(米)相同。

C[i][j] * C[j][i] == 1。总是

您要测量的每个数量都有不同的转换矩阵。也就是说,你将有一个用于加热,一个用于时间,一个用于距离(上面的例子),一个用于能量,一个用于力......等等。祝你好运!