#include <iostream>
#include <fstream>
#include <windows.h>
#include <stdlib.h>
#include <cmath>
#include <string>
void Go_To(int a,int b);
using namespace std;
int main()
{
	int I = 1;
	string ch;
	double A, B, C, Disc, Root1, Root2;
	do
	{
		system("cls"); system("color 31"); cout <<"\t";
		cout << "Enter A, B, and C "
			 << "to solve: A x^2+ B x + C = 0."
			 << endl;
	Go_To(5,5);  cout << "Enter A: "; 
	Go_To(25,5); cout << "Enter B: ";
	Go_To(45,5); cout << "Enter C: "; 
	Go_To(10,10); cout << "Root1: ";
	Go_To(10,15); cout << "Root2: ";
	
	//Go_To(15,4); cout <<"-------";
	//Go_To(15,5); cout <<"|";
	//Go_To(21,5); cout <<"|";
	Go_To(15,6); cout <<"-----";

	Go_To(15,5); cin >> A; 
	if ( !cin )
	{
		Go_To(20,20);
		cout << "You've entered invalid input."
			 << endl;
		Go_To(20,21);
		cout << "The program is terminated."
			 << endl;
		Go_To(20,22);
		//system("Quad_Eq_Goto.exe");
		return 1;
	}
	Go_To(15,6); cout <<"     ";
	Go_To(35,6); cout <<"-----";
	Go_To(35,5); cin >> B; 
	if ( !cin )
	{
		Go_To(20,20);
		cout << "You've entered invalid input."
			 << endl;
		Go_To(20,21);
		cout << "The program is terminated."
			 << endl;
		Go_To(20,22);
		return 1;
	}
	Go_To(35,6); cout <<"     ";
	Go_To(55,6); cout <<"-----";
	Go_To(55,5); cin >> C;
	if ( !cin )
	{
		Go_To(20,20);
		cout << "You've entered invalid input."
			 << endl;
		Go_To(20,21);
		cout << "The program is terminated."
			 << endl;
		Go_To(20,22);
		return 1;
	}
	Go_To(55,6); cout <<"     ";
	// Compute the discriminant.
	Go_To(0,0); 
	cout << "                                     ";
	cout << "                   ";
	Disc = B * B - 4 * A * C;
	if (Disc > 0)
	{
		// Two distinct real roots.
		// Calculate the roots.
		Root1 = (-1.0 * B + sqrt(Disc))/(2.0 * A);
		Root2 = (-1.0 * B - sqrt(Disc))/(2.0 * A);
		// Display the roots.
		Go_To(20,10); cout << Root1;
		Go_To(20,15); cout << Root2;
	}
	else if (Disc == 0)
	{
		// One repeated real root.
		// Calculate the root.
		Root1 = (-1.0*B)/(2.0 * A);
		// Display the root.
		Go_To(20,10); cout << Root1;
		Go_To(20,15); cout << Root1;
	}
	else
	{
		// Two complex roots.
		// Calculate and display the first root.
		Go_To(20,10); 
		cout << (-1.0 * B)/(2.0 * A)
			 << " + " 
			 << sqrt(fabs(Disc)) / (2.0 * A)
			 << " i";
		// Calculate and display the second root.
		Go_To(20,15);
		cout << (-1.0 * B)/(2.0 * A)
			 << " - " 
			 << sqrt(fabs(Disc)) / (2.0 * A)
			 << " i";
	}
	/* Go_To(11,19);
	for (int j=1; j<=48; j++)
		cout <<"*";
	//Go_To(19,20); cout << "*";
	Go_To(12,20); cout << "To Re-enter: R";
	Go_To(32,20); cout << "| To Exit: E";
	Go_To(44,20); cout << "| To Print: P";
	Go_To(56,20); cout << "| To Save: S";
	Go_To(11,21);
	for (j=1; j<=48; j++)
		cout <<"*"; */
	Go_To(6,19);
	cout << "**********************************************************";
	Go_To(6,20);
	cout << "* To Re-enter: R | To Exit: E | To Print: P | To save: S *"; 
	Go_To(6,21);
	cout << "**********************************************************";
	//Go_To(41,20); cout << "*";
Label1:
	Go_To(36,23); cout << "-";
	Go_To(36,22);cin>>ch;
	if ((ch=="e")||(ch=="E"))
	{
		Go_To(36,23); cout << " ";
		Go_To(1,22);
		cout << "\t \t \t";
		return 0;
	}
	else if ((ch=="s") || (ch == "S"))
	{
		string FileName;
		ofstream outFile;
		Go_To(20,17);
		cout << "Enter the name of the file: ";
		cin >> FileName;
		Go_To(20,17); cout << "Done"; 
		outFile.open(FileName.c_str());
		outFile<<"The solutions of "<< A << "x^2 + " 
			   << B << " x + " << C << " are: "<<endl;
		if (Disc > 0)
		{
			outFile << Root1 << endl;
			outFile << Root2 << endl;
		}
		else if (Disc == 0)
		{
			outFile << Root1 << endl;
			outFile << Root1 << endl;	
		}
		else
		{
			outFile << (-1.0 * B)/(2.0 * A)
				    << " + " 
				    << sqrt(fabs(Disc)) / (2.0 * A)
				    << " i" << endl;
			outFile << (-1.0 * B)/(2.0 * A)
				    << " - " 
				    << sqrt(fabs(Disc)) / (2.0 * A)
				    << " i" << endl;

		}
		outFile.close();
		Go_To(20,17);
		cout << "                                            ";
		Go_To(36,22); cout << " ";
		goto Label1;
	}
	else if ((ch=="p") || (ch=="P"))
	{
		ofstream print; 	// Stream variable declaration 
		print.open("LPT1"); 	// Open stream (Prepare the printer for printing)
	
		print<<"The solutions of "<< A << "x^2 + " 
			 << B << " x + " << C << " are: "<<endl;
		if (Disc > 0)
		{
			print << Root1 << endl;
			print << Root2 << endl;
		}
		else if (Disc == 0)
		{
			print << Root1 << endl;
			print << Root1 << endl;	
		}
		else
		{
			print << (-1.0 * B)/(2.0 * A)
				  << " + " 
				  << sqrt(fabs(Disc)) / (2.0 * A)
				  << " i" << endl;
			print << (-1.0 * B)/(2.0 * A)
				  << " - " 
				  << sqrt(fabs(Disc)) / (2.0 * A)
				  << " i" << endl;

		}


		print <<'\f';	// Do not forget this line.
		print.close();	// Tell the printer there is nothing left to print.
		Go_To(36,22); cout << " ";
		goto Label1;
	}


	else if ((ch.length() != 1) || ((ch != "r") && (ch != "R"))) 
	{
		//Go_To(36,22); 
		Go_To(36,22); cout << "       ";
		goto Label1;
	}
	} // do while ends here.
	while ((ch == "R") || (ch == "r"));
	/*while (I <= 2)
	{
		cout << endl;
		I++;
	}
	cout << "\t";*/
	return 0;
}

void Go_To(int a,int b)
{
	COORD S;
	HANDLE T;
	S.X = a; S.Y = b;
	T = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(T,S);
}

