L.sha 0 Newbie Poster

Hey,
I Have a code to implement digital logic circuits , using linked-list. I have managed to build a simple program to work on 3 gates, which gives the user the option to choose between the gates and enter the kind of operation he wants and the values also. The problem now is I want to build on this code for more than 2 gates in the first time, I can limit the user to choose a particular number of gates in each level. But I am very confused on how to extend this program, as I do not want to use alot of switch and if else cases in the main to extend this program. Here is the basic code I have worked on and its working :

#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

struct link{
	int d1;
	int d2 ; 
	
    link* next ; 
} ; 


///////////////////////

class linklist 
{
private :
	link* first ; 
public : 
	linklist () 
	{
		first = NULL ; 
	} 
	~linklist () ; 
	void additem(int d1,int d2 ) ; 
	int andGate(int x, int y);
	int orGate(int w, int z);
	int notGate(int p);
	int nandgate(int x1, int x2);
	int norgate (int w1, int w2);
	
} ; 

/////////////////////

void  linklist :: additem(int d1,int d2) 
{ 
	link* newlink = new link ; 
	newlink ->  d1 ; 
	newlink ->  d2 ; 
	newlink -> next = NULL ; 
	if ( first == NULL ) 
		first = newlink ; 
	else { 
		link* current = first ; 
		while ( current -> next != NULL ) 
			current = current -> next ; 
		current -> next = newlink ; 
	}
}

//////////////////

int linklist::andGate(int x, int y){

	if (x==1&&y==1)
		return 1;
	else 
		return  0;
}

int linklist::orGate(int w, int z){

	if(w==1 ||z==1)
		return 1;
	else
		return 0;
	
}
int linklist::notGate(int p)
{
	if (p==1)
		return 0;
	else
		return 1;
}
int linklist::nandgate(int x1, int x2){
	if (x1 ==1 && x2 == 1)
		return 0;
	else return 1;
}

int linklist::norgate(int w1, int w2)
{
	if(w1==1 ||w2==1)
		return 0;
	else
		return 1;
}
linklist::~linklist()
{
	link* current = first ; 
	while ( current != NULL) 
	{
		link* temp = current ; 
		current = current -> next ; 
		delete temp ; 
	}
}

/////////////////

int main () 
{
	linklist gate;
	int a, b, c ,d;
	int op1, op2; 
	int num1;
	int num2;
	int num3;
	int num4;
	int r1;
	int r2;
	int r3;
	int r4;
	cout<< "Please enter the number corresponding to the gate to choose your gate : "<<endl;
	cout<<"1) AND"<<endl<<"2) OR"<<endl<<"3) NAND"<<endl<<"4) NOR"<<endl;
	cin>>num1;
	cout<<endl;

	cout<<"Please choose the operation you want to perform by entering the corresponding number :"<<endl;
	cout<<" 1) addition"<<endl<<"2) multipication";
	cin>>num2;
	cout<<endl;

	cout<< "Please enter the number corresponding to the gate to choose your second gate : "<<endl;
	cout<<"1) AND"<<endl<<"2) OR"<<endl<<"3) NAND"<<endl<<"4) NOR"<<endl;
	cin>>num3;
	cout<<endl;

	cout<<"Please enter the values for gate 1 :";
	cin>>a>>b;
	cout<<endl;
	cout<<"Please enter the values for gate 2 :";
	cin>>c>>d;
	cout<<endl;

	switch(num1){
		case 1:
			r1 = gate.andGate(a ,b);
			break;
		case 2:
			r1 = gate.orGate(a,b);
			break;
		case 3:
			r1 = gate.nandgate(a,b);
			break;
		case 4:
			r1 = gate.norgate(a,b);
			break;
			return r1;
	}
	switch(num3){
		case 1:
			r2 = gate.andGate(c ,d);
			break;
		case 2:
			r2 = gate.orGate(c,d);
			break;
		case 3:
			r2 = gate.nandgate(c,d);
			break;
		case 4:
			r2 = gate.norgate(c,d);
			break;
			return r2;
	}
	switch(num2){
		case 1:
			r3 = gate.orGate(r1, r2);
			break;
		case 2:
			r3= gate.andGate(r1, r2);
			break;
			return r3;
}
	cout<<"The final answer is:" << r3<<endl;
	cout<<"Do you want to Not or invert your output:"<<endl;
	cout<<"Enter 1 for yes" <<endl<< "Enter 2 for no"<<endl;
	cin>>num4;
	
	if (num4 == 1){
		r4 = gate.notGate(r3);
		cout<<"After inverting the output is : "<< r4;}
	else
		cout<<"The answer remains : "<< r3;


	getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.