User Tools

Site Tools


user:smalik2:portfolio:cprogbonusproject

Project -99: Logic Gates

A project by Saad Malik for C/C++ Programming, Spring 2012.

Objective

The goal of this project is to make a program that will create logic gates. It does not accept inputs, but if you go in and change the course code accordingly, you will get the corresponding answer. For example, the and of 1 and 1 is going to be 1.

Code

This is a multifile program. I will display the .h and corresponding .cc files in sections.

and

  1 #ifndef _and_h
  2 #define _and_h
  3
  4 #include "GATE.h"
  5
  6 class AND:public GATE{
  7     public:
  8         AND();
  9         void process();
 10 };
 11
 12 #endif
  1 #include "GATE.h"
  2 #include "and.h"
  3
  4 AND::AND()
  5 {
  6         setA(false);
  7         setB(false);
  8         setX(false);
  9 }
 10
 11 void AND::process()
 12 {
 13         if((A==true)and(B==true))
 14                 setX(true);
 15         else
 16                 setX(false);
 17 }

or

  1 #ifndef _or_h
  2 #define _or_h
  3
  4 #include "GATE.h"
  5
  6 class OR:public GATE{
  7     public:
  8         OR();
  9         void ORProcess();
 10 };
 11
 12 #endif
  1 #include "GATE.h"
  2 #include "or.h"
  3
  4 OR::OR()
  5 {
  6     setA(false);
  7     setB(false);
  8     setX(false);
  9 }
 10
 11 void OR::ORProcess()
 12 {
 13     if((A==true)or(B==true))
 14         setX(true);
 15     else
 16         setX(false);
 17 }

not

  1 #ifndef _not_h
  2 #define _not_h
  3
  4 class NOT{
  5     public:
  6         bool get();
  7         void set(bool);
  8         void NOTProcess();
  9     private:
 10         bool A;
 11         bool X;
 12 };
 13
 14 #endif
  1 #include "GATE.h"
  2 #include "not.h"
  3
  4
  5 void NOT::set(bool A)
  6 {
  7     this->A=A;
  8 }
  9
 10 void NOT::NOTProcess()
 11 {
 12     this->X=!A;
 13 }
 14
 15 bool NOT::get()
 16 {
 17     return X;
 18 }

nor

  1 #ifndef _nor_h
  2 #define _nor_h
  3
  4 #include "not.h"
  5 #include "or.h"
  6
  7 class NOR:public NOT, public OR{
  8     public:
  9         void NORProcess();
 10 };
 11
 12 #endif
  1 #include "nor.h"
  2
  3 void NOR::NORProcess()
  4 {
  5     ORProcess();
  6     set(getX());
  7     NOTProcess();
  8     setX(get());
  9 }

nand

  1 #ifndef _nand_h
  2 #define _nand_h
  3
  4 #include "not.h"
  5 #include "and.h"
  6
  7 class NAND:public NOT,public AND{
  8     public:
  9         void NANDProcess();
 10 };
 11
 12 #endif
  1 #include "nand.h"
  2
  3 void NAND::NANDProcess()
  4 {
  5     process();
  6     set(getX());
  7     NOTProcess();
  8 }

gate

This header and .cc file is basically the parent class. Everything else inherits from this.

  1 #ifndef _GATE_H
  2 #define _GATE_H
  3
  4 class GATE{
  5     public:
  6         GATE();
  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 #endif
  1 #include "GATE.h"
  2
  3 GATE::GATE()
  4 {
  5         A=B=X=false;
  6 }
  7
  8 bool GATE::getX()
  9 {
 10         return(X);
 11 }
 12
 13 void GATE::setA(bool A)
 14 {
 15         this->A=A;       //The first A is the class A, the second A is the parameter
 16 }
 17
 18 void GATE::setB(bool B)
 19 {
 20         this->B=B;
 21 }
 22
 23 void GATE::setX(bool X)
 24 {
 25         this->X=X;
 26 }
 27

Main

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include "GATE.h"
  4 #include "and.h"
  5 #include "or.h"
  6 #include "not.h"
  7 #include "nand.h"
  8 #include "nor.h"
  9
 10
 11 int main()
 12 {
 13     char menu;
 14     printf("1: And\n2: Or\n3: Not\n4: Nand\n5: Nor\n");
 15     scanf("%hhu", &menu);
 16
 17     switch (menu)
 18     {
 19     case 1:
 20         {
 21         AND *myAND = new AND();
 22         myAND->setA(true);
 23         myAND->setB(false);
 24         myAND->process();
 25         printf("Result is: %d\n", myAND->getX());
 26         }
 27         break;
 28     case 2:
 29         {
 30         OR *wowOR = new OR();
 31         wowOR->setA(true);
 32         wowOR->setB(false);
 33         wowOR->ORProcess();
 34         printf("Answer is: %d\n", wowOR->getX());
 35         }
 36         break;
 37     case 3:
 38         {
 39         NOT *coolNOT = new NOT();
 40         coolNOT->set(true);
 41         coolNOT->NOTProcess();
 42         printf("A is now: %d\n", coolNOT->get());
 43         }
 44         break;
 45     case 4:
 46         {
 47         NAND *whoaNAND = new NAND();
 48         whoaNAND->setA(true);
 49         whoaNAND->setB(true);
 50         whoaNAND->NANDProcess();
 51         printf("Result is: %d\n", whoaNAND->get());
 52         }
 53         break;
 54     case 5:
 55         {
 56         NOR *lazyNOR = new NOR();
 57         lazyNOR->setA(false);
 58         lazyNOR->setB(false);
 59         lazyNOR->NORProcess();
 60         printf("Result is: %d\n", lazyNOR->getX());
 61         }
 62         break;
 63     default:
 64         break;
 65     }
 66     return (0);
 67 }

Running Program

lab46:~/src/cprog/c++/logic$ g++ -c *.cc
lab46:~/src/cprog/c++/logic$ g++ -o program *.o
lab46:~/src/cprog/c++/logic$ ./program
1: And
2: Or
3: Not
4: Nand
5: Nor
1
Result is: 0
lab46:~/src/cprog/c++/logic$ ./program
1: And
2: Or
3: Not
4: Nand
5: Nor
5
Result is: 1
lab46:~/src/cprog/c++/logic$

Checking these results against the code will confirm that they are correct. :)

user/smalik2/portfolio/cprogbonusproject.txt · Last modified: 2012/05/09 20:01 by smalik2