Thread: Please Help

  1. #1
    Mike Jones
    Guest

    Question Please Help

    Does anyone know how to do this?

    You are to write a program that evaluates simple arithmetic expressions which are composed by positive numbers (real or integers) and the four basic arithmetic operators: +, -, * and /. The program will have to adhere to the following rules:

    The expressions will be evaluated left to right (all operators will have the same precedence).
    Blank spaces are ignored.
    The expression is terminated by the end of the line, at which point you will display the answer.
    The prompt is indicated with >.
    You have to guard against illegal characters entered.
    The program can be terminated only by pressing <CTRL-C>.
    Ex:

    > 2 + 3 * 4/6
    3.333333
    > 2/4+3
    3.500000
    > 2!/4+3
    *** Illegal character !
    >

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does anyone know how to do this?
    Yes, and please read the homework thread at the very top of the list.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Mike Jones
    Guest
    I understand the homework rules, and I'm not asking for an answer, but rather just some hints or some help. I've been trying to do this problem for the past two days and I haev no idea how to even begin it. I understand that I'm going to have to use getchar, but I dont exactly understand why I'm going to need it. Also I know I'm probably going to have to use a switch to go from + to - to * to \ , and that I'm going to have to use a while loop. But geting rid of the white space problem, is also bugging me. Basically I dont need an answer, but rather sometype of algorithm for how i should go about soving this problem....thanx

  4. #4
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    more constraints?

    How would you want the program to handle the following:

    > 3456348908687874545.76456489056456 + 23.444

    > / 17

    > 8 ++ 3

    is there a limit to how long an expression can be?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Since this is homework, you don't need to error proof it, that's a future exercise. All you have to do is maintain some semblance of order. If the expression is

    3 + 7 / 2 * 4

    you can make some assumptions about the order of the expression. A number will always precede an operator, there is always an operator after a number, there is always another number after an operator. The first and last tokens in the input will always be numbers. With these assumptions in mind, you develop an algorithm:
    Code:
    Read a number and save it
    Read an operator and save it
    Read a number and save it
    Determine what the operator is
    Perform the appropriate operation on the two numbers
    Save the result
    Loop while not EOL
      Read an operator and save it
      Read a number and save it
      Determine what the operator is
      Perform the appropriate operation on the result and the number
      Save the result
      Rinse
    Repeat
    Print the result
    That's all there is to it.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Mike Jones
    Guest
    I'm assuming there is no limit to the expression, I know I'll be using a float.

  7. #7
    Mike Jones
    Guest
    Prelude, Thanx a lot, I'm going to try it out and see if it works, I really appreciate the algorithm but I have just one question, how does this get rid of it not reading blank space as a character? What i mean is the program is supposed to see "3 + 5" and "3+5" as the same thing, and just pass over the " ", how is this accomplished?

    Thanx again.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how is this accomplished?
    Read a character, if that character is whitespace then read another one until it isn't whitespace.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed