• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Extract variables from string math expression with parenthesis

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to evaluate an a string math expression , so before i want to extract variables so i have to remove all special characters my code only can remove a special character like `a+b*c` and the result i get is `a=;b=;c=`, now if i write two successive special characters like `(a+b)*c` and `a++b` results will be `=;a=;b=;c=` and `a=;=;b=` always there is a sign `=;` added for no reasons so how to get rid of them because after that i will use mathematic functions so that will be a real problem for me please help me ! here is my code
 
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 So what is the expected output for this expression?
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exactly it's the expected output, i want the output of a+b*c expression in (a+b)*c expression !
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean except the arithmetic operators (+,-,*./) and operands it will take out everything from the given string?  To add more to this are you looking for bodmas evaluation?
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i mean if i want to type two splits like `a++b` i want the output Always look like `a=;b= ` but my output is `a=;=;b=` why??  i have a mistake in my code!
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What value are you getting in variables array after execution of the above line?
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output you are getting seems to because of any array element is a space. for a++b the length should be 2.  Print the length of that array and see what length are you getting.
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's used to remove duplicate varibles for example if i have a+a+b (input) the (output) will be `a=;b=` not `a=;a=;b=`
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i didn't understand what you said please can you explain a little?
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you take a++b as an example.



Can you check the length of variables array?

i.e. value of variables.Length
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh it's 3 ! so why it's not 2?
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that's why for a++b you are getting output as a=;=;b; because the array is now somewhat like {a,' ',b}

What are you doing in this line?

 it seems to be adding an extra space.
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing , but it's not the line that cause the problem even if i put it in comment the length is Always 3
 
Master Rancher
Posts: 4371
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check whether variables[j] is an empty string (ie the entry caused by something like '++').
If it is then don't append anything to your output string.
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try printing the elements of the array using a loop and see what it prints.
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i put a condition if   variable[j]=="" and it works now , thank you all  for help , i want to ask now another question if someone can answer me i'll be happy , i want to add a func_var which remove strings like 'cos' or 'sin' or 'tan' from string expression , did someone have an idea how to do that ?
 
Swastik Dey
Master Rancher
Posts: 2250
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest I am mainly into java and don't know much about c#.  But the logic should be somewhat like search for the position of the string and remove from there till the length of the string.

 
Saloon Keeper
Posts: 10875
235
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're going down the wrong path trying to replace characters. I would write a tokenizer and a parser to solve this problem.

I wrote an example project for you to take a look at: https://github.com/Nibsi/MathExpressions

Start by running the MathExpressions.CLI.Program. If you have questions, let me know.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!