LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-04-2012, 07:34 PM   #1
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Rep: Reputation: Disabled
error in reading lines..


i am converting infix to postfix.. but when i try to read expressions from file (line by line). its just giving me not what i want.

Code:
 
postfix = []
temp = []
operator = -1
operand = -2
leftparent = -3
rightparent = -4
empty = -5

def precedence(s):
        if s is '(':
                return 0
        elif s is '+' or '-':
                return 1
        elif s is '*' or '/' or '%':
                return 2
        else:
                return 99

def typeof(s):
        if s is '(':
                return leftparent
        elif s is ')':
                return rightparent
        elif s is '+' or s is '-' or s is '*' or s is '%'or s is '/':
                return operator
        elif s is ' ':
                return empty
        else :
                return operand

lines = [line.strip() for line in open('tem')]

for i in line :
        type = typeof(i)
        if type is leftparent :
                temp.append(i)
        elif type is rightparent :
                next = temp.pop()
                while next is not '(':
                        postfix.append(next)
                        next = temp.pop()
        elif type is operand:
                postfix.append(i)
        elif type is operator:
                p = precedence(i)
                while len(temp) is not 0 and p <= precedence(temp[-1]) :
                        postfix.append(temp.pop())
                temp.append(i)
        elif type is empty:
                continue

while len(temp)>0:
   postfix.append(temp.pop())

print "It's postfix notation is ",' '.join(postfix)

Last edited by NickPats; 11-04-2012 at 07:56 PM.
 
Old 11-04-2012, 07:53 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
1. which language is this?
2. when posting, use square brackets [ ] to mark code, not < > https://www.linuxquestions.org/quest...do=bbcode#code
3. please show an example of input and output, also desired output
 
Old 11-04-2012, 08:01 PM   #3
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
its in python ...
tried 13 + 23 - 42 * 2

gives me 1 3 2 3 + 4 2 - 2
* (gives me * operator in new line and space between digits)
wants : 13 23 + 42 - 2 *

there is input file which has this samples:
677 + 127 - 456
24 - 17 + 13
4 * ( 5 - 3 )

output
677 127 + 456 -
24 17 - 13 +
4 5 3 - *

Last edited by NickPats; 11-04-2012 at 08:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] reading lines from file zuze11 Programming 8 11-23-2011 11:47 AM
While reading lines of a file do this.. plax65 Linux - Newbie 4 11-13-2011 01:07 PM
Reading lines from files Perl jack.barnes Linux - Newbie 3 04-15-2011 05:05 AM
Reading lines within a file (Perl) jack.barnes Programming 12 03-16-2011 12:09 PM
Reading random lines in c++? twirl Programming 7 10-30-2005 11:11 AM


All times are GMT -5. The time now is 11:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration