1

I am using DDMathParser in my app, and have recently come across the need to get occurrences of any group of numbers within a () parentheses bracket thingy (very highly technical!). For example, I would need to get (6+5) out of 6+7/8(6+5). Specifically, I would like to be able to do this so that I can make (56+9)sqrt compile just as well as sqrt(56+9). Any help?

P.S. I know that the maker of DDMathParser is often sighted in this neck of the woods. I am secretly hoping that he will come to the rescue and either fix my problem so I can implement it myself or him make it part of DDMathParser! :)

Undo
  • 25,519
  • 37
  • 106
  • 129
  • Does NSString's componentsSeparatedByString: method, which returns an NSArray of all substrings not suffice? – Luke Mar 12 '13 at 22:37
  • @Luke No, I need components **between** two **different** characters. Unless there's something I'm missing... – Undo Mar 12 '13 at 22:41
  • Indeed not likely the cleanest of methods, but passing said method your first bracket, then filtering once more with the closing brace would work... it's just messy. Perhaps an NSPredicate would work - but I can't advise you on their usage. – Luke Mar 12 '13 at 22:45
  • Yes, I do lurk in these woods. Can you explain the problem a bit more? Are you basically trying to sanitize the input string or something? – Dave DeLong Mar 13 '13 at 05:15
  • @DaveDeLong I'm trying to make a calculator app. My problem is that the user would have to enter `sqrt(9)` to get that square root - not all that good of a UX. Same with sin, etc. My hope was to be able to find occurrences of values inside parenthesis, check for sin/cos/sqrt/etc on the other side, and make necessary adjustments. Sorry if that looks like a wall of text. \n doesn't work. – Undo Mar 13 '13 at 15:10
  • So, I've been thinking about this, and I'm hesitant to answer it because I don't think this is the right way of doing things. But I also haven't formalized what I think the correct way is yet. – Dave DeLong Mar 24 '13 at 14:46
  • Yes - maybe I'm using DDMathParser for what it's not intended? – Undo Mar 24 '13 at 20:38
  • Well, kind of. I think that you're using DDMathParser in a suboptimal fashion, which has led you into this corner of what you're trying to do. – Dave DeLong Mar 25 '13 at 00:56
  • @DaveDeLong What would be considered an optimal fashion? – Undo Mar 25 '13 at 02:59
  • @ErwaySoftware I'm still working that out :) – Dave DeLong Mar 25 '13 at 03:07
  • @DaveDeLong Any more thoughts? – Undo Apr 19 '13 at 22:02

1 Answers1

2

So, I've thought a lot about this question since you posted it a month ago. From what I understand, you're constructing a string as the user clicks/taps buttons.

I think this is your problem.

As the user taps buttons, you should be constructing (or modifying) DDExpression objects. This is the "pure" format of a math expression, whereas a string is lossy and difficult to manipulate. The string you show to the user should be generated from the DDExpression tree you're building.

This is a complex problem, and I'm still not entirely sure how I would go about implementing this, but this is the root of how I'd do it. I would not just construct a string based on what the user types.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 2
    Wow, thanks. I actually influenced the thoughts of a 110K user and the creater of my favorite framework ever! (Except maybe UIKit). I've already launched the app - Totalizer, iPad only - but I had to launch it without special operations. I'll be implementing what you suggested in the next few weeks. – Undo Apr 19 '13 at 23:40