User Tools

Site Tools


user:cforman:portfolio:cprogproject5

Logic Gates

A project for C/C++ by Corey Forman during the Spring 2012.

Objectives

Create a program that uses two created inputs and checks them in a series of way to observe and report if they pass through specific logic gates.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • basic idea of how logic gates work
  • c++ coding
  • class programming (class files and separating the program into manageable pieces)

Background

The idea of this project was brought up to us by the teacher. He discussed what an AND gate is and we went from there. here is a pic that has some logic gates in minecraft just for entertainment.

Scope

The scope of this project was to expand upon the idea of using both class functions and learning how logic gates function. The program will take two predetermined variables and then runs them through a series of checks to see which logic gates they pass.

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • attribute1: why you feel your pursuit of this project will gain you this attribute
  • attribute2: why you feel your pursuit of this project will gain you this attribute
  • etc…

Procedure

The first step after writing the code was to assign boolean values to A and B. next the program is run and results checked. Change A and B test again to confirm.

Code

Upon completion of the project, if there is an applicable collection of created code, place a copy of your finished code within <code> </code> blocks here.

 #include <cstdio>
  2 #include <cstdlib>
  3
  4 class Gate{
  5     public
  6         GATE()=0;
  7         bool getX();
  8         void setA(bool);
  9         void setB(bool);
 10     protected:
 11         void setX(bool);
 12         bool A;
 13         bool B;
 14     private:
 15         bool X;
 16 };
 17
 18 GATE::GATE()
 19 {
 20     A=B=X=false
 21 }
 22
 23 GATE()=0;
 24         bool getX();
 25         void setA(bool);
 26         void setB(bool);
 27     protected:
 28         void setX(bool);
 29         bool A;
 30         bool B;
 31     private:
 32         bool X;
 33 };
 34
 35 GATE::GATE()
 36 {
 37     A=B=X=false
 38 }
 39
 40
 41 bool GATE:: getX()
 42 {
 43     return(X);
 44 }
 45
 46 void GATE:: setA(bool A)
 47 {
 48     this.A=A;
 49 }
 50
 51 void GATE:: setB(bool B)
 52 {
 53     this.B=B;
 54 }
 55
 56 void GATE:: setX(bool X)
 57 {
 58     this.X=X;
 59 }
 60
 61
 62 Class AND:public GATE{           //this is class AND inherting Class GATE's     code
 63     Public:
 64         AND();
 65         void process();
 66 };
 67
 68 AND::AND()
 69 {
 70     setA(false);
 71     setB(false);
 72     setX(false);
 73 }
 74
 75 void AND:: process()
 76 {
 77     if((A==true)and(B==true))
 78         setX(true);
 79     else
 80         setX(false);
 81 }
 82
 83
 84 #include "gate.h"
 85 #inlcude "and.h"
 86
 87 int main()
 88 {
 89     AND myAND();
 90     myAND.setA(true);
 91     myAND.setB(false);
 92     myAnd.process();
 93     printf("result is: %d\n",myAND.getX());
 94     return(0);
 95 }
 96
 97 class NAND:public GATE{
 98     public:
 99         NAND();
100         void Process();
101 };
102
103 NAND::NAND()
104 {
105     setA(false);
106     setB(false);
107     setX(false);
108 }
109
110 void NAND:: process()
111 {
112     if ((A==true)and(B==true))
113         setX(false);
114     else
115         setX(true);
116 }
117
118
119
120 OR::OR()
121 {
122     setA(false);
123     setB(false);
124     setX(false);
125 }
126
127 void OR:: process()
128 {
129     if ((A=true) or (B=true))
130         setX(true);
131     else
132         setX(false);
133 }
134
135 XOR::XOR
136 {
137     setA(false);
138     setB(false);
139     setX(false);
140 }
141
142 XOR::process()
143 {
144     if((A=true) and (B=true))
145         setX(false);
146     else if((A=true) or (B=true))
147             setX(true);
148     else
149         setX(false);
150 }
151
 class NOT:public GATE{
  7     public:
  8         NOT();
  9         void process();
 10 };
4 NOT::NOT()
  5 {
  6     setA(false);
  7     setB(false);
  8     setX(false);
  9 }
 10
 11 void NOT:: process()
 12 {
 13     if(A==true)
 14         setX(false);
 15     else
 16         setX(true);
 17 }
class NOR:public GATE{
  7     public:
  8         NOR();
  9         void process();
 10 };
 NOR::NOR()
  5 {
  6     setA(false);
  7     setB(false);
  8     setX(false);
  9 }
 10
 11 void NOR:: process()
 12 {
 13     if((A==false)and(B==false))
 14         setX(true);
 15     else
 16         setX(false);
 17 }
 18
 class XNOR:public GATE{
  7     public:
  8         XNOR();
  9         void process();
 10 };
 XNOR::XNOR()
  5 {
  6     setA(false);
  7     setB(false);
  8     setX(false);
  9 }
 10
 11 void XNOR:: process()
 12 {
 13     if((A==false)and(B==false))
 14         setX(true);
 15     else if((A==true)and(B==true))
 16         setX(true);
 17     else
 18         setX(false);
 19 }
 20
 
 
 

this is what main.cc looked like in order for the program to run.

  #include <cstdio>
  2 #include <cstdlib>
  3
  4 #include "gate.h"
  5 #include "and.h"
  6 #include "or.h"
  7 #include "xor.h"
  8 #include "nand.h"
  9 #include "not.h"
 10 #include "nor.h"
 11 #include "xnor.h"
 12
 13 int main()
 14 {
 15     AND *myAND = new AND();
 16     NAND *myNAND = new NAND();
 17     OR *myOR = new OR();
 18     XOR *myXOR = new XOR();
 19     NOT *myNOT = new NOT();
 20     NOR *myNOR = new NOR();
 21     XNOR *myXNOR = new XNOR();
 22
 23     myAND->setA(true);
 24     myAND->setB(true);
 25     myNAND->setA(true);
 26     myNAND->setB(true);
 27     myOR->setA(false);
 28     myOR->setB(false);
 29     myXOR->setA(false);
 30     myXOR->setB(true);
 31     myNOT->setA(false);
 32     myNOT->setB(false);
 33     myNOR->setA(true);
 34     myNOR->setB(true);
 35     myXNOR->setA(true);
 36     myXNOR->setB(true);
 37     myAND->process();
 38     printf("result for AND  is: %d\n",myAND->getX());
 39     myNAND->process();
 40     printf("result for NAND  is: %d\n",myNAND->getX());
 41     myOR->process();
 42     printf("result for OR is: %d\n",myOR->getX());
 43     myXOR->process();
 44     printf("result for XOR is: %d\n",myXOR->getX());
 45     myNOT->process();
 46     printf("result for NOT is: %d\n",myNOT->getX());
 47     myNOR->process();
 48     printf("result for NOR is %d\n",myNOR->getX());
 49     myXNOR->process();
 50     printf("result for XNOR is %d\n",myXNOR->getX());
 51
 52     return(0);

Execution

Again, if there is associated code with the project, and you haven't already indicated how to run it, provide a sample run of your code:

result for AND  is: 1
result for NAND  is: 0
result for OR is: 0
result for XOR is: 1
result for NOT is: 1
result for NOR is 0
result for XNOR is 1

changed variables…original found in main.cc above

  myAND->setA(true);
 24     myAND->setB(true);
 25     myNAND->setA(false);
 26     myNAND->setB(true);
 27     myOR->setA(false);
 28     myOR->setB(true);
 29     myXOR->setA(false);
 30     myXOR->setB(false);
 31     myNOT->setA(false);
 32     myNOT->setB(true);
 33     myNOR->setA(true);
 34     myNOR->setB(false);
 35     myXNOR->setA(false);
 36     myXNOR->setB(false);

results

lab46:~/src/cprog/c++/sampleprogs/abtractbaseclass$ ./program      
result for AND  is: 1
result for NAND  is: 1
result for OR is: 1
result for XOR is: 0
result for NOT is: 1
result for NOR is 0
result for XNOR is 1

lab46:~/src/cprog/c++/sampleprogs/abtractbaseclass$

Reflection

Logic gates are an important thing to understand in c++ as this helps you go about through the decision process and gives the user the ability to interpret and realize a decision making process.

References

In performing this project, the following resources were referenced:

  • in class data only except for the photo which was from google.com
user/cforman/portfolio/cprogproject5.txt · Last modified: 2012/05/09 23:20 by cforman