Splitting a string based on specific characters

509 views Asked by At

I will try to describe my problem as well as I can. I am trying to write a program that will handle equations like:

F = (X∨A) ↔ (X∨B) ( (X OR A) is equivalent to (X OR B) )!

I have to solve it by 'X', or to better say, write disjunctive and/or conjunctive normal form.

So, theoretically, it goes like this:

enter image description here

When the truth table is written, you have to see when F is equal to 1 (tautology), and write conjunctive/disjunctive normal form.

For example (disjunctive normal form for the given table): For A=0,B=0 and A=1,B=1, the value of X does not matter, and for A=0,B=1 AND A=1,B=0, X must be 1.

enter image description here

In the end, X=A∨B.

Since I'm writing it in C#, equations are written in a TextBox. What bothers me,is how should I separate my string so I can solve it part by part?

2

There are 2 answers

4
fcmonoid On

What about first trying "Split()" method (or other methods) of the class String in C#? In the first place, you'd better push your users to insert a blank (as the separator of Split()) between each pair of tokens (e.g. A AND B) , so that you can concentrate on the main logic of your solver.

5
LocEngineer On

I see. It's basically a simplifying calculator that doesn't (necessarily) have sequential input via buttons but a ready-made formula typed or pasted in a textbox.

What you need to do is therefore

  • define a set of permitted operators (AND, OR, NOT, XOR, etc.) in various permitted notations (Λ, V, !, =, !=, etc., +,*,-,=, ↔ etc.)
  • check whether the input is syntactically correct; which means
    you'll probably have to first remove all spaces from the input
    string, then use regular expressions for allowed constructs, which
    will probably prove to be a pain
  • if input is valid, check for parentheses first to determine grouped expressions
  • simplify according to boolean algebra

Or simply follow this link (Java but still): Boolean expression solver/simplifier, use the tool bc2cnf or any other library that is linked there and spare yourself a lot of headache by restricting the permitted input to one permitted by the used library.

Hope this helps.