+ 8

How to perform arithmetic operation on algebraic equations in c++

Every one I am new to programming as I am just 16 but I want some help in code I was thinking of creating a program which will find the the derivative ok I know how to find the derivative finding derivative of 3x or 3x² e.t.c in c ++ as it is very easy as we can apply power rule to this thing but some time we have to apply quotient rule to find derivative so how can I perform arithmetic operations on algebraic equation. My question is how can we do this we know that x+x is 2x and 3x+2+4x+2 is 7x+4 how can I do that in c++. I just want to know about this rest of the solution I will try to figure out by myself as we know best way to learn programming is to do it by yourself I will ask if got stuck😁😉😅

24th May 2019, 9:02 AM
Arslan Iftikhar
Arslan Iftikhar - avatar
14 Answers
+ 9
Cool idea! Theres a lot to this but I can give you some ideas. You can represent polynomials using arrays: 9 + 2x + 7x² + 10x³ -> [9,2,7,10] Then to take the derivative you can remove the leftmost element and multiply the others with the usual power rule. [9,2,7,10] -> [2*1, 7*2, 10*3] = [2,14,30] which is `2 + 14x + 30x²`! And to add polynomials you can just add the arrays, element by element. Of course you will need to do some work to turn the arrays back into text to display them nicely on screen but you'll figure it out!
24th May 2019, 9:16 AM
Schindlabua
Schindlabua - avatar
+ 6
I just made a short (hopefully not to complex) code that will tokenize basic mathematical statements. In this example a token can be a number, a coefficient (a number in front of a variable), an operator (+, -, * or /) and a variable (a-Z). A token object will store the type and the value of the token. Depending on the type the value is either a char (for variables and operators) or an integer (for numbers and coefficients). All tokens will be stored in the tokens vector. The program will output type and value of any new token that is added. Try something like 63 + 56t - 6318 + x as input and you will see what happens. I hope this helps you :) https://code.sololearn.com/ca5Ue0VjwyH8/?ref=app
24th May 2019, 6:06 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 5
I assume you use a string to enter your equation. You can analyse the string and give each number or operator a token. 3 will be a token of type number, + a token of type operator and 3x e.g. a token of type x. Then you can add tokens of type number together and tokens of type x together to simplify the equation. You could also combine two minus operators into one plus operator. I hope that made sense to you. If not just ask again :)
24th May 2019, 9:21 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 5
A token is a small piece of syntax. E.g. the C++ compiler will fist tokenize the code to further process it later. The compiler will put brackets, numbers, keywords as tokens into specific categories. You can also use tokens for any syntax and math has in a way it's own syntax. E.g. "2+3" can be tokenized into number, operator, number. These tokens can be handled much easier by a program. For example this code uses tokens to handle syntax (in a very basic form): https://code.sololearn.com/cFeUl4CwIVzZ/?ref=app Here's a further link: https://en.m.wikipedia.org/wiki/Lexical_analysis#Token
24th May 2019, 10:18 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 4
bell ups, that coefficient is a bug. I forgot to initialize the num variable with value 0 so it had random start values. It's fixed now :)
24th May 2019, 9:01 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 4
Aaron Eberhardt thanks for helping but I can't get it the reason is that I don't know how to use union and also don't know about the vector so I guess I have to learn these first and after that I will see your code again by the way thanks for helping you are awesome 😎😎
25th May 2019, 1:15 AM
Arslan Iftikhar
Arslan Iftikhar - avatar
+ 2
What do you mean by tokens here Aaron Eberhardt
24th May 2019, 10:02 AM
Arslan Iftikhar
Arslan Iftikhar - avatar
+ 2
You can simply use a string and cin I guess.
24th May 2019, 11:11 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
Ok
24th May 2019, 2:55 PM
Arslan Iftikhar
Arslan Iftikhar - avatar
+ 2
very helpful! what is the added token coefficient at the beginning? input: y=3x+1 Added Token (coefficient, 4889504) Added Token (variable, y) Added Token (variable, =) Added Token (coefficient, 3) Added Token (variable, x) Added Token (operator, +) Added Token (number, 1)
24th May 2019, 8:43 PM
bell
bell - avatar
+ 2
good! I was puzzled and thought I was missing something. thanks for this second code easier than the js minify
24th May 2019, 9:08 PM
bell
bell - avatar
+ 1
Question arises how can I actually input the the equation first
24th May 2019, 10:36 AM
Arslan Iftikhar
Arslan Iftikhar - avatar
+ 1
But bro I don't understand your token stuff maybe it will take time as I am knew I have to learn alot 😁😁
24th May 2019, 2:56 PM
Arslan Iftikhar
Arslan Iftikhar - avatar
+ 1
Okey.
25th May 2019, 4:37 AM
Jerry