Thread: stacks and linked lists question,can somebody figure it out please?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    56

    stacks and linked lists question,can somebody figure it out please?

    question1



    Write a program to process commands as follows.A command can be S(for set) or P(for print),upper or lower case.Each command is on a line by itself ,but can apperar any where on the line.

    The S command is followed by two arguments separated by one or more spaces.The first argument is a sssingle letter(upper or lower caese).The scond argument is an arithmetic expression.The command assigns the value of the expression to a single letter variable.Variables are Case-sensitive.(A is different from a).


    An expression may include single letter variables ,numeric constants,(with or without decimal point),the binary operators +,-,/,* and the function names ln,exp,sqrt,sin, and cos.If a variable appears in the expression ,then the current value of the variable is used in evaluating the expression.If the expression contains an error (eg mismatched brackets or undefines brackets),a message is printed and the value of the variable to be set reamins unchanged.

    the P command is folllowed by a varable,.The value of the variable is printed.

    data consists of a series of commands in any order.The commands must be executed in the order given.Data is terminated by the"command",E.It is permitted for the S caomand to be specified by S,s,set,Set,setting variable,etc.(only the first letter is significant);similarly for P


    sample data:

    S p 24
    set r 3.75
    s a P+r*21
    Set B sin(p+r)*cos(p-r)+p/(r+3.0)
    print B
    P p
    P a
    S C (a+B)/(p+r)
    set discr B*B -4.0 a*C
    PRINT discr
    E

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    that's not a question. what have you tried so far?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what exactly are you asking? For us to write your homework?

    I would suggest that you make an attempt to write the code, and then post what you've got, and then we can discuss. This is also the "Homework policy" which you could find if you search the forum.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Write a program to process commands as follows
    No! I will do nothing of the sort!
    The moment I come across an imperative statement like that in a post I immediately ignore it, and I'm sure many others do the same.

    Does this look like www.WeDoYourHomeworkForYouNoQuestionsAsked.com ???
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    No! I will do nothing of the sort!
    Are you in a bad mood today?

    I think the "Write a program to ... " came directly from the assignment itself, and was not really intended to you or me - but I'm still somewhat confused as to what the original poster actually want us to help with. The title sort of hints that the OP has some clue to how to solve the problem [perhaps], but I'm not sure what he/she wants help with [unless it really _IS_ writing his/her homework of course].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    oh sorry about that- i didnt say what it is i wanted help with!.i do apologise.im studyin for final exams and this question came in a past exam but im not sure how to do it.I am able push and pop onto a linked list.Thats the only thing ive done so far.right now im tryin to break up the problem in parts.i am tryin to write the function that will read the character 's' and set the value to the variable.
    like "S p 24" (set p=24) but the other line uses words not just a character. eg set discr.. where discr will be the variable.im not sure how to/cant write the fucntion to read n set the value to the variable when the variable are characeters or words.how can i go about this?

    the only code ive written so far works for just pushing and integer onto a stack and popping it out whenever called.should i still post my push and pop code?im workin on my evaluation of the postfix will post it when im finished.i need heplp with the redin and settting of values part please!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Which part of reading & setting a variable do you have a problem with? What have you done so far?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah I overreacted in my attempt to make a point, sorry. Just never copy and paste something into a thread without adding some of your own words about it.

    This is no easy question you're attempting!
    You need to start by working out the grammar for the language. For example C looks like this:
    http://www.quut.com/c/ANSI-C-grammar-y.html
    Have you written a recursive descent parser before?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need a data structure which associates a word (say "descr") with a value (say 42).
    Perhaps a struct containing a string and an int ?

    Then perhaps a list / array / tree / hash of these (take your pick), along with functions to say
    - find a value given a name
    - add a new name/value pair
    - update a value
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked lists, stacks and queues
    By aniramg69 in forum C Programming
    Replies: 10
    Last Post: 11-29-2008, 11:58 AM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. need help understanding Linked lists
    By Brain Cell in forum C Programming
    Replies: 6
    Last Post: 07-16-2004, 01:29 PM
  4. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 05:48 PM
  5. Linked lists.... help!!
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2003, 08:44 PM