[xcodeitalia] Re: Parser 0.2

  • From: Giovanni Lanzani <ulissesroc@xxxxxxxxx>
  • To: xcodeitalia@xxxxxxxxxxxxx
  • Date: Mon, 17 Mar 2008 19:17:26 +0100

It's just an idea, but why not having a graphical panel with which the user can insert the functions? So that they would always be correct, and we would also have the possibility to use any function present in the cocoa mathematical libraries. I don't know how to implement it, but that would simplify everything (and not restrict ourselves to polynomial expression.


Giovanni


On 17/mar/08, at 18:27, Claudio Mezzasalma wrote:

Ho trovato un bug nel caso l'espressione iniziasse con una x senza coefficiente, risolto. La modifica inserita per risolvere questo, comunque, dovrebbe farci riflettere sull'inadeguatezza dell'NSScanner per fare cose così.

I found a bug when the expression starts with an x without a coefficient. However, the fix introduced should make us think about the inadequacy of NSScanner to make such things.

@implementation functionParser
- (IBAction)parseFunction:(id)sender; {
        NSMutableArray *factorsArray = [NSMutableArray array];
        NSMutableArray *functionArray = [NSMutableArray array];
        int exponent;
        float coefficient;
        NSLog (@"%@",[functionTextField stringValue]);
NSString *functionString = [NSString stringWithString: [functionTextField stringValue]]; NSScanner *functionScanner = [NSScanner scannerWithString:functionString];
        [functionScanner setScanLocation: 2];
        while ([functionScanner isAtEnd] == NO)
        {
                if ([functionScanner scanFloat: &coefficient]) {
                        if([functionScanner scanString: @"x^" intoString:NULL]) 
{
                                [functionScanner scanInt: &exponent];
                        }
                        else if ([functionScanner scanString: @"x" 
intoString:NULL]) {
                                exponent = 1;
                        }
                        else {
                                exponent = 0;
                        }
[factorsArray insertObject: [NSNumber numberWithFloat:coefficient] atIndex: 0]; [factorsArray insertObject: [NSNumber numberWithFloat:exponent] atIndex: 1];
                [functionArray addObject: factorsArray];
                }
                else {
                        coefficient = 1;
                        if([functionScanner scanString: @"x^" intoString:NULL]) 
{
                                [functionScanner scanInt: &exponent];
                        }
                        else if ([functionScanner scanString: @"x" 
intoString:NULL]) {
                                exponent = 1;
                        }
                        else {
                                exponent = 0;
                        }
[factorsArray insertObject: [NSNumber numberWithFloat:coefficient] atIndex: 0]; [factorsArray insertObject: [NSNumber numberWithFloat:exponent] atIndex: 1];
                        [functionArray addObject: factorsArray];
                }
        }
        //return functionArray;

Other related posts: