Qbasicnews.com
September 20, 2020, 01:50:20 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: n00b Help  (Read 4059 times)
nkk_kan
Member
*
Posts: 73


« on: June 27, 2006, 01:47:12 PM »

Hi
i'm nkk

i am making a program called "mathemagic" in pure Qbasic
and i wanted to know how to make a console...

what i mean it this..

if user types "1+1" then it should give 2
i wanted to know different possibilities for this....


nkk
Logged

\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
Dio
I hold this place together
*****
Posts: 874



« Reply #1 on: June 27, 2006, 03:18:01 PM »

jeez dude, don't go into to much detail, each character is one byte and they add up you know. i don't think sumo wants to go over his bandwidth because you decided to type up the encylopaedia.

ok, sorry, i just woke up.
what are you talking about? please use details. unless i'm missing something here.
Logged

quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Anonymous
Guest
« Reply #2 on: June 27, 2006, 05:19:33 PM »

well, you need to learn how to "parse expressions".

Deleter posted a nice recursive descent expression parser a while ago... maybe he will show you when he sees this topic. =)
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #3 on: June 27, 2006, 08:29:29 PM »

Basically you need to convert the string into individual numbers: Use MID$() and ASC() to get the ASCII value of each character and then create IF statements that figure out what to do. I agree, Deleter's parser is an awesome example.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #4 on: June 27, 2006, 08:44:43 PM »

red_Marvin's Equator program is my preference (although I've never tried Deleter's).
http://fileanchor.com/17037-d
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Deleter
Na_th_an
*****
Posts: 1293



WWW
« Reply #5 on: June 27, 2006, 09:15:38 PM »

http://forum.qbasicnews.com/viewtopic.php?t=11145

though recursive stuff can be hard for a newbie to learn...

edit: just realized that uses string indexing, which is not supported in qb, if you want me to rewrite it quick so it works for qb then say so.
Logged

nkk_kan
Member
*
Posts: 73


« Reply #6 on: June 28, 2006, 05:53:01 AM »

well yeah.. if you could spare anytime i would like you to translate it in qb
but i think this will help and even save u from trouble..
i could understand the parser by going through it.....
so thanx for the prompt help =P
i think i will make convert it in qb

keep rocking!

nkk
Logged

\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
nkk_kan
Member
*
Posts: 73


« Reply #7 on: June 28, 2006, 06:25:41 AM »

well and sorry to post this here
 i should have posted it in Qbasic general Help..


nkk
Logged

\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
Anonymous
Guest
« Reply #8 on: June 28, 2006, 08:19:41 AM »

Don't worry about it, after all it was your first post...


btw, welcome  :^_^:
Logged
nkk_kan
Member
*
Posts: 73


« Reply #9 on: June 28, 2006, 12:39:28 PM »

thank you chaos..
i used qbasic 4.5 for about 1 year
and i learned lot of it..
but what i lacked was a hard disk..Sad(i don't have one..blownup mine)
Logged

\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
Deleter
Na_th_an
*****
Posts: 1293



WWW
« Reply #10 on: June 28, 2006, 02:28:22 PM »

here ya go...tested with qb4.5

Code:
DECLARE FUNCTION ParseEq# (eq AS STRING)

 
DIM eq AS STRING

eq = "(10  ^(2*2))/20+(  90*4)*10+ (1+9)*88  +244  /866-  46^  2-1"
PRINT (10 ^ (2 * 2)) / 20 + (90 * 4) * 10 + (1 + 9) * 88 + 244 / 866 - 46 ^ 2 - 1
PRINT ParseEq#(eq)

SLEEP

FUNCTION ParseEq# (eq AS STRING)
    'holds the lowest math function found in the equation
    '+or- 1=addition/subtraction   2=multiplication/division   3=power
    DIM curLow AS INTEGER
    curLow = 4
   
    'location of the lowest math function in the string
    DIM curInd AS INTEGER
   
    'how many math operations are found in the equation
    DIM numOfOps AS INTEGER
    numOfOps = 0
   
    'number of parentheses
    DIM numOfPar AS INTEGER
    numOfPar = 1
   
    'filter spaces
    FOR c = 1 TO LEN(eq)
        IF MID$(eq, c, 1) = " " THEN eq = LEFT$(eq, c - 1) + RIGHT$(eq, LEN(eq) - c)
    NEXT c
   
   
    'get rid of parantheses if they include the whole equation 'ex (234+434*(23+1)) ->  234+434*(23+1)
    IF LEFT$(eq, 1) = "(" AND RIGHT$(eq, 1) = ")" THEN
        FOR c = 2 TO LEN(eq)
           
            'if we find close or open parantheses, add or subract to the current open number
            IF MID$(eq, c, 1) = ")" OR MID$(eq, c, 1) = "(" THEN numOfPar = numOfPar + (40 - ASC(MID$(eq, c, 1))) * 2 + 1
           
            'if there are 0 parantheses open, then the opening and closing parantheses
            'do not enclose the whole equation so we cannot remove them
            IF numOfPar = 0 AND c <> LEN(eq) THEN EXIT FOR
           
            'the opening and closing parantheses enclose the entire thing
            'so we can safely remove them
            IF numOfPar = 0 AND c = LEN(eq) THEN eq = MID$(eq, 2, LEN(eq) - 2)
           
        NEXT
    END IF
   
    'find the lowest operator
    FOR c = 1 TO LEN(eq)
     
        'skip stuff inside parantheses in the operation count
        IF MID$(eq, c, 1) = "(" THEN
            numOfPar = 1
           
            FOR tempc = c + 1 TO LEN(eq)
                IF MID$(eq, tempc, 1) = ")" OR MID$(eq, tempc, 1) = "(" THEN numOfPar = numOfPar + (40 - ASC(MID$(eq, tempc, 1))) * 2 + 1
                c = tempc + 1
                IF numOfPar = 0 THEN EXIT FOR
            NEXT
           
            IF c = LEN(eq) + 1 THEN EXIT FOR'equation end has been reached        
        END IF
       
        IF MID$(eq, c, 1) = "+" OR MID$(eq, c, 1) = "-" OR MID$(eq, c, 1) = "*" OR MID$(eq, c, 1) = "/" OR MID$(eq, c, 1) = "^" THEN numOfOps = numOfOps + 1
        IF MID$(eq, c, 1) = "+" OR MID$(eq, c, 1) = "-" THEN '+,-
           
            'make it either +1 or -1, addition or subtraction
            curLow = 44 - ASC(MID$(eq, c, 1))
            curInd = c
           
        ELSEIF MID$(eq, c, 1) = "*" OR MID$(eq, c, 1) = "/" THEN '*,/
            IF ABS(curLow) > 1 THEN
               
                'make it either +2 or -2, mutliplication or division
                curLow = -((ASC(MID$(eq, c, 1)) - 42) / 5 * 4 - 2)
                curInd = c
            END IF
        ELSEIF MID$(eq, c, 1) = "^" THEN '^
            IF ABS(curLow) > 2 THEN
               
                'make it +3 for power function
                curLow = 3
                curInd = c
               
            END IF
        END IF
    NEXT
   
    'single number, no equation to evaluate!
    IF numOfOps = 0 THEN ParseEq# = VAL(eq)

   
    'addition or subtraction
    IF ABS(curLow) = 1 THEN ParseEq# = ParseEq(LEFT$(eq, curInd - 1)) + ParseEq(RIGHT$(eq, LEN(eq) - curInd)) * curLow'split formula into two formulas, the split being at the lowest operation
    'by multiplying the second clause by the number, if it is negative, it is converted to subtraction
    'ie) 1+1 * -1 = 1-1
   
    'multiplication or division
    IF ABS(curLow) = 2 THEN ParseEq# = ParseEq(LEFT$(eq, curInd - 1)) * ParseEq(RIGHT$(eq, LEN(eq) - curInd)) ^ (curLow / 2)
    'by taking the second clause to the number, if it is negative, it is converted to division
    'ie 2* 2^-1 = 2 * 1/2 = 2/2
   
    'power function
    IF curLow = 3 THEN ParseEq# = ParseEq(LEFT$(eq, curInd - 1)) ^ ParseEq(RIGHT$(eq, LEN(eq) - curInd))
   

END FUNCTION


man its been a *long* time since ive used qb..... Smiley
Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #11 on: June 28, 2006, 11:41:15 PM »

Quote from: "Deleter"

......
man its been a *long* time since ive used qb..... Smiley

From the performance of your expression evaluator, it looks like QB for you is like riding a bicycle --- you never forget. :wink:

I did a few quick tests with different expressons. Congratulations on a nice piece of work.
*****
Logged
Anonymous
Guest
« Reply #12 on: June 28, 2006, 11:53:58 PM »

Quote from: "Moneo"
Quote from: "Deleter"

......
man its been a *long* time since ive used qb..... Smiley

From the performance of your expression evaluator, it looks like QB for you is like riding a bicycle --- you never forget. :wink:

I did a few quick tests with different expressons. Congratulations on a nice piece of work.
*****


don't let him fool you - he had to ask the master :king:


....XD


 :^_^:
Logged
Deleter
Na_th_an
*****
Posts: 1293



WWW
« Reply #13 on: June 29, 2006, 01:10:22 AM »

Quote from: "Moneo"
Quote from: "Deleter"

......
man its been a *long* time since ive used qb..... Smiley

From the performance of your expression evaluator, it looks like QB for you is like riding a bicycle --- you never forget. :wink:

I did a few quick tests with different expressons. Congratulations on a nice piece of work.
*****

Well, it was coded in fb, which I am pretty good at, the hard part was converting it, which as cha0s said wasn't entirely my own doing. Still it was interesting and helped remind me what I like so much about freebasic.
Logged

nkk_kan
Member
*
Posts: 73


« Reply #14 on: June 29, 2006, 06:43:35 AM »

Well thanx for that code
actually i was doing the conversion when i read your post
and i liked urs..
it was better than mine...=P

thanx for it
i hope i could program like u,.,=P
Logged

\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!