# Qbasicnews.com

Full Version: Mathematical expression translator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Thank you for pointing me to the confusion.
I have changed the original exampleÂ  ;D
b is indeed an array.

But you have seen it right that the challenge is all about an parsing correction!

Sorry for the confusion.

Kind regards,
Frontrunner
There is nothing as good for explaining as one or more examples!Â  Could you post at least one, good example of a math expression and the qbasic equivalent code that you would want?Â  That would help a lot!!! Frontrunner: No problem, haven't got to using it yet though still working on the simpler bracket-less parser.

Ralph: The challenge is to write a converter/parser that changes x ^ y to pow(x,y) however in the context of a more complex equation such as posted.
Thanks, Wildcard.Â  I've never tried things like this before.Â  At first thought, it would seem that the algorithm would look for each character ^, then, for each, compare the characters going backwards and consider where to stop, put a "(" there, then continue until either a "(" or any mathematical function is found; next, do the same, going forward.Â  If this is the way to do it, I can see I would flounder in all the considerations I would have to take into account!Â  It really is too much for me, and definetely, "Not my cup of tea!"
Hi All...

Ralph I see you are understanding the problem quite good and your approach is also good.

As for your reference I will give another tricky example:

Basic
a = -x ^ 3 - 3 * x ^ 2 + 4 * x - (x ^ 3 + x ^ 2 - 2) * (-1)

C
a = -pow(x,3) - 3 * pow(x,2) + 4 * x - (pow(x,3) + pow(x,2) - 2) * (-1)

Kind regards,
Frontrunner
It's proving to be a nice challenge for getting back into some coding. My parser is coming along nicely, just need to figure out how to cope with brackets and arrays.
Hi Wildcard,

I am glad to see you are making good progress 8)

Cheers,
Frontrunner
Frontrunner:

I am going to try to make the code to solve for the simple multiple occurrances of the general type. variable^power, which shouldn't be too difficult.Â

Once I succeed, I will try to go one step farther, considering an array*power.

After that, I will publish a list of all the cases I can think of, and ask for input on other cases I will have missed.

As to your good-will post to Wildcard, whom I consider to be an Ace of a wildcard, ha, ha, it makew me think that Wildcard is the true "frontrunner".Â  Sorry for the lame pun, but I just couln't resist it!
Frontrunner:

Though I'm not competing, here is my beginning code, with all my thoghts so far.Â  It seems to work fine for Example1.Â  If you approve it, I will code for Example2 and, maybe, Example3 :
Code:
```'B2C-Tran is a QuickBASIC to C translator for the C "pow" (power) function. 'I have decide to produce the necessary QuickBASIC code for the challenge as 'posted by Frontrunner at: 'http://forum.qbasicnews.com/index.php?topic=13196.0 'on an example-by-example basis, as I am not a professional programmer, just an amateur piddler. 'Example1: x ^ n 'BASIC: a = x ^ n 'Â  Â  C: a = pow(x, n) '''TO BE COVERED LATER: 'Example2: b(x) ^ n 'Â   BASIC: a = b(x) ^ n 'Â  Â  Â   C: a = pow(v(x), n) 'Example3: 'Â   BASIC: r1 = (-c + (SQR(b(x) ^ 2 - (4 * a * c)))) / (2 * a) 'Â  Â  Â   C: r1 = (-c + (sqrt(pow(b(x), 2) - (4 * a * c)))) / (2 * a) '============================================================================ 'Program specifications: 'Program must convert the BASIC Example1 to its equivalent C code. '============================================================================ 'PROGRAM DESCRIPTION: 'BASIC expresion in which the power function "r = x ^ n" is to be convert to 'its equivalent C power function, "a = pow(x, n)". 'PROGRAM DEVELOPEMENT: '1. Search the BASIC string, BAS\$, for an instance of a power expression, "^". '2. Once the "^" is found, proceed to the left, after the first " ": 'Â   a. If the next character is a not a ")", add it to the empty string, C\$. 'Â   b. Add the C power function letters, "pow", to the front of the above 'Â  Â  Â  group of characters. 'Â   c. Proceed to the right, afterÂ  the first " "; add those characters to 'Â  Â  Â  the above expression, C\$, until the next " " is found.Â  Done. '3. Compare the C\$ obtained with the correct expression contained in Ceq\$. 'Â   Once a match is obtained, print it to screen. '============================================================================ 'CODE: CLS 'for development stage, use "test = 1" test = 0 'test = 1 'Expressions: BAS\$ = "a = x ^ n" Ceq\$ = "a = pow(x, n)" 'OBTAIN STRINGS TO USE 'leftSide\$ = left side of BAS\$, including the equals sign and a " ": FOR i = 1 TO LEN(BAS\$) Â  a\$ = MID\$(BAS\$, i, 1) Â  IF a\$ <> "=" THEN Â  Â  leftSide\$ = leftSide\$ + a\$ Â  ELSE Â  Â  EXIT FOR Â  END IF NEXT i leftSide\$ = leftSide\$ + "= " 'rightSide\$ = string to the right of the equals sign: FOR i = LEN(leftSide\$) + 1 TO LEN(BAS\$) Â  rightSide\$ = rightSide\$ + MID\$(BAS\$, i, 1) NEXT i 'dist = the position of "^" in string rightSide\$ FOR i = 1 TO LEN(rightSide\$) Â  a\$ = MID\$(rightSide\$, i, 1) Â  IF a\$ = "^" THEN dist = i: EXIT FOR NEXT i 'leftChr\$ = left characters for the C function FOR i = dist - 2 TO 1 STEP -1 Â  a\$ = MID\$(rightSide\$, i, 1) Â  leftChr\$ = a\$ + leftChr\$ NEXT i leftChr\$ = "pow(" + leftChr\$ + ", " 'rightChr\$ = right characters for the C function FOR i = dist + 2 TO LEN(rightSide\$) Â  a\$ = MID\$(rightSide\$, i, 1) Â  rightChr\$ = rightChr\$ + a\$ NEXT i rightChr\$ = rightChr\$ + ")" C\$ = leftSide\$ + leftChr\$ + rightChr\$ IF C\$ = Ceq\$ THEN Â  PRINT " The C equivalent for the QB expression, " Â  PRINT "Â  Â  " + BAS\$ Â  PRINT " is "; C\$ ELSE Â  PRINT " The result generated by the program," Â  PRINT "Â  Â  "; C\$ Â  PRINT " is wrong!Â  Program must be corrected. Notify the responsible person." END IF '---------------------------------------------------------------------------- 'temporary troubleshooting code Â  IF test = 1 THEN PRINT PRINT " BAS\$ = "; CHR\$(34); BAS\$; CHR\$(34) PRINT " Ceq\$ = "; CHR\$(34); Ceq\$; CHR\$(34) PRINT PRINT " left side = "; CHR\$(34); leftSide\$; CHR\$(34) PRINT "right side = "; CHR\$(34); rightSide\$; CHR\$(34) PRINT "distance to ^ in rightSide\$ ="; dist PRINT " leftChr\$ = "; CHR\$(34) + leftChr\$; CHR\$(34) PRINT "rightChr\$ = "; CHR\$(34) + rightChr\$; CHR\$(34) Â  END IF '---------------------------------------------------------------------------- GOSUB pause SYSTEM '============================================================================ 'SUBROUTINES: pause: WHILE INKEY\$ = "": WEND RETURN```
whom I consider to be an Ace of a wildcard, ha, ha, it makew me think that Wildcard is the true "frontrunner".Â  ;D
Lol Ralph!

Thanks for your contribution so far.
The way you are tackling the problem is interesting but you have to consider that the conversion should work in all situations and not only with examples where assignments are included.

So you really need a parse engine to make this work.

But it is good to see you are doing your best!

Cheers,
Frontrunner
Pages: 1 2 3 4