开发者

Is my math over complicated, it works

I've made a function to perform a calculation against four fields. Then outputs the that calculation to a receiver field. The math is fairly simply in concept. The receiver field is the remainder of the lowest int from the next lowest int. For instance the app reads values 2, 5 , 6, 8 from user input. Receiver field should equal(3)

I have implemented NSSet to remove any duplicates than re-stack the set into an array for ease of reading and the values and custom control.

The math section of the function seems like over kill. Is there a more elegant way to perform this other than what I have built? It works it's just ugly and probably not the correct path.

Any help is appreciated.

// set of strings populated from a sibling View so i used a singleton to sync.
NSArray *myArray = [[NSArray alloc] initWithObjects:[StringLinker sharedManager].singcol1Row1String,
                                                    [StringLinker sharedManager].singcol1Row2String,
                                                    [StringLinker sharedManager].singcol1Row3String,
                                                    [StringLinker sharedManager].singcol1Row4String,nil];
NSSet *set = [NSSet setWithArray:myArray];
NSLog(@"mySet count: %d", [set count]);

NSMutableArray *newArray = [NSMutableArray arrayWithArray:[set allObjects]];
// define locals 
int i;
int tempV;
id *objects;
int  v1Col1=0;
int  v2Col1=0;
int  v3Col1=0;
int  v4Col1=0;
// create helpers
NSUInteger count = [newArray count];
objects = malloc(sizeof(id) * count);
[newArray getObjects:objects];
// check the correct id and location
for (i = 0; i < [newArray count]; i++) {
    tempV = [[newArray objectAtIndex:i]intValue];
    NSLog(@"current objectAtIndex = %i id = %i",i,tempV);
}
NSLog(@"count of new Array = %d",count);
// assign our controlled items. and make string into a Value
if (count >= 1) { v1Col1 = [[newArray objectAtIndex:0]intValue];}
if (count >= 2) { v2Col1 = [[newArray objectAtIndex:1]intValue];}
if (count >= 3) { v3Col1 = [[newArray objectAtIndex:2]intValue];}
if (count >= 4) { v4Col1 = [[newArray objectAtIndex:3]intValue];}

// then do raw calculations
if ( count ==2) {
    if (v1Col1 < v2Col1) { _master1PinsCol1 = v2Col1 - v1Col1;}
    if (v2Col1 < v1Col1) { _master1PinsCol1 = v1Col1 - v2Col1;}
}
if ( count ==3) {
    if ((v1Col1 < v2Col1) && (v2Col1 < v3Col1)) { _master1PinsCol1 = v2Col1 - v1Col1;}
    if ((v1Col1 < v2Col1) && (v3Col1 > v2Col1)) { _master1PinsCol1 = v2Col1 - v1Col1;}
    if ((v1Col1 < v2Col1) && (v3Col1 < v2Col1)) { _master1PinsCol1 = v3Col1 - v1Col1;}
    if ((v2Col1 < v1Col1) && (v1Col1 < v3Col1)) { _master1PinsCol1 = v1Col1 - v2Col1;}
    if ((v2Col1 < v3Col1) && (v3Col1 < v1Col1)) { _master1PinsCol1 = v3Col1 - v2Col1;}
}
if (count >=4){
    if ((v1Col1 < v2Col1) && (v1Col1 < v3Col1) && (v1Col1 < v4Col1)) { 
        if ((v2Col1 < v3Col1) && (v2Col1 < v4Col1)) {_master1PinsCol1 = v2Col1-v1Col1;}
        if ((v3Col1 < v2Col1) && (v3Col1 < v4Col1)) {_master1PinsCol1 = v3Col1-v1Col1;}
        if ((v4Col1 < v2Col1) && (v4Col1 < v3Col1)) {_master1PinsCol1 = v4Col1-v1Col1;}
    }
    if ((v2Col1 < v1Col1) && (v2Col1 < v3Col1) && (v2Col1 < v4Col1)) {
        if ((v1Col1 < v3Col1) && (v1Col1 < v4Col1)) {_master1PinsCol1 = v1Col1-v2Col1;}
        if ((v3Col1 < v1Col1) && (v3Col1 < v4Col1)) {_master1PinsCol1 = v3Col1-v2Col1;}
        if ((v4Col1 < v1Col1) && (v4Col1 < v3Col1)) {_master1Pin开发者_Python百科sCol1 = v4Col1-v2Col1;}
    }
    if ((v3Col1 < v1Col1) && (v3Col1 < v2Col1) && (v3Col1 < v4Col1)) {
        if ((v1Col1 < v2Col1) && (v1Col1 < v4Col1)) {_master1PinsCol1 = v1Col1-v3Col1;}
        if ((v2Col1 < v1Col1) && (v2Col1 < v4Col1)) {_master1PinsCol1 = v2Col1-v3Col1;}
        if ((v4Col1 < v1Col1) && (v4Col1 < v2Col1)) {_master1PinsCol1 = v4Col1-v3Col1;}
    }
    if ((v4Col1 < v1Col1) && (v4Col1 < v2Col1) && (v4Col1 < v3Col1)) { 
        if ((v1Col1 < v2Col1) && (v1Col1 < v3Col1)) {_master1PinsCol1 = v1Col1-v4Col1;}
        if ((v2Col1 < v1Col1) && (v2Col1 < v3Col1)) {_master1PinsCol1 = v2Col1-v4Col1;}
        if ((v3Col1 < v1Col1) && (v3Col1 < v2Col1)) {_master1PinsCol1 = v3Col1-v4Col1;}
    }
}


NSLog(@"***********************************Col1 Master1 = %.0f",_master1PinsCol1);


Your code has a lot of potential in saving method calls and objects. Also, it seems you were leaking memory: you malloc'd but never free'd, and your myArray wasn't released. Here's how I would do it:

// Get instance once, save three method calls.
StringLinker *linker = [StringLinker sharedManager];
// Initialize set with the objects. Convert them to NSNumbers, we need
// to have the numbers parsed later on anyway. Doing it now makes
// sorting easier.
NSSet *set = [NSSet setWithObjects:
    [NSNumber numberWithInt:[linker.singcol1Row1String intValue]],
    [NSNumber numberWithInt:[linker.singcol1Row2String intValue]],
    [NSNumber numberWithInt:[linker.singcol1Row3String intValue]],
    [NSNumber numberWithInt:[linker.singcol1Row4String intValue]],
    nil
];

// Sanity check.
if ([set count] < 2) {
    // Error handling goes here. For example, if there's only one
    // element, can that be used as value for _master1PinsCol1 ?
    return;
}

// Get mutable array of distinct objects...
NSMutableArray *array = [[set allObjects] mutableCopy];
// ... and sort it.
[array sortUsingSelector:@selector(compare:)];

// Calculate the value by subtracting the smaller number from the
// bigger number.
_master1PinsCol1 = [[array objectAtIndex:1] intValue]
                   - [[array objectAtIndex:0] intVale];

// Release the mutable copy.
[array release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜