⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 digital.cpp

📁 SIMPLE SIMULATOR FOR DIGITAL CIRCUITS
💻 CPP
字号:
#include <stdio.h>
#include <conio.h>
struct node
	{
	int i1;
	int i2;
	int f;
	int o;
	int u;
	};
struct node arr[20];
void and(int k);
void or(int k);
void invert(int k);
void nand(int k);
void nor(int k);
void xor(int k);
void xnor(int k);

int iop[30];
int s[30];

main()
{
int i,j,p,choice,cho,y;
char ch;
clrscr();
for(i=0;i<30;i++)
	s[i]=0;
for(i=0;i<20;i++)
	{
	arr[i].i1=0;
	arr[i].i2=0;
	arr[i].f=0;
	arr[i].u=0;
	arr[i].o=0;
	}
for(i=0;i<30;i++)
	{
	iop[i]=0;
	}
for(;;)
{
clrscr();

for(i=0;i<20;i++)
    {
    arr[i].i1=0;
    arr[i].i2=0;
    arr[i].o=0;
    arr[i].u=0;
    arr[i].f=0;
    };
printf("\n\n\n");
printf("\t\t ");
printf("WELCOME TO DIGITAL SIMULATOR");
printf("\n\n\t your choice 0:to quit ,1: to continue with the simulation :");
scanf("%d",&choice);
if(choice==0)
	break;
clrscr();
printf("\n\n\n");
printf("\t THE FOLLOWING GATE ARE CURRENTLY AVAILBLE    \n");
printf("\t  1. AND \n");
printf("\t  2. OR  \n");
printf("\t  3. INVERTOR \n");
printf("\t  4. NAND  \n");
printf("\t  5. NOR   \n");
printf("\t  6. XOR    \n");
printf("\t  7. EXCLUSIVE NOR  \n");
printf("\t  0. CIRCUIT COMPLETED \n");
printf("\t ENTER YOUR CHOICES FOR CREATING THE SIMULATION \n");
printf("\t note:you have 30 pin positions availble where you can connect inputs \n\tand outputs\n\t");
for(i=0;i<30;)
{

printf("\t enter the code for the gate as shown above\n\t\t");
scanf("%d",&j);
if(j==0)
  break;
else if(j==3)
	{
	int k,l;
	arr[i].f=3;
	printf("\t enter the input position (between 0 to 30): ");
	scanf("%d",&k);
	printf("\t enter the output position (between 0 to 30): ");
	scanf("%d",&l);
	arr[i].i1=k;
	arr[i].o=l;
	arr[i].u=1;
	i++;
	}
else
	{
	int k,l,m;
	arr[i].u=1;
	arr[i].f=j;
	printf("\t enter the input position 1(0 to 30): ");
	scanf("%d",&k);
	printf("\t enter the input position 2(0 to 30): ");
	scanf("%d",&l);
	printf("\t enter the output position (0 to 30): ");
	scanf("%d",&m);
	arr[i].i1=k;
	arr[i].i2=l;
	arr[i].o=m;
	i++;
	}
}
clrscr();
for(;;)
{
for(y=0;y<30;y++)
	s[y]=0;

printf("\n\n\t\t NOW YOU HAVE TO ENTER THE INPUTS \n ");

for(;;)
{
int k,l;
printf("enter the input positions (0 to 29),more than 29 when entering input positions completed): \n\t\t\t");
scanf("%d",&k);
if(k>=29)
	break;
printf("now enter the logic input at position %d (0 or 1) : \n\t\t\t",k);
scanf("%d",&l);
iop[k]=l;
};
clrscr();
printf("\tNOW ENTER THE POSITIONS AT WHICH YOU WISH TO SEE THE LOGIC LEVELS\n");
printf(" enter a value  greater than 29 when you are done ");
for(i=0;i<=29;i++)
{
	int k;
	printf("\t");
	scanf("%d",&k);
	if(k>29)
		break;
	else
		s[k]=1;
};
for(p=0;p<4;p++)
{
for(i=0;(i<30)&&(arr[i].u==1);i++)
{
if(arr[i].f==1)
   and(i);
if(arr[i].f==2)
   or(i);
if(arr[i].f==3)
  invert(i);
if(arr[i].f==4)
  nand(i);
if(arr[i].f==5)
  nor(i);
if(arr[i].f==6)
  xor(i);
if(arr[i].f==7)
  xnor(i);
}
}
clrscr();
printf("\n \t\t THE RESULT OF THE SIMULATION IS FOLLOWING\n");
for(i=0;i<30;i++)
{
	if(s[i]==1)
	    {
	    printf("\n\tthe logic level at position %d :%d",i,iop[i]);
	    }

};
printf("\n enter your choice \n\t0:to simulate another circuit");
printf("\n\t1:enter different inputs into the same circuit:");
scanf("%d",&cho);
if(cho==0)
	break;
clrscr();
}
};
printf("\n\n\t\tthank you ,you are quitting   press any key to quit");
getch();
return 0;
}
void and(int k)
{
iop[arr[k].o]=iop[arr[k].i1]*iop[arr[k].i2];
}
void or(int k)
{
iop[arr[k].o]=iop[arr[k].i2]+iop[arr[k].i1];
}
void invert(int k)
{
if(iop[arr[k].i1]==0)
       iop[arr[k].o]=1;
else
       iop[arr[k].o]=0;
}
void nand(int k)
{
if((iop[arr[k].i1]*iop[arr[k].i2]==0))
	iop[arr[k].o]=1;
else
	iop[arr[k].o]=0;
}
void nor(int k)
{
if(iop[arr[k].i1]+iop[arr[k].i2]==0)
	iop[arr[k].o]=1;
else
	iop[arr[k].o]=0;
}
void xor(int k)
{
if(((iop[arr[k].i1]==1)&&(iop[arr[k].i2]==0))||((iop[arr[k].i1]==0)&&(iop[arr[k].i2]==1)))
	iop[arr[k].o]=1;
else
	iop[arr[k].o]=0;
}
void xnor(int k)
{
xor(k);
if(iop[arr[k].o]==0)
	iop[arr[k].o]=1;
else
	iop[arr[k].o]=0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -