473,802 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Simple Integer Expression

25 New Member
My task is Create a program to read a simple integer expression and display the result. "Simple" means no parentheses are allowed and only the +, -, *, and / operators are allowed.

I think expression would be Multiplications , then Division, then Addition and Subtraction.

My program gave me a wrong result after addition, take a look to my code you will understand. Please help me to develop this.

Here is my Code
Mar 1 '07 #1
5 1524
DeMan
1,806 Top Contributor
Having looked at your code, I'm a little confused as to what you are trying to achieve....perh aps if you attached the output it is givving we might be able to work out what's going on
Mar 1 '07 #2
wazzup
25 New Member
Ok, First, This is my input as a string 12+34-34+11*4*11/11
Then, convert them to integer and calc them.

My program gave me result is -32
But Correct must be 56

My algorithm is get the string input then create an Array and put them into the Array. For ex input above: 12 is the first element and + is second elements of the array ...etc

Then I check the the Array if there is * then get the element before that time with elements after that. For ex input above: 11*4 . Then, get the result replace to position of * in array. Keep doing like that until no more *. Same thing with /, + and -
Mar 1 '07 #3
wazzup
25 New Member
This is my output of the program:

Expand|Select|Wrap|Line Numbers
  1. Original Input is 12 + 34 - 34 + 11 * 4 * 11 / 1
  2. Input after trimmed is 12+34-34+11*4*11/11
  3. Number elements of Array is 13
  4. Original Values of Elements of the Array
  5. A[0] = 12
  6. A[1] = +
  7. A[2] = 34
  8. A[3] = -
  9. A[4] = 34
  10. A[5] = +
  11. A[6] = 11
  12. A[7] = *
  13. A[8] = 4
  14. A[9] = *
  15. A[10] = 11
  16. A[11] = /
  17. A[12] = 11
  18.  
  19. Begin Calculate ...............
  20.  
  21. Expression: 11 * 4 = 44
  22. Expression: 44 * 11 = 484
  23.  
  24. Values of Elements after Multiplications.
  25. A[0] = 12
  26. A[1] = +
  27. A[2] = 34
  28. A[3] = -
  29. A[4] = 34
  30. A[5] = +
  31. A[6] =
  32. A[7] =
  33. A[8] =
  34. A[9] = 484
  35. A[10] =
  36. A[11] = /
  37. A[12] = 11
  38.  
  39. Expression: 484 / 11 = 44
  40.  
  41. Values of Elements after Divisions.
  42. A[0] = 12
  43. A[1] = +
  44. A[2] = 34
  45. A[3] = -
  46. A[4] = 34
  47. A[5] = +
  48. A[6] =
  49. A[7] =
  50. A[8] =
  51. A[9] =
  52. A[10] =
  53. A[11] = 44
  54. A[12] =
  55.  
  56. Expression: 12 + 34 = 46
  57. Expression: 34 + 44 = 78
  58.  
  59. Values of Elements after Additions.
  60. A[0] =
  61. A[1] = 46
  62. A[2] =
  63. A[3] = -
  64. A[4] =
  65. A[5] = 78
  66. A[6] =
  67. A[7] =
  68. A[8] =
  69. A[9] =
  70. A[10] =
  71. A[11] =
  72. A[12] =
  73.  
  74. Expression: 46 - 78 = -32
  75.  
  76. Final Answer is -32
  77. Correct answer is: 56
  78.  
Mar 1 '07 #4
wazzup
25 New Member
I got it! Computer does the * and / first (left to right) then do the + and - (left to right). Thanks for the help!
Mar 1 '07 #5
DeMan
1,806 Top Contributor
That was easy....
Mar 1 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4548
by: Yodai | last post by:
Hi all! I have an int that comes with a value like 0x07fa and I have to turn it into a float value of 204.2 decimal to display it.... if I try to divide it by 10 I get bogus numbers. I presume I have to make it decimal before calculating, but I am not sure about it. Any light upon this simple (or it seems to me so, at least) begginner problem? TIA..
39
2618
by: gtippery | last post by:
Newbie-ish questions - I've been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways to do what this does. It does compile & run for me (with PowerC, a 16-bit DOS compiler); if there are nonstandard or "accidentally-works" aspects, please let me know. {This is the sort of situation where if I knew *what* to Google for, I wouldn't *need* to... <grin>}
35
1583
by: A Jafarpour | last post by:
Hi everyone, hope someone can tell if there is any way to, dynamically, build an statement and then call some functions to execute the statement. I know examples always help, so here what I am trying to accomplish: Dim myStatement as String myStatement = "Dim result as integer result= 2 * 3" now is there any way to actually let a program to treat myStatement as a statment and therefore execute whatever
4
1956
by: ItsMe | last post by:
Hi Guyz, I'm unable to understand this (AddressOf) error??? In VB6 I have two functions: ---------------------------- Public Function ImageFirstImageCallback(ByVal hWnd As Integer, ByVal iMsg As Integer, ByVal wParam As Integer, ByVal LParam As Integer) As Integer 'Code here
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9561
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10058
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9111
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7597
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6835
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3789
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.