String parsing menu

User Tag List

Results 1 to 1 of 1
  1. #1
    Siretu's Avatar Active Member
    Reputation
    22
    Join Date
    Aug 2007
    Posts
    142
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String parsing

    What it does is, you enter an arithmetic expression and it calculates it.
    Things to think of:
    *Use spaces between everything!
    *Write it like you do with hp calculators. What this means is:

    It goes from left to right, whenever it finds an operator(like +) it takes the two closest numbers to the left and calculates them with the operator it found. It then repeats doing that until there's only one thing left and no operators.

    So, for example. 7 + 3 is 7 3 +. (2 + 3) * 4 is 4 2 3 + *. This is good because there's no need for parenthesis or priority like * is handled before +

    Yes, I was bored. I'm going to make it calculate equations later.

    Code:
    from time import sleep
    def isfunction(x):
        if x in ['+','-','*','/', '**']:
            return True
        else:
            return False
    
    def stack(string):
        stack = string.split()
        ostack = []
        for z in stack:
            if isfunction(z):
                if len(ostack) >= 2:
                    arg1 = ostack.pop(-1)
                    arg2 = ostack.pop(-1)
                    ans = solve(arg1, arg2, z)
                    ostack.append(ans)
                else:
                    print 'Error: Stack empty'
            else:
                ostack.append(z)
        print
        print 'The answer is: ', ans
        print
        print '_' * 80
    
    def solve(arg1, arg2, z):
        x = float(arg1)
        y = float(arg2)
        if z == '+':
            return x + y
        elif z == '-':
            return y - x
        elif z == '*':
            return x * y
        elif z == '/':
            return y / x
        elif z == '**':
            return y ** x
            
    def stringparse():
        print
        print 'Sting parsing testing'
        print '-' * 20
        string = raw_input("Enter string to parse: \n")
        stack(string)
    
    print
    print 'Welcome to String parsing 1.0'
    print
    loop = 1
    while loop == 1:
        sleep(0.7)
        print
        print 'Please choose an option:'
        print '1. Parse string'
        print '2. exit'
        option = input(">>>")
        if option == 1:
            stringparse()
        elif option == 2:
            loop = 0
        else:
            'Error: Invalid option'


    String parsing

Similar Threads

  1. [Help] Replace String
    By EmiloZ in forum Programming
    Replies: 3
    Last Post: 04-21-2009, 12:14 PM
  2. [How To] Wowmeteronline(combat log parsing)
    By gerathros in forum World of Warcraft Guides
    Replies: 4
    Last Post: 03-15-2009, 01:20 PM
  3. [Guide][VB.NET] Read a string from memory
    By Gothian in forum Programming
    Replies: 14
    Last Post: 08-18-2008, 04:39 PM
  4. Replies: 8
    Last Post: 07-10-2008, 02:45 PM
  5. [Guide][VB.NET] Reading a String From Memory
    By Gothian in forum WoW Memory Editing
    Replies: 14
    Last Post: 01-18-2008, 12:08 PM
All times are GMT -5. The time now is 07:39 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search