如果声明比较保持说4大于5

时间:2014-05-02 18:40:27

标签: objective-c

我有一个0-49的阵列。当我比较acc_x [i]> acc_x [i-1],在比较5和4之前它会起作用,然后它说4大于5并进入else语句。请帮忙。

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        // insert code here...
        //NSLog(@"Hello, World!");

        //use velocity not acceleration. sorry for the naming. so run the velocity function for the array first that I wrote a already
        NSMutableArray * acc_x = [NSMutableArray array];


        NSNumber * temp = 0;
        //the highest point or lowest point
        NSNumber *highest =0;
        NSNumber *lowest = 0;
        int flag = 0;

        //array for the highest and lowest point
        NSMutableArray * array_lowest = [NSMutableArray array];
        NSMutableArray * array_highest = [NSMutableArray array];
        //array for the time when the highest and the lowest point
        NSMutableArray * time_lowest = [NSMutableArray array];
        NSMutableArray * time_highest = [NSMutableArray array];


        double temp1 = 0;
        NSNumber *temp2 = 0;
        // the time variable is is just for temp variable. the real variable will be how long it take to have one measurement. i think it was like .001 or something like that but i don't remember. the time have to be in second if it is not in second the conver it.
        double time = 0.1;
        //trying to find the highest point or the lowest points in the graph from the acceleration
        for (int i=0; i<50; i++)
        {
            //putting 0-49 into the array for testing
            temp = [NSDecimalNumber numberWithDouble:i];
            [acc_x addObject:temp];

            if(i == 2) {
                if (acc_x[i] > acc_x[i-1]) {
                    flag = 0;
                }
                if(acc_x[i] < acc_x[i-1]){
                    flag = 1;
                }
                NSLog(@"flag = %d",flag);
            }

            if(i>1) {
                if(acc_x[i] > acc_x[i-1]) {
                    NSLog(@"x now is bigger then x past");
                }
            }

            if(i >1) {
                if(acc_x[i] > acc_x[i-1]) {
                    NSLog(@"x now is bigger then x pass");
                }
                 NSLog(@"i = %d , i-1 = %d",i, i-1);
                if (flag == 0) {
                NSLog(@"flag is 0");
                if(acc_x[i] > acc_x[i-1]) {
                    highest = acc_x[i];
                }
                else {
                    NSLog(@"flag going to turn into 1");
                    [array_highest addObject:highest];
                    flag = 1;
                    // calculate the time when the highest point is
                    temp1 = time * i;
                    temp2 = [NSNumber numberWithDouble:temp1];
                    [time_highest addObject:temp2];
                }
            }
            if (flag ==1) {
                NSLog(@"flag is 1");
            }
            }

        }


        // the size of the array
      /*  long size = [acc_x count];
       for (int i =1; i<size-1; i++) {
            NSLog(@"i = %d, flag = %d, array = %@, array[i-1] = %@",i,flag,acc_x[i],acc_x[i-1]);
            if (flag == 1) {
                if (acc_x[i] < acc_x[i-1]) {
                    lowest = acc_x[i];
                }
                if (acc_x[i] > acc_x[i-1]) {
                    flag = 0;
                    [array_lowest addObject:lowest];
                    // the temp1 is storing the time when this point got recorded
                    temp1 = time * i;
                    temp2 = [NSNumber numberWithDouble:temp1];
                    [time_lowest addObject:temp2];
                }
            }
            if (flag == 0) {
                if (acc_x[i] > acc_x[i-1]) {
                    highest = acc_x[i];
                    NSLog(@"x now is bigger than x-1");
                    //NSLog("highest = %d", highest);
                }
                if (acc_x[i] < acc_x[i-1]) {
                    NSLog(@"x now is less than x-1");
                    flag = 1;
                    [array_highest addObject:highest];
                    // the temp1 is storing the time when this point got recorded
                    temp1 = time * i;
                    temp2 = [NSNumber numberWithDouble:temp1];
                    [time_highest addObject:temp2];
                }
            }
        }*/

        //finding the period: time for 1 oscillation in second (remember that it is in second VERY IMPORTANT)


    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您正在比较对象(NSNumber)而不是它们的数值。

执行:if ([acc_x[i] intValue] > [acc_x[i-1] intValue])

而不是if (acc_x[i] > acc_x[i-1])