//main.cpp #include #include #include #include #include #include"mouse.h" #include"digital.h" #include"dia.h" #define CKT_X (30) #define CKT_Y (30) int run_flag=0; int open_flag=0; class Button { int x; int y; int h; int w; int color; char *text; int (*f)(); public: Button(int a,int b,int c,int d,int col) { x=a; y=b; w=c; h=d; color=col;} void setText(char str[]) { text=str; } void setFunction(int (*func)()) { f=func; } void show(int val) { hidemouse(); setcolor(color); rectangle(x,y,x+w,y+h); setfillstyle(1,0); setcolor(0); bar(x+1,y+1,x+w-1,y+h-1); if (val==1) setcolor(RED); else setcolor(color); int px=x+(w-textwidth(text))/2; int py=y+(h-textheight(text))/2; outtextxy(px,py,text); showmouse(); } int isClicked(int a,int b) { int btn=mousebtn(); if ((x<=a)&&(y<=b)&&(x+w>=a)&&(y+h>=b)) { show(btn); if (btn==1) { if ((*f)()) show(0);} return(1); } return(0); } }; file filename; void connector(); void rewire(); void cktdelete(int); int fOpen(); int fSave(); int fRun() { run_flag=1; setcolor(GREEN); setfillstyle(1,GREEN); circle(60,getmaxy()-10,5); floodfill(60,getmaxy()-10,GREEN); return(0); } int fStop() { run_flag=0; setcolor(RED); setfillstyle(1,RED); circle(60,getmaxy()-10,5); floodfill(60,getmaxy()-10,RED); rewire(); return(0); } int fClear(); void fMove(int); Gate *cktStore[200]; int cktcount=0; int currGate=-1; Led *ledStore[100]; int ledcount=0,ledindex[200]; Switch *switchStore[100]; int switchcount=0,switchindex[200]; void main(int argc,char *argv[]) { int gd=DETECT,gm,ox=120,oy,i,j; Gate *m_gate[8]; initgraph(&gd,&gm,"."); Button clear(ox,getmaxy()-20,60,20,BLUE); clear.setText("Clear"); clear.setFunction(fClear); clear.show(0); ox+=60; Button open(ox,getmaxy()-20,60,20,BLUE); open.setText("Open"); open.setFunction(fOpen); open.show(0); ox+=60; Button save(ox,getmaxy()-20,60,20,BLUE); save.setText("Save"); save.setFunction(fSave); save.show(0); ox+=60; Button run(ox,getmaxy()-20,60,20,BLUE); run.setText("Run"); run.setFunction(fRun); run.show(0); ox+=60; Button stop(ox,getmaxy()-20,60,20,BLUE); stop.setText("Stop"); stop.setFunction(fStop); stop.show(0); ox=getmaxx()-25; oy=25; setcolor(DARKGRAY); rectangle(getmaxx()-60,0,getmaxx(),getmaxy()); setcolor(YELLOW); outtextxy(getmaxx()-50,20,"GATES"); setcolor(RED); rectangle(0,0,getmaxx()-70,getmaxy()-30); for(i=0;isetPosition(ox,oy); m_gate[0]->draw(); //Nand oy+=50; m_gate[1]=new Nand(2); m_gate[1]->setPosition(ox,oy); m_gate[1]->draw(); //Or Gate oy+=50; m_gate[2]=new Or(2); m_gate[2]->setPosition(ox,oy); m_gate[2]->draw(); //Nor oy+=50; m_gate[3]=new Nor(2); m_gate[3]->setPosition(ox,oy); m_gate[3]->draw(); //Not Gate oy+=50; ox-=5; m_gate[4]=new Not(); m_gate[4]->setPosition(ox,oy); m_gate[4]->draw(); //Led oy+=50; ox+=15; m_gate[5]=new Led(); m_gate[5]->setPosition(ox,oy); m_gate[5]->draw(); //Switch oy+=50; ox-=10; m_gate[6]=new Switch(); m_gate[6]->setPosition(ox,oy); m_gate[6]->draw(); //Connector oy+=50; ox-=5; Connector *c=new Connector; m_gate[7]=c; c->setIn(ox-15,oy-15); c->setOut(ox+15,oy+15); m_gate[7]->draw(); setcolor(BROWN); outtextxy(0,getmaxy()-14,"Status"); fStop(); open_flag=1; for(i=1;iisInside(mx,my))&&(run_flag==0)) { //gate creator routine if (cur) {int num; hidemouse(); setcolor(YELLOW); rectangle(getmaxx()-59,i*50+51,getmaxx()-1,i*50+99); showmouse(); if ((i>=0)&&(i<=3)) { num=getInt(); if (num==0) continue; } switch(i) { case 0: cktStore[cktcount++]=new And(num); break; case 1: cktStore[cktcount++]=new Nand(num); break; case 2: cktStore[cktcount++]=new Or(num); break; case 3: cktStore[cktcount++]=new Nor(num); break; case 4: cktStore[cktcount++]=new Not; break; case 5: Led *l=new Led; ledindex[cktcount]=ledcount; cktStore[cktcount++]=l; ledStore[ledcount++]=l; break; case 6: Switch *s=new Switch; switchindex[cktcount]=switchcount; cktStore[cktcount++]=s; switchStore[switchcount++]=s; break; default: break; } if (i==7) { connector(); rewire(); } else { cktStore[cktcount-1]->setPosition(CKT_X,CKT_Y); cktStore[cktcount-1]->draw(); break; } }//if mouse == 1 }//if any gate is clicked }//for all gates in the menu if (cur) { currGate=-1; if (run_flag==0) { for(int i=cktcount-1;i>=0;i--) { if (cktStore[i]->isInside(mx,my)) { currGate=i; fMove(currGate); rewire(); break; } }//for } if (run_flag==1) { hidemouse(); for(i=0;iisInside(mx,my)) switchStore[i]->toggle(); switchStore[i]->draw(2); } showmouse(); } }//if (cur) }//if clicked prev=cur; if (kbhit()) in=getch(); if (in==27) break; if ((in==83)&&(!run_flag)) { cktdelete(currGate); rewire(); } if (run_flag==1) { //hidemouse(); for(int i=0;ireset();} for(i=0;igetOutput();} //showmouse(); } }//while true for(i=0;i<7;i++) delete(m_gate[i]); for(i=0;iox-x; int dy=cktStore[i]->oy-y; if (cktStore[i]->type==7) { dsx=cktStore[i]->startx-x; dsy=cktStore[i]->starty-y; dex=cktStore[i]->endx-x; dey=cktStore[i]->endy-y; } setviewport(0,0,getmaxx()-70,getmaxy()-30,1); setfillstyle(1,0); hidemouse(); while(mousebtn()) { x=near5(mousex()); y=near5(mousey()); if ((x!=prevx)||(y!=prevy)) { cktStore[i]->draw(0); cktStore[i]->setPosition(x+dx,y+dy); if (cktStore[i]->type==7) { cktStore[i]->startx=x+dsx; cktStore[i]->starty=y+dsy; cktStore[i]->endx=x+dex; cktStore[i]->endy=y+dey; } setcolor(RED); rectangle(0,0,getmaxx()-70,getmaxy()-30); for(int k=0;kdraw(); } prevx=x; prevy=y; } setviewport(0,0,getmaxx()-70,getmaxy()-30,0); showmouse(); } void rewire() { Gate *in, *out; hidemouse(); setcolor(RED); setfillstyle(1,0); bar(1,1,getmaxx()-71,getmaxy()-31); rectangle(0,0,getmaxx()-70,getmaxy()-30); for(int k=0;kdraw(); setcolor(RED); setfillstyle(1,RED); for(int i=0;iresetConn(); for(int j=0;jgetO(); if (in->isInput(out->getO())) { in->setInput(out); bar(p.x-1,p.y-1,p.x+1,p.y+1); } }// for j }// for i showmouse(); }//rewire void connector() { int btn,prev; int count=0; int x[2],y[2]; btn=prev=mousebtn(); while(1) { btn=mousebtn(); if (btn!=prev) { if (count==0) { hidemouse(); setcolor(0); rectangle(getmaxx()-59,7*50+51,getmaxx()-1,7*50+99); showmouse(); } else { if ((mousex()>getmaxx()-70)||(mousey()>getmaxy()-30)) return; x[count-1]=near5(mousex()); y[count-1]=near5(mousey()); } count++; } if (count==3) break; prev=btn; } for(int i=0;i<2;i++) { Gate *g; Point p,q; int j=1-i; for(int k=0;kisInput(p)) { Connector *c=new Connector(); c->setIn(x[j],y[j]); c->setOut(x[i],y[i]); cktStore[cktcount++]=c; hidemouse(); c->draw(1); showmouse(); return; } q=g->getO(); if ((p.x==q.x)&&(p.y==q.y)) { Connector *c=new Connector(); c->setIn(x[i],y[i]); c->setOut(x[j],y[j]); cktStore[cktcount++]=c; hidemouse(); c->draw(1); showmouse(); return; } } } } void cktdelete(int n) { if (n==-1) return; if (cktStore[n]->type==5) { int nindex=ledindex[n]; for(int i=nindex;itype==6) { int nindex=switchindex[n]; for(int i=nindex;isetIn(sx,sy); c->setOut(ex,ey); cktStore[cktcount++]=c; break; default: break; } cktStore[cktcount-1]->setPosition(ox,oy); cktStore[cktcount-1]->draw(); }//while rewire(); showmouse(); fclose(fp); filename.name[0]='\0'; return(1); } int fSave() { int flag=0; if (cktcount==0) { dia("No circuit to Save"); return(1);} if (strlen(filename.name)==0) { filename=getfile(); flag=1; } if (strlen(filename.name)==0) { return(flag); } FILE *fp=fopen(filename.name,"w"); if (!fp) { dia("File could not be Saved"); return(flag); } for(int i=0;itype,g->ox,g->oy,g->n); if (g->type==7) fprintf(fp,"%d %d %d %d\n",g->startx,g->starty,g->endx,g->endy); } fclose(fp); char msg[100]; sprintf(msg,"Circuit saved in File \"%s\".",filename.name); dia(msg); return(1); }