Thread: array problem

  1. #1
    Unregistered
    Guest

    Post array problem

    i have to write a program where the users will input mathematical problems for example they might input 34*4+6-9/3, the program should ouptut the correct answer. I have to write a program that will do calculator like problems, for example: 34*3+20/2, the multiplication and division must be done first like in regular math. and i have to store the numbers as integers in an array and the *,+,-, / stored as char in another array. well i know how to do that part but how do I make the program actually do the operations and in the correct order it should. thanx

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Lightbulb

    In my opinion, you would have to parse the line that the user enters, for example: 2*3+4.

    You have to scan the line until you reach a symbol ('*'), store what you scanned in a variable. Then, depends on the symbol scanned, you have to store the type of operation the user is looking for (e.g. multiplication) and keep doing this until you reach the end of the line.
    When you are done parsing the line, you can combine all you variables based on the operation the user requested and output the result.
    Also make sure that you check for all error conditions (e.g. don't let the user to input alphabetic characters).

    Hope this helped.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Ah, a good program to do. The calculator program. I like this type of programming because it causes you to analyze. It makes you think about what you, as a person, have to do when you encounter a problem. To write the program, you have to first ask yourself, what would I do first?

    -I would analyze and work with the order of operations.
    -Then, after looking through the whole problem, I would start to do the calculations in order (by operators).

    That is how you would go about doing something like this. The thing about this type of program is that the code isn't too hard, but it is the logic. It is coming up with what to and how to do it...in code. Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM