Click here to Skip to main content
15,122,251 members
Articles / Programming Languages / C#
Tip/Trick
Posted 16 Jun 2014

Tagged as

Stats

33K views
435 downloads
10 bookmarked

Truth Table and Simplification of a Boolean Expression

Rate me:
Please Sign up or sign in to vote.
3.00/5 (12 votes)
30 Oct 2016CPOL1 min read
Generation of Karnaugh table

Introduction

The Karnaugh map, also known as the K-map, is a method to simplify boolean algebra.

Karnaugh maps are used to facilitate the simplification of Boolean algebra functions. Take the Boolean function described by the following truth table.

Image 1

                                                               Figure1 truth table  [1]

This article, and especially the attached code, is for those want to know how KARNAUGH table reduice an algebra expression.

Requirements

The reader should have basic notions of boolean algebra; Knowledge of the Quine McKluskey algorithm is optimal. Basic notions of C# are required for understanding the code.

Using of the Application

First, we have to introduce the boolean expression in the textbox. This one must use only (a;b:c:d;!,+) characters, then we click the button of table truth generation to fill our listview by the appropriate values.

Once the truth table is filled, we can process to generation of the Karnaugh table by clicking the button named Kanaugh.

Image 2

To simplify the expression in codesource, we use two methods, the first is to simplify two terms and return the simplified expression.

C#
 public string simplifier1(int i,int j,int order)
{
    int pos = -1;
    var dd = Form1.local_tab[i];
    var ff = new StringBuilder(dd);
    if (shift(Form1.local_tab[i], Form1.local_tab[j]) == 1)
   {
     ff.Remove(pos, 1);
     ff.Insert(pos, "-");
     lstTerms[order].SetItemChecked(i, true);
     lstTerms[order].SetItemChecked(j, true);
   }
  return ff.ToString();
  }

The second makes all simplifications possible in the Karnaugh table.

C#
public void evaluer(int order)
    {
        int s = 0;
        for (int i = 0; i < Form1.j; i++)
        {
            for (int k = i; k < Form1.j; k++)
            {
                simplifier1(i, k, order);
                if ((lstTerms[order].GetItemCheckState(i) == CheckState.Checked) &&
                   (lstTerms[order].GetItemCheckState(k) == CheckState.Checked))
                {
                    if (!tab.Contains(simplifier1(i, k, order)) &&
                        !Form1.local_tab.Contains(simplifier1(i, k, order)))
                    {
                        tab[s] = simplifier1(i, k, order);
                        lstTerms[order+1].Items.Add(tab[s]);
                        lstTerms[order+1].SetItemChecked(s, false);
                        s++;
                    }
                }
            }
        }
        for (int i = 0; i < Form1.j; i++)
        {
            Form1.local_tab[i] = " ";
        }
            Form1.j = s;
            for (int i = 0; i < Form1.j; i++)
            {
                Form1.local_tab[i] = tab[i];
                tab[i] = " ";
            }
            if (s == 0)
                arret = true;
    }

This is what we have as result:

Image 3

Reference an Attribution 

[1} https://commons.wikimedia.org/wiki/File:K-map_minterms_A.svg

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amar zaidi
Algeria Algeria
No Biography provided

Comments and Discussions

 
PraiseThanks Pin
Phil Ouellette29-Oct-16 9:42
MemberPhil Ouellette29-Oct-16 9:42 
GeneralRe: Thanks Pin
Amar zaidi30-Oct-16 11:51
MemberAmar zaidi30-Oct-16 11:51 
BugMy vote of 2 Pin
Philippe Mori28-Oct-16 4:19
MemberPhilippe Mori28-Oct-16 4:19 
It might have been an interesting article.... However, they are major flaws in it:
  • There is no explanation of what the program does.
  • There is no clear explanation of what second form is.
  • The capture are partially in English, partially in French.
  • When you list characters that can be used in the text box, your separators are inconsistent (; : and ,). For 5 separators, you used 3 characters : (a;b:c:d;!,+).
  • Your own code is also partly in French and partly in English.
  • Your code does not follows usual .NET convention for naming functions.
  • Your variables are poorly named. What id dd? What is ff?
  • Indentation is incorrect at some place (for ex. return statement of simplifier1 function.
  • Very poor alignment in the user interface. For example, 01 (in cd column) is not aligned with 00 above it.
  • There are some spelling errors.
  • Code is too much tight to the user interface (UI). If one, would want to use that code with a different UI, he would have to rewrote most functions.
  • Useless use of a StringBuilder. You could have used string.Replace instead.
  • Poor formatting.
  • Inconsistant use of i, j and k variables.
  • Your forms have default name (Form1, Form2), instead of something meaningful.
  • No explanation of the code in the article or as comment in code.
If it was not for so many glitches, then your article would have worth 3 and if there was more explanation it would have worth 4. And with also a cleaner design, it might even have worth 5 points.
Philippe Mori

GeneralRe: My vote of 2 Pin
Amar zaidi30-Oct-16 3:06
MemberAmar zaidi30-Oct-16 3:06 
GeneralMissing attribution for image Pin
Jochen Arndt28-Oct-16 3:34
professionalJochen Arndt28-Oct-16 3:34 
GeneralCannot Download the ZIP File Pin
Member 1004250111-Oct-16 5:59
MemberMember 1004250111-Oct-16 5:59 
GeneralRe: Cannot Download the ZIP File Pin
ronen4428-Oct-16 20:30
Memberronen4428-Oct-16 20:30 
Questiondownload Pin
TerryM7928-Feb-15 9:19
MemberTerryM7928-Feb-15 9:19 
AnswerRe: download Pin
Amar zaidi30-Oct-16 12:20
MemberAmar zaidi30-Oct-16 12:20 
General[My vote of 1] Vote 1 Pin
TonBill18-Jun-14 3:30
MemberTonBill18-Jun-14 3:30 
GeneralRe: [My vote of 1] Vote 1 Pin
Amar zaidi30-Oct-16 12:22
MemberAmar zaidi30-Oct-16 12:22 
GeneralMy vote of 1 Pin
LostTheMarbles16-Jun-14 3:48
professionalLostTheMarbles16-Jun-14 3:48 
GeneralRe: My vote of 1 Pin
Amar zaidi30-Oct-16 12:23
MemberAmar zaidi30-Oct-16 12:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.