How do i add parentheses and exponents to my code?

Joined
Nov 30, 2022
Messages
1
Reaction score
0
This is the pastebin for my code: http://pastebin.com/gV01kTje

Basically I have an assignment where I need to create a calculator capable of up to 3 step equations and being able to do all PEMDAS operations in PEMDAS order. So far I've gotten it up to 3 step operations and being able to do addition, subtraction, multiplication, and division in PEMDAS order, but I don't know where I would even start with trying to implement parentheses and exponents into my code. Can someone ease help me out?
 
Joined
Sep 20, 2022
Messages
230
Reaction score
38
This is the pastebin for my code: http://pastebin.com/gV01kTje

Basically I have an assignment where I need to create a calculator capable of up to 3 step equations and being able to do all PEMDAS operations in PEMDAS order. So far I've gotten it up to 3 step operations and being able to do addition, subtraction, multiplication, and division in PEMDAS order, but I don't know where I would even start with trying to implement parentheses and exponents into my code. Can someone ease help me out?

Is it easier to ignore the "3 step" requirement and solve calculations of ANY length?

Can the program be subdivided into reusable parts?

(1) get a string from the user

(2) parse the string into an array of operators and numbers (aka tokenization)

(3) convert the array into postfix form (see shunting yard algorithm) to remove parentheses and simplify calculation

(4) perform the operations

(5) print the result

This is a common task, so are (2) and (3) already in the library? (4) is fairly simple, it needs one loop and a stack.

Exponents are explained in the shunting algorithm.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top