wildcard
*.*
Administrator
__/--\__
    
Posts: 2370
|
 |
« Reply #15 on: April 24, 2008, 06:30:31 PM » |
|
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.
|
|
|
Logged
|
|
|
|
Frontrunner
New Member
Posts: 16
|
 |
« Reply #16 on: April 25, 2008, 07:23:32 AM » |
|
Hi Wildcard, I am glad to see you are making good progress  Cheers, Frontrunner
|
|
|
Logged
|
|
|
|
Ralph
|
 |
« Reply #17 on: April 25, 2008, 11:40:45 AM » |
|
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!
|
|
|
Logged
|
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
|
|
|
Ralph
|
 |
« Reply #18 on: April 25, 2008, 04:40:46 PM » |
|
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 : '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
|
|
|
Logged
|
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
|
|
|
Frontrunner
New Member
Posts: 16
|
 |
« Reply #19 on: April 25, 2008, 05:59:45 PM » |
|
whom I consider to be an Ace of a wildcard, ha, ha, it makew me think that Wildcard is the true "frontrunner".  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
|
|
« Last Edit: April 25, 2008, 06:01:32 PM by Frontrunner »
|
Logged
|
|
|
|
Ralph
|
 |
« Reply #20 on: April 25, 2008, 08:33:14 PM » |
|
What is a parse engine?
|
|
|
Logged
|
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
|
|
|
|
Ralph
|
 |
« Reply #22 on: April 29, 2008, 12:30:37 AM » |
|
Thank you, Frontrunner, for your link. I now have a fair idea of what a parser is.
|
|
|
Logged
|
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
|
|
|
LPG
Member

Posts: 61
semper ubi sub ubi (always wear under wear)
|
 |
« Reply #23 on: April 29, 2008, 02:57:46 AM » |
|
I tried this from QB > C. i made it do this: - put spaces between expressions and operators
- find the ^
- search left until it finds a space. if it sees a ) forget spaces until it comes to a (
- put "pow(" in
- do the same going right but add ")"
- put spaces in again
the only error is that it searches from left to right so for: (5^5)^5 it returns: (pow( 5, 5) )pow( ,5) but for something like this: 5^5 it returns: pow(5,5) it should look from the inside brackets to the outside brackets but i don't know how to do that. LPG
|
|
|
Logged
|
WHILE RPG$ <> "complete" : make up silly excuses :WEND
|
|
|
Frontrunner
New Member
Posts: 16
|
 |
« Reply #24 on: April 29, 2008, 11:49:28 AM » |
|
You are very welcome Ralph, I am glad it helped! Hi LPG, If I may give you a tip ? Use an array to split the whole input string in to tokens. This makes it easier to keep track of symbols like brackets etc. and once you know where to place the pow statement you can simply change the array. So (5^5)^5 could look like this: Token$(1) = "(" Token$(2) = "5" Token$(3) = "^" Token$(4) = "5" Token$(5) = ")" Token$(6) = "^" Token$(7) = "5" To become pow((pow(5,5)),5) Token$(1) = "pow" + Token$(1) Token$(2) = "(pow(" + Token$(2) Token$(3) = "," Token$(4) = "5" Token$(5) = ")" + Token$(5) Token$(6) = "," Token$(7) = Token$(7) + ")" Note that all tokens which are ^ simply become a comma. The trick is to take track of the brackets! I hope that helps. And then one more tricky example when it comes to unary operators  If -8 - -5 = 1 - -2 ^ 2 Then Becomes If -8 - -5= 1- -pow(2,2) Then Cheers, Frontrunner
|
|
|
Logged
|
|
|
|
wildcard
*.*
Administrator
__/--\__
    
Posts: 2370
|
 |
« Reply #25 on: April 29, 2008, 06:53:31 PM » |
|
Interesting post there Frontrunner, I've not had much time last few days to work on my entry but was stuck at the brackets stage still too anyway, will hopefully get some time to finish it soon.
|
|
|
Logged
|
|
|
|
Frontrunner
New Member
Posts: 16
|
 |
« Reply #26 on: April 29, 2008, 07:06:20 PM » |
|
Hi Wildcard,
I hope it will help you to tackle the brackets.
Have a nice day. Frontrunner
|
|
|
Logged
|
|
|
|
LPG
Member

Posts: 61
semper ubi sub ubi (always wear under wear)
|
 |
« Reply #27 on: April 30, 2008, 01:06:44 AM » |
|
Thanks Frontrunner
|
|
|
Logged
|
WHILE RPG$ <> "complete" : make up silly excuses :WEND
|
|
|
Frontrunner
New Member
Posts: 16
|
 |
« Reply #28 on: April 30, 2008, 06:27:43 AM » |
|
No problem, I hope it helped! Have a sunny day  Frontrunner
|
|
|
Logged
|
|
|
|
Frontrunner
New Member
Posts: 16
|
 |
« Reply #29 on: May 03, 2008, 11:16:27 AM » |
|
I am not sure how many of you are working on this challenge but I came along this website which might be interesting for those who use FreeBasic and want to solve this challenge. http://www.runicsoft.com/fparser.phpCheers, Frontrunner
|
|
« Last Edit: May 03, 2008, 11:31:45 AM by Frontrunner »
|
Logged
|
|
|
|
|