473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Parenthesis and internal arithmetic equation with operation

I have an equation like ((3+4)-(24/5)), and if the user presses ., then the equation has ended, and it checks that the parentheses are balanced and solves all the internal operations; what should I do? How do I make this program? Please help, thank you. I am required to use arrays to make sure the parentheses are balanced."
Apr 15 '10 #1
4 2711
jkmyoung
2,057 Expert 2GB
There are many ways to go about implementing this program. You also have to worry about order of operation.
I suggest something that considers the following:
- Tokenize: Each number, each operator, and each parenthesis counts as a token.
- Do multiple passes, where each operation determines the highest precedence operation.
- If you have a parentheses, it automatically makes it highest precedence.
- With parentheses, you can recursively solve by solving the entire operation inside the parentheses first.
Eg. Let's take a different example (5 + 4 * 3) + (3 * 2 / 1) * 3
First pass. We immediately hit parentheses, so let's solve for that.

subsolve 5 + 4 * 3.. * is highest priority.
5 + 12 + is highest priority
17. return

17 + (3 * 2 / 1) * 3 . Parentheses again.

Solve 3 * 2 / 1 * is highest priority
6 /1 / is highest priority
6. return

17 + 6 * 3 * is highest priority
17 + 18 + is highest priority
35

One possible structure that might lend itself to this problem is a linked list.
Apr 15 '10 #2
donbock
2,426 Expert 2GB
What are your requirements for an expression like (4+6/2)?
  • Evaluate according to arithmetic order of operations - the result is 7.
  • Evaluate from left to right - the result is 5.
  • This expression is illegal, the user is not allowed to input any expression where there are more than two terms within the parentheses.
Apr 16 '10 #3
the solution must be from left to right and paranthesis of c code must be balanced
Apr 16 '10 #4
Dheeraj Joshi
1,123 Expert 1GB
You can user stack to implement this.
Whenever the item is number push them into stack, if the item is operator pop the element and perform the operation. Push the result back to stack.

You also should consider "(" and ")".
You need to modify the algorithm for infix and postfix expressions.

This link may give you some idea

Regards
Dheeraj Joshi
Apr 16 '10 #5

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

Similar topics

7
by: jdog1016 | last post by:
Recently I coded something for a class that uses a LOT of doubles with precision out to the thousands place. I did most of the arithmetic by multiplying each by a thousand and converting to an...
17
by: Mantorok Redgormor | last post by:
are all integers represented internally as just bit vectors? -- nethlek
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
4
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in...
12
by: Raghu | last post by:
Hello all, I need your help on how to redefine teh function of arithmetic operators in C. for example : if there is an equation c = a/b; i want to execute my own algorithm for division...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
9
by: Mike Aubury | last post by:
Is there any standard (or even non-standard) way to detect limit overflow in arithmetic in C ? eg. /* assuming 4 byte ints.. */ int a=2147483647; int b=2147483647; int c;
1
by: thebigsquid | last post by:
hi, its me again, so this software obviously has serious problems. now i'm getting this error message when i try to access any of it! any ideas much appreciated thanks the big squid An...
7
by: Mike TI | last post by:
Nov 13, 2007 Hi all Can I build a string for arithmetic operation. (VB NET 2005) e.g. I want to build an arithmetic operation from within the program as
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.