C program related to logic gates!

This is a discussion on C program related to logic gates! within the C Programming forums, part of the General Programming Boards category; Hi, have been asked to produce a c program allowing the user to simulate combinational logic gates where the user ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    12

    C program related to logic gates!

    Hi,
    have been asked to produce a c program allowing the user to simulate combinational logic gates where the user inputs binary values for A,B and C and the output circuit is displayed. have been given a template array to use with the program which contains the binary info for AND, OR and NOT gates.
    the first part of the spec tells me to Start by adding C functions for simulating additional logic gates: XOR, XNOR, NAND and NOR.

    all i am looking for is how would i go about answering the first part.... adding c functions for the XOR, XNOR etc logic gates as this part is confusing me?

    The template code is shown below!

    Code:
    #include<stdio.h>
    int And(int a, int b);
    int Or(int a, int b);
    int Not(int a);
    void main()
    {
     ///where main body of code will go
    }
    int And(int a, int b)
    {
     int output;
     if(a==0 && b==0)
      output=0;
      if(a==1 && b==0)
      output=0;
     if(a==0 && b==1)
      output=0;
     if(a==1 && b==1)
      output=1;
     return (output);
    }
    int Or(int a, int b)
    {
     int output;
     if(a==0 && b==0)
      output=0;
      if(a==1 && b==0)
      output=1;
     if(a==0 && b==1)
      output=1;
     if(a==1 && b==1)
      output=1;
     return (output);
    }
    int Not(int a)
    {
    int output;
     if(a==0 )
      output=1;
     if(a==1 )
      output=0;
      return (output);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,185
    I suggest that you simplify, e.g.,
    Code:
    int And(int a, int b)
    {
        return a && b;
    }
    Similiarly, Or and Not should be implemented using || and ! to keep it simple.

    For NAND, you can implement it in terms of Not and And. Likewise for the other "gates". (Since NAND is a universal gate, you could implement the other functions in terms of Nand alone, but that's probably rather unnecessary.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    12
    '' For NAND, you can implement it in terms of Not and And. Likewise for the other "gates". (Since NAND is a universal gate, you could implement the other functions in terms of Nand alone, but that's probably rather unnecessary.) ''



    • I am familiar with logic gates writing them down etc but how do i go about putting that as code? obviously a NAND gate is an AND gate into a NOT gate but how do i put that as a function in my code?


  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,185
    Quote Originally Posted by yellowmania
    how do i go about putting that as code? obviously a NAND gate is an AND gate into a NOT gate but how do i put that as a function in my code?
    Composition of function calls, e.g.,
    Code:
    return Not(And(a, b));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    142
    Quote Originally Posted by yellowmania View Post
    ... but how do i put that as a function in my code?
    LaserLight has given her usual excellent advice.

    But maybe I can get you started by asking a question or two.
    From your original code or template which line is the declaration of the And function?
    Which lines are the definition of the And function?
    What name would you give your function with the NAND operaton to be consistent with the names of the other functions given in your original code or template?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program related in using RANDOM numbers using C.
    By Ashleykaye in forum C Programming
    Replies: 3
    Last Post: 09-11-2011, 11:30 AM
  2. Help with first c++ program (math-related)
    By necrovamp in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2010, 01:33 PM
  3. Cannot get my program to exit properly (switch related)
    By Skarjak in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 10:14 AM
  4. Logic Gates
    By JFK in forum C Programming
    Replies: 0
    Last Post: 10-21-2001, 06:56 AM
  5. non program related questions about C
    By Nova in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:42 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21