patternMinor
Improving calculation code
Viewed 0 times
codecalculationimproving
Problem
I'm making a converter app, and after realising I would have to type about 200 lines of code to get it working with more than 5 units converted, I should have a better conversion calculation.
What I have currently is a ifelse that find out what units i selected from the wheel, a sting to find out what it should answer and a float to calculate. It looks like this atm:
```
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController;
@synthesize _convertFrom, _convertTo, _convertRates;
@synthesize inputText, picker, resultLabel;
{
[super viewDidLoad];
{
[super viewDidLoad];
_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];
}
-(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];
}
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:[pickerV
What I have currently is a ifelse that find out what units i selected from the wheel, a sting to find out what it should answer and a float to calculate. It looks like this atm:
```
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController;
@synthesize _convertFrom, _convertTo, _convertRates;
@synthesize inputText, picker, resultLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
{
[super viewDidLoad];
_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];
}
-(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:[pickerV
Solution
Copied from SO with a few edits and additions.
Dude use C arrays to create an nxn matrix of conversions. Then just have one array with unit names in it. Done and done. If you don't know what a matrix is, then you have more theory to learn.
It goes like this.
So if
And
You will have a different conversion matrix for each quantity that you are measuring. That is, you will have one for heat, one for time, one for distance (the example above), one for energy, one for force... etc.
Then each measure is just an index value:
And you can name them like so:
And you might want to have images for each:
And now you have the foundation for building up your GUI and architecture. More than this I won't answer - unless you pay me!
Good luck!
Dude use C arrays to create an nxn matrix of conversions. Then just have one array with unit names in it. Done and done. If you don't know what a matrix is, then you have more theory to learn.
It goes like this.
M is your conversion matrix:M: inch meter
+-----------------
inch | 1 0.0254
meter | 39.3701 1So if
x is in inches then M[inch][meter] * x is the same length in meters.And
M[i][j] * M[j][i] == 1. Always.You will have a different conversion matrix for each quantity that you are measuring. That is, you will have one for heat, one for time, one for distance (the example above), one for energy, one for force... etc.
Then each measure is just an index value:
enum { inch = 0, meter = 1 };
typedef unsigned int MeasureType;And you can name them like so:
NSArray *measureNames = [NSArray arrayWithObjects:@"Inches", @"Meters", nil];And you might want to have images for each:
UIImage *inchesImage = [UIImage imageNamed:@"inches_icon"];
UIImage *metersImage = [UIImage imageNamed:@"meters_icon"];
NSArray *measureImages = [NSArray arrayWithObjects:inchesImage, metersImage, nil];And now you have the foundation for building up your GUI and architecture. More than this I won't answer - unless you pay me!
Good luck!
Code Snippets
M: inch meter
+-----------------
inch | 1 0.0254
meter | 39.3701 1enum { inch = 0, meter = 1 };
typedef unsigned int MeasureType;NSArray *measureNames = [NSArray arrayWithObjects:@"Inches", @"Meters", nil];UIImage *inchesImage = [UIImage imageNamed:@"inches_icon"];
UIImage *metersImage = [UIImage imageNamed:@"meters_icon"];
NSArray *measureImages = [NSArray arrayWithObjects:inchesImage, metersImage, nil];Context
StackExchange Code Review Q#17575, answer score: 3
Revisions (0)
No revisions yet.