0

Can someone help me with my calculator

Hello I've tried for a very long time to create a calculator with 1 line input that can solve for example 3+7*2 which the answer is 17 so it will do the multifunction first then the add I couldn't manage to do it if someone can make it for me I'll be greatful language: C++ .with console..

12th Jul 2019, 2:53 PM
Cout
Cout - avatar
12 Answers
+ 1
I finished my implementation of the algorithm. :) I noticed the lesson missed one step: When you encounter an operator whose precedence is not higher and the stack is not empty, you evaluate the expression before you push it. I also added support for numbers with more than one digit, but obviously, you still could add tons of additional features, for example support for spaces in the expression, checking for illegal characters, additional operators, etc. Of course, if you have any specific questions regarding the code, feel free to ask me. That said, here is the code: https://code.sololearn.com/cRn3GTjf0804/?ref=app Looking forward to seeing your implementation! ;)
12th Jul 2019, 6:43 PM
Shadow
Shadow - avatar
+ 1
This lesson will get you started: https://www.sololearn.com/learn/704/?ref=app It pretty describes every single step of the algorithm used for the task. What you need to know is a bit of C++ knowledge and what a Stack is. Again, there is a lesson here simplifying the concept: https://www.sololearn.com/learn/642/?ref=app The good thing is, you don't actually have to write the Stack yourself if you don't want to - although it will teach you quite a lot - but C++ offers a ready-to-use library for that. Here is the link: http://www.cplusplus.com/reference/stack/stack/ This should help you with your problem. If you still have questions or problems with the code, go ahead and ask again. Don't just let someone else do all the work for you. It's a lot less rewarding. ;) Here is an inspiration for you from another user (not entirely functional, my bad): https://code.sololearn.com/cZjtQib292vB/?ref=app
12th Jul 2019, 3:10 PM
Shadow
Shadow - avatar
+ 1
Shadow thanks you so much, I know what stack is I've learnt assembly but the solution you gave me is incorrect you can't do for example 5*3+5 this is the thing I am not doing right
12th Jul 2019, 3:38 PM
Cout
Cout - avatar
+ 1
Sure, best thing would be for you to just link the code here so i can view it directly. That makes it easier for me.
12th Jul 2019, 7:21 PM
Shadow
Shadow - avatar
0
Please excuse me, R4Z7, I was not aware of that. The example input I tried worked, therefore I assumed the code to be functional. I edited the comment, guess I was too lazy. The first lesson should still work as a guide though. I'll sit down myself later and try to code a working solution. You could attempt the same, so that we can compare later. :)
12th Jul 2019, 3:44 PM
Shadow
Shadow - avatar
0
Shadow You don't have to be sorry you were very very helpful regardless thank you I will be at my house in 2hours~
12th Jul 2019, 3:46 PM
Cout
Cout - avatar
0
Shadow thank you so much I appreciate it a lot I'll work to do my own algorithm as well. Thank you for your help! I don't understand 1 thing why do I have to use stack I understand that it is for the ornize so it'll do multifunction before plus but still, there is no other way? can I tell you where I am having a problem?
12th Jul 2019, 6:46 PM
Cout
Cout - avatar
0
You don't neccessarily have to use a Stack. The fundamental problem is a recursive one, which means we can also solve it using recursive functions. We evaluate a basic expression, and when we encounter a higher operator, we evaluate that basic expression first, before going back to the first, and so on. A Stack is just another way of realizing the solution. Maybe there are also other methods, but those are the two I know of. If you are interested, just look it up on the internet.
12th Jul 2019, 7:07 PM
Shadow
Shadow - avatar
0
Shadow I am trying to do it but something seems to not work can you help me with it?
12th Jul 2019, 7:18 PM
Cout
Cout - avatar
0
Shadow maybe we can talk on discord/Skype please?
12th Jul 2019, 7:25 PM
Cout
Cout - avatar
0
Shadow please?
12th Jul 2019, 8:17 PM
Cout
Cout - avatar
0
I am currently busy with moving and setting up in a new flat, so that is rather hard. And unless you need anything special apart from help with your code, SoloLearn should be sufficient. DM me if you have any particular problems.
12th Jul 2019, 10:37 PM
Shadow
Shadow - avatar