#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
	int gdriver=9,gmode=2,q,x,y,x1,x2,y1,y2,f,c,c1,c2,xq,solc;
	float xd,xb,xf,rr,a,xx,yy,solm;
	clrscr();
	gotoxy(30,1);
	cout<<"McCABE - THIELE DIAGRAM "<<endl<<endl;
	do
	{
		cout<<"Enter the mole fraction of feed (Xf) : ";
		cin>>xf;
		if ((xf>=1)||(xf<=0))
			cout<<"Mole fraction of feed should lie between 0 and 1"<<endl;
	}
	while((xf>=1)||(xf<=0));
	do
	{
		cout<<"Enter the mole fraction of distillate (Xd) : ";
		cin>>xd;
		if ((xd>=1)||(xd<=xf))
			cout<<"Xd should lie between Xf and 1"<<endl;
	}
	while((xd>=1)||(xd<=xf));
	do
	{
		cout<<"Enter the mole fraction of bottom product (Xb) : ";
		cin>>xb;
		if ((xb>=xf)||(xb<=0))
			cout<<"Xb should lie between 0 and Xf"<<endl;
	}
	while((xb>=xf)||(xb<=0));
	do
	{
		cout<<"The feed is which of the following ? :"<<endl;
		cout<<"1.Saturated liquid\n2.Saturated Vapor\n3.Liquid-vapor 50% each : ";
		cin>>f;
	}
	while ((f<1) || (f>3));
	cout<<"Enter the reflux ratio (RR) : ";
	cin>>rr;
	cout<<"Enter the relative volatility (alpha) : ";
	cin>>a;
	initgraph(&gdriver,&gmode,"");
	line(0,450,400,450);
	line(0,450,0,50);
	line(0,450,400,50);
	for(x=40,y=40;x<=400,y<=400;x=x+40,y=y+40)
	{
		line(0,450-y,5,450-y);
		line(x,450,x,445);
	}
	xx=0;
	while(xx<=1)
	{
		yy=a*xx/(1+(a-1)*xx);
		x=400*xx;
		y=400*yy;
		putpixel(x,450-y,15);
		xx=xx+0.0025;
	}
	x2=xd*400;
	y2=x2;
	line(x2,450,x2,450-y2);
	x2=xb*400;
	y2=x2;
	line(x2,450,x2,450-y2);
	x2=xf*400;
	y2=x2;
	line(x2,450,x2,450-y2);
//to draw R.O.L.
	x1=xd*400;
	y1=xd*400;
	x2=0;
	y2=400*xd/(rr+1);
	line(x1,450-y1,x2,450-y2);
//to draw q-line
	c=y2;
	x1=xf*400;
	y1=xf*400;
	if (f==1)
	{
		x2=x1;
		y2=(xd*400-c)*x2/(xd*400)+c;
		line(x1,450-y1,x2,450-y2);
	}
	if (f==2)
	{
		y2=y1;
		x2=(y2-c)*xd*400/(xd*400-c);
		line(x1,450-y1,x2,450-y2);
	}
	if (f==3)
	{

		x2=(2*xf*400-c)*(xd*400/(xd*800-c));
		y2=y1+(x1-x2);
		line(x1,450-y1,x2,450-y2);
	}
	xq=x2;
//to draw S.O.L.
	line(xb*400,450-xb*400,x2,450-y2);
	x=x2;
	y=y2;
	solm=(y2-xb*400)/(x2-xb*400);
	solc=xb*400*(1-solm);
//to draw the plates
	c1=0;
	c2=0;
	setcolor(BLUE);
	x1=xd*400;
	y1=xd*400;
	do
	{
		c1=c1+1;
		if (x1>xq)
		{
			if (c1%2==1)
			{
				y2=y1;
				yy=y2;
				yy=yy/400;
				xx=yy/(a-yy*(a-1));
				x2=xx*400;
				line(x1,450-y1,x2,450-y2);
				c2=c2+1;
			}
			if (c1%2==0)
			{
				x2=x1;
				y2=(xd*400-c)/(xd*400)*x2+c;
				line(x1,450-y1,x2,450-y2);

			}
		}
		if (x1<=xq)
		{
			if (c1%2==1)
			{
				y2=y1;
				yy=y2;
				yy=yy/400;
				xx=yy/(a-yy*(a-1));
				x2=xx*400;
				line(x1,450-y1,x2,450-y2);
				c2=c2+1;
			}
			if (c1%2==0)
			{
				x2=x1;
				y2=solm*x2+solc;
				line(x1,450-y1,x2,450-y2);

			}
		}
		x1=x2;
		y1=y2;
	}
	while(x1>xb*400);
	getch();
	closegraph();
	gotoxy(19,13);
	cout<<"The theoretical number of plates required is : "<<c2;
	gotoxy(18,20);
	cout<<"Coming soon ...... more chemical engg software !!!";
	getch();
}

