Question

In my code, I'm dealing with an NSString that contains an NSNumber value. This NSNumber value could possibly be a repeating decimal number (e.x. 2.333333333e+06) that shortens to "2.333333" in a string format. It could also be a terminating number (e.x. 2.5), negative, or irrational number (2.398571892858...) (only dealing with decimals here)

I need to have a way to figure out if there are the repeating numbers in the string (or the NSNumber, if necessary). In my code, I would have no way to know what the repeating number would be, as it's a result of computations started by the user. I have tried this for loop (see below) that doesn't work the way I want it to, due to my inexperience with string indexing/ranges/lengths.

BOOL repeat = NO; //bool to check if repeating #
double repNum, tempNum; //run in for loop
NSString *repeating = [numVal stringValue]; //string that holds possible repeating #
for (int i = 3; i <= [repeating length]-3; i++) { //not sure about index/length here
            if (i == 3) {
                  repNum = [repeating characterAtIndex:i];
                    }
            tempNum = [repeating characterAtIndex:i];
            if (tempNum == repNum) {
                     repeat = YES;
                      } else {
                        repeat = NO;
                    }

                }

This code doesn't work as I'd like it to, mainly because I also have to account for negative dashes in the string and different amounts of numbers (13 1/3 vs. 1 1/3). I've used the modffunction to separate the integers from the decimals, but that hasn't worked well for me either.

Thank you in advance. Please let me know if I can clarify anything.

EDIT:

This code works with the finding of different solutions for polynomials (quadratic formula). Hope this helps put it into context. See here. (Example input)

NSNumber *firstPlusSolution, *secondMinusSolution;
NSString *pValueStr, *mValueStr;

firstPlusSolution = -(b) + sqrt(square(b) - (4)*(a)*(c)); //a, b, c: "user" provided
firstPlusSolution /= 2*(a);

secondMinusSolution = -(b) - sqrt(square(b) - 4*(a)*(c));
secondMinusSolution /= 2*(a);

pValueStr = [firstPlusSolution stringValue];
mValueStr = [secondMinusSolution stringValue];

if ([NSString doesString:pValueStr containCharacter:'.']) { //category method I implemented

    double fractionPart, integerPart;
    fractionPart = modf(firstPlusSolution, &integerPart);

    NSString *repeating = [NSString stringWithFormat:@"%g", fractionPart];
    int repNum, tempNum;
    BOOL repeat = NO;

   //do for loop and check for negatives, integers, etc.
}

if ([NSString doesString:mValueStr containCharacter'.']) { 
   //do above code
   //do for loop and check again    
}
Was it helpful?

Solution

Use C. Take the fractional part. Convert to a string with a known accuracy. If length of string indicates that last digits are missing, then it does not repeat. Use NSString-UTF8String to convert a string. Get rid of the last digit (may be rounding or actual floating point arithmetic error). Use function int strncmp ( const char * str1, const char * str2, size_t num ) to perform comparison within the string itself. If the result is 8 characters long and the last 2 characters match the first 2 characters, then shall the first 6 characters be considered repeating?

Assuming that fraction knowledge your desire:

• Possibility 1: Use fractions. Input fractions. Compute with fractions. Output fractions. Expand upon one of the many examples of a c++ fraction class if necessary and use it.

• Possibility 2: Choose an accuracy which is much less than double. Make a fraction from the result. Reduce the fraction allowing rounding based upon accuracy.

OTHER TIPS

I suggest use not optimal but easy to write solution

Create NSMutableDictionary that will contain number as key and count of occurrence as value.

You can use componentsSeparatedByString: if numbers in string delimited by known symbol

In loop check valueForKey in dictionary and if need increase value

Last step is analyzing our dictionary and do anything you need with numbers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top