/*This program simulates Buffon's Needle. Buffon's Needle appoximates the value
	of PI (you know 3.141528...) by dropping a needle between two lines
	and counting the number of times the the needle has crossed the line
	as opposed to landing between the lines.
*/

/*variables
	cnod (current number of drops)
	tnod (total number of drops)
	con  (center of needle.A random value between 0 and 1)
 	aon  (angle of needle. a random value between 0 and pi)
	nld  (needle line distance. Distance from center of needle to nearest
	      line)
	ctd  (center tip distance. Vertical distance from center of needle 
	      to tip of needle.)
	pia  (pi approximation)
	cnoh (counter that counts the number of times the needle hits the line
*/

#include<iostream.h>
#include<fstream.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
	//declare variables
	time_t tloc; //needed to seed random number generator
	int cnod,tnod;
	int cnoh= 0;
	float con,aon,nld,ctd,pia;
	char choice, choice2;
	char filename[20];
	//seed random number generator
	srand(int(time(&tloc)));
	//output file handle
	ofstream OutFile;


	cout << "\nHow many needle drops do you want? ";
	cin >> tnod;

	do{
       	  cout <<"Do you want to see (a)ll output or (f)inal output only?";
	  cout <<"\nEnter a or f  ";
	  cin >> choice;
       	}while(choice!='a' && choice!='A' && choice!='f' && choice !='F');

	do{
	  cout <<"Do you want the output to (s)screen or (f)ile?";
	  cin >> choice2;
	}while(choice2!='s' && choice2!='S' && choice2!='f' && choice2 !='F');

	//Used for output to file
	if (choice2 == 'f')
	{
		cout << "Enter the OUTPUT FILE NAME ";
		cin  >> filename;
		OutFile.open(filename);
		if(!OutFile)
		{
			cerr << "Can't open buffon.dat file";
			exit(1);
		}
	}
	for (cnod=1;cnod<=tnod;cnod++)
	  {
	  	con=float(rand()%10000)/10000;           //get center of needle
		aon=float(rand()%10000)/10000*3.141528;  //get angle of needle

		//calculate distance of center of needle to nearest line
		if(con <= .5)
			{nld = con;}
		else
			{nld = 1 - con;}

	        //calculate distance from center of needle to tip of needle
		ctd = (.5*sin(aon));

		//determine and count if needle crosses the line
		if(nld <= ctd)
		{cnoh=cnoh+1;}	

		//Calculate PI approximation
		if(cnoh!=0)
		{pia = 2*float(cnod)/cnoh;}
		
	  	//OUTPUT	
	        //print the pi approximation for each drop
		if(choice == 'a' || choice == 'A')
		{
		    if(choice2=='s'|| choice2 == 'S')
			{cout<<cnod<<"="<<pia<<"   ";}
		    else if (choice2 =='f' || choice2 == 'F')
			{OutFile << cnod << "   " << pia << endl;}
		}//end if
	  }//end  for loop

	  //print the pi approximation for ONLY the final result
	  if(choice == 'f' || choice == 'F')
	  {
		if (choice2 == 's' || choice2 == 'S')
		{
	   	 cout << "\n\nTotal Drops ="<<tnod;
		 cout << " PI approximation ="<<pia<<endl;
		}
		else if(choice2 == 'f' || choice2 == 'F')
		{	
	   	  OutFile << "\n\nTotal Drops ="<<tnod;
		  OutFile << " PI approximation ="<<pia<<endl;
		}//end else
	  }//end if
	
	if(choice2 == 'f')	
	   OutFile.close();
return(0);
}//end main
