• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Mathematical expression translator Ralph Senior Member Posts: 544 Threads: 27 Joined: Jan 2005 04-26-2008, 12:40 AM 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``` Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer. « Next Oldest | Next Newest »

 Messages In This Thread Mathematical expression translator - by Frontrunner - 04-21-2008, 06:15 PM Re: Mathematical expression translator - by wildcard - 04-21-2008, 11:48 PM Re: Mathematical expression translator - by Frontrunner - 04-22-2008, 12:08 AM Re: Mathematical expression translator - by wildcard - 04-22-2008, 12:17 PM Re: Mathematical expression translator - by Frontrunner - 04-22-2008, 02:57 PM Re: Mathematical expression translator - by Ralph - 04-23-2008, 05:40 AM Re: Mathematical expression translator - by Frontrunner - 04-23-2008, 11:54 AM Re: Mathematical expression translator - by Ralph - 04-23-2008, 11:09 PM Re: Mathematical expression translator - by Frontrunner - 04-24-2008, 01:25 AM Re: Mathematical expression translator - by wildcard - 04-24-2008, 02:06 AM Re: Mathematical expression translator - by Frontrunner - 04-24-2008, 02:52 AM Re: Mathematical expression translator - by Ralph - 04-24-2008, 02:56 AM Re: Mathematical expression translator - by wildcard - 04-24-2008, 03:19 AM Re: Mathematical expression translator - by Ralph - 04-24-2008, 07:51 AM Re: Mathematical expression translator - by Frontrunner - 04-25-2008, 01:39 AM Re: Mathematical expression translator - by wildcard - 04-25-2008, 02:30 AM Re: Mathematical expression translator - by Frontrunner - 04-25-2008, 03:23 PM Re: Mathematical expression translator - by Ralph - 04-25-2008, 07:40 PM Re: Mathematical expression translator - by Ralph - 04-26-2008, 12:40 AM Re: Mathematical expression translator - by Frontrunner - 04-26-2008, 01:59 AM Re: Mathematical expression translator - by Ralph - 04-26-2008, 04:33 AM Re: Mathematical expression translator - by Frontrunner - 04-26-2008, 01:48 PM Re: Mathematical expression translator - by Ralph - 04-29-2008, 08:30 AM Re: Mathematical expression translator - by LPG - 04-29-2008, 10:57 AM Re: Mathematical expression translator - by Frontrunner - 04-29-2008, 07:49 PM Re: Mathematical expression translator - by wildcard - 04-30-2008, 02:53 AM Re: Mathematical expression translator - by Frontrunner - 04-30-2008, 03:06 AM Re: Mathematical expression translator - by LPG - 04-30-2008, 09:06 AM Re: Mathematical expression translator - by Frontrunner - 04-30-2008, 02:27 PM Re: Mathematical expression translator - by Frontrunner - 05-03-2008, 07:16 PM Re: Mathematical expression translator - by yetifoot - 05-07-2008, 07:37 PM Re: Mathematical expression translator - by Frontrunner - 05-08-2008, 03:23 AM Re: Mathematical expression translator - by wildcard - 05-13-2008, 06:26 PM Re: Mathematical expression translator - by Frontrunner - 06-02-2008, 02:23 AM Re: Mathematical expression translator - by wildcard - 06-04-2008, 12:29 PM Re: Mathematical expression translator - by Agamemnus - 08-06-2008, 01:17 AM

Forum Jump:

Users browsing this thread: 1 Guest(s)