Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mathematical expression translator
#19
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.
Reply


Messages In This Thread
Re: Mathematical expression translator - by Ralph - 04-23-2008, 05:40 AM
Re: Mathematical expression translator - by Ralph - 04-23-2008, 11:09 PM
Re: Mathematical expression translator - by Ralph - 04-24-2008, 02:56 AM
Re: Mathematical expression translator - by Ralph - 04-24-2008, 07:51 AM
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 Ralph - 04-26-2008, 04:33 AM
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 LPG - 04-30-2008, 09:06 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)