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 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
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 -
This is my output of the program: -
Original Input is 12 + 34 - 34 + 11 * 4 * 11 / 1
-
Input after trimmed is 12+34-34+11*4*11/11
-
Number elements of Array is 13
-
Original Values of Elements of the Array
-
A[0] = 12
-
A[1] = +
-
A[2] = 34
-
A[3] = -
-
A[4] = 34
-
A[5] = +
-
A[6] = 11
-
A[7] = *
-
A[8] = 4
-
A[9] = *
-
A[10] = 11
-
A[11] = /
-
A[12] = 11
-
-
Begin Calculate ...............
-
-
Expression: 11 * 4 = 44
-
Expression: 44 * 11 = 484
-
-
Values of Elements after Multiplications.
-
A[0] = 12
-
A[1] = +
-
A[2] = 34
-
A[3] = -
-
A[4] = 34
-
A[5] = +
-
A[6] =
-
A[7] =
-
A[8] =
-
A[9] = 484
-
A[10] =
-
A[11] = /
-
A[12] = 11
-
-
Expression: 484 / 11 = 44
-
-
Values of Elements after Divisions.
-
A[0] = 12
-
A[1] = +
-
A[2] = 34
-
A[3] = -
-
A[4] = 34
-
A[5] = +
-
A[6] =
-
A[7] =
-
A[8] =
-
A[9] =
-
A[10] =
-
A[11] = 44
-
A[12] =
-
-
Expression: 12 + 34 = 46
-
Expression: 34 + 44 = 78
-
-
Values of Elements after Additions.
-
A[0] =
-
A[1] = 46
-
A[2] =
-
A[3] = -
-
A[4] =
-
A[5] = 78
-
A[6] =
-
A[7] =
-
A[8] =
-
A[9] =
-
A[10] =
-
A[11] =
-
A[12] =
-
-
Expression: 46 - 78 = -32
-
-
Final Answer is -32
-
Correct answer is: 56
-
I got it! Computer does the * and / first (left to right) then do the + and - (left to right). Thanks for the help!
DeMan 1,806
Top Contributor Sign in to post your reply or Sign up for a free account.
Similar topics |
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..
|
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>}
|
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
|
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
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |