LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 06: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 06:56 PM.
 
Old 11-04-2012, 06:53 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 07: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 07:34 PM.
 
  


Reply



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 10:47 AM
While reading lines of a file do this.. plax65 Linux - Newbie 4 11-13-2011 12:07 PM
Reading lines from files Perl jack.barnes Linux - Newbie 3 04-15-2011 04:05 AM
Reading lines within a file (Perl) jack.barnes Programming 12 03-16-2011 11:09 AM
Reading random lines in c++? twirl Programming 7 10-30-2005 10:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:55 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
Open Source Consulting | Domain Registration