// Copyright 1999
// College of Computer Science
// Northeastern University Boston MA 02115

// This software may be used for educational purposes as long as this copyright
// notice is retained at the top of all files

// Should this software be modified, the words "Modified from Original" must be
// included as a comment below this notice

// All publication rights are retained.  This software or its documentation may
// not be published in any media either in whole or in part.

///////////////////////////////////////////////////////////////////////////////

//	Recitation 2:	Using and writing simple functions; Conversion pattern

//	STUDENT NAME:	¥?¥?¥
//	STUDENT ID:		¥?¥?¥
//	DATE:			¥?¥?¥

///////////////////////////////////////////////////////////////////////////////

// The standard include files that include traditional C and C++ headers and
// many other Core Tools headers ... see CHeaders.h for additional details

#include "IOTools.h"
#include "Graphics.h"
#include "Random.h"

// Enter project specific include files here as well as classes and functions
// that you choose to define in the main shell rather than in separate files

// prototypes

void Exercise1();
void Exercise2();
void Exercise3();
void Exercise4();

void Doughnut(int xcenter, int ycenter, int size, int thickness);
void Exercise5();

void PaintWindow(int left, int top, int height, int width);
void Exercise6();

void CountCoins1(int q,int d, int n, int p);
void Exercise7();

int CountCoins2(int q,int d, int n, int p);
void Exercise8();

void ConvertMoney(int totalCents, int& wholeDollars, int& fractionCents);
void Exercise9();

int ConvertToMinutes(int hours, int minutes);
void ConvertMinutesToHM(int time, int& hours, int& minutes);
void Exercise10();

 
// definitions

void Exercise1(){
// Inside a supplied loop, read x, y, radius, frame a circle 
	
	ClearDrawing();
	cout << "Frame several circles - user selects and size" << endl << endl;

	while (Confirm("Another circle?", true)){
	
		// code starts here
		cout << "Specify next circle to frame." << endl;
		int x = RequestInt("x coordinate of the center:");
		int y = RequestInt("y coordinate of the center:");
		int radius = RequestInt("radius:");

		FrameCircle(x, y, radius);
			
		// code ends here
	}	
};

void Exercise2(){
// Modify the program so it reads two radii: inside and outside radius of a doughnut
// Paint the outside circle and invert the inside circle

	ClearDrawing();
	SetForeColor(255, 255, 0);

	cout << "Painting doughnuts given inside and outside radius" << endl << endl;

	while (Confirm("Another doughnut?", true)){
	
		// code starts here
		int x = RequestInt("x coordinate of the center:");
		int y = RequestInt("y coordinate of the center:");
		int outradius = RequestInt("outside radius:");
		int inradius = RequestInt("inside radius:");
		
		PaintCircle(x, y, outradius);
		EraseCircle(x, y, inradius);
		
		// code ends here
	}
};

void Exercise3(){
// Modify the program so that it reads the outside radius and the thickness of the doughnut

	ClearDrawing();
	SetForeColor(255, 0, 255);

	cout << "Painting doughnuts given outside radius and thickness" << endl << endl;

	while (Confirm("Another doughnut?", true)){
	
		// code starts here
		int x = RequestInt("x coordinate of the center:");
		int y = RequestInt("y coordinate of the center:");
		int radius = RequestInt("radius of the doughnut:");
		int thickness = RequestInt("thickness of the doughnut:");
		
		PaintCircle(x, y, radius);
		EraseCircle(x, y, radius - thickness);
		
		// code ends here
	}
};

void Exercise4(){
// Given the top left corner, the width, and the height paint a window. 
// Window will consist of a rectangular frame and two dividing lines through the middle 
// (one vertical and one horizontal). 
// It makes a window with four window panes

	ClearDrawing();

	cout << "Paint a window with four window panes" << endl << endl;

	while (Confirm("Another window?", true)){
	
		// code starts here
		SetFillColor(0, 0, 255);					// make the window blue

		int x = RequestInt("x coordinate of the left edge:");
		int y = RequestInt("y coordinate of the top edge:");
		int height = RequestInt("height of the window:");
		int width = RequestInt("width of the window:");
		
		FrameRect(x, y, x + width, y + height);
		DrawLine(x + width/2, y, x + width/2, y + height);
		DrawLine(x, y + height/2, x + width, y + height/2);
	
		// code ends here
	}

};

void Doughnut(int xcenter, int ycenter, int size, int thickness){
// Define a function Doughnut that uses four arguments: 
// the x and y coordinates of the center, the size and the thickness
//
// Input: 			User supplies the coordinates of the center, 
//					the outside radius and the thickness of the doughnut
// Return values: 	None
// Side Effect:		A doughnut shape is painted in the current background color

	PaintCircle(xcenter, ycenter, size);
	InvertCircle(xcenter, ycenter, size - thickness);	
};

void Exercise5(){
// Test Doughnut function

	ClearDrawing();
	SetForeColor(255, 255, 0);

	cout << "Painting doughnuts using Doughnut(...) function" << endl << endl;

	while (Confirm("Another doughnut?", true)){
	
		// code starts here
		int x = RequestInt("x coordinate of the center:");
		int y = RequestInt("y coordinate of the center:");

		int radius = RequestInt("radius of the doughnut:");
		int thickness = RequestInt("thickness of the doughnut:");
		
		Doughnut(x, y, radius, thickness);
	
		// code ends here
	}

};


void PaintWindow(int left, int top, int height, int width){
// Define a function PaintWindow, given its top left corner, its height and its width. 
// It is a framed rectangle with a vertical and horizontal line through the middle. 
// You may choose to paint the rectangle with some color, then frame it in black
//
// Input: 			User supplies the coordinates of the top left corner of the window
//        			and its height and width
// Return values: 	None
// Side Effect:		A window shape is painted in light blue color
//					(framed rectangledivided into four parts
	
	SetForeColor(100, 100, 255);
	PaintRect(left, top, left + width, top + height);
	
	SetForeColor(0, 0, 0);
	SetPenSize(2, 2);
	
	FrameRect(left, top, left + width, top + height);
	
	// vertical line at the distance left + width/2 from the edge of drawing window
	// going from 'top' to 'top + height of this window - the size of the pen'
	DrawLine(left + width/2, top, left + width/2, top + height - 2);
	
	// hoorizontal line at the distance top + height/2 from the edge of drawing window
	// going from 'left' to 'left + width of this window - the size of the pen'
	DrawLine(left, top + height/2, left + width - 2, top + height/2);
	
	SetPenSize(1, 1);
	
};

void Exercise6(){
// Test PaintWindow function

	ClearDrawing();
	cout << "Paint a window with four window panes using PaintWindow function" << endl << endl;

	while (Confirm("Another window?", true)){
	
		// code starts here
		cout << "Test the PaintWindow function" << endl << endl;
		int x = RequestInt("x coordinate of the left edge:");
		int y = RequestInt("y coordinate of the top edge:");
		int height = RequestInt("height of the window:");
		int width = RequestInt("width of the window:");
		
		PaintWindow(x, y, height, width);
	
		// code ends here
	}

};



void CountCoins1(int q,int d, int n, int p){
//
// Input: 			User supplies number of quarters, dimes, nickels, and pennies
//					Function makes no error checks for positive values
// Return values:	None
// Side Effect:		Function prints the total amuount in cents

	int total = 25 * q + 10 * d + 5 * n + p; // compute total amount in cents
	
	cout << "There is " << total << " cents in the pile." << endl; // print the result
	 
};

void Exercise7(){
// Read in how many quarters, dimes, nickles, and pennies are there in a pile
// Print how much money is there in dollars and cents

	// code starts here
	cout << "Input: count of coins" << endl;
	cout << "Output: total amount in dollars and cents" << endl;
	cout << "Use CountCoins1(...) function" << endl << endl;

	int quarters = RequestInt("Count the quarters:");	// number of quarters
	int dimes    = RequestInt("Count the dimes:");		// number of dimes
	int nickels  = RequestInt("Count the nickels:");	// number of nickels
	int pennies  = RequestInt("Count the pennies:");	// number of pennies
	
	CountCoins1(quarters, dimes, nickels, pennies);	
	
	// code ends here


};


int CountCoins2(int q,int d, int n, int p){
//
// Input: 			User supplies number of quarters, dimes, nickels, and pennies
//					Function makes no error checks for positive values
// Return values:	The total amuount in cents
// Side Effect:		None

	int total = 25 * q + 10 * d + 5 * n + p; // compute total amount in cents	
	return total;	 
};


void Exercise8(){
// Read in how many quarters, dimes, nickles, and pennies are there in a pile
// Print how much money is there in dollars and cents

	// code starts here
	cout << "Input: count of coins" << endl;
	cout << "Output: total amount in dollars and cents" << endl << endl;
	cout << "Use CountCoins1(...) function" << endl;
	cout << "Use CountCoins2(...) function" << endl << endl;

	int quarters = RequestInt("Count the quarters:");	// number of quarters
	int dimes    = RequestInt("Count the dimes:");		// number of dimes
	int nickels  = RequestInt("Count the nickels:");	// number of nickels
	int pennies  = RequestInt("Count the pennies:");	// number of pennies
	
	int total = CountCoins2(quarters, dimes, nickels, pennies); // compute total amount in cents
	
	int dollars = total / 100;						// compute and save number of whole dollars
	int cents = total % 100;						// compute the remaining change in cents
	
	// print the result
	cout << "There is $" << dollars << "." << cents << " in the pile." << endl; 
		
	// code ends here

};


void ConvertMoney(int totalCents, int& wholeDollars, int& fractionCents){
//
// Input: 			User supplies number of cents in the total amount
//					Function makes no error checks for positive values
// Return values:	whole dollar amount and the remaining cents
//					the value of reamining cents will be between 0 and 99
// Side Effect:		None

	wholeDollars = totalCents / 100;				// compute and save number of whole dollars
	fractionCents = totalCents % 100;				// compute the remaining change in cents
	
};

void Exercise9(){
// Read in how many quarters, dimes, nickles, and pennies are there in a pile
// Print how much money is there in dollars and cents

	// code starts here
	cout << "Input: count of coins" << endl;
	cout << "Output: total amount in dollars and cents" << endl;
	cout << "Use CountCoins2(...) and ConvertMoney(...) functions" << endl << endl;

	int quarters = RequestInt("Count the quarters:");	// number of quarters
	int dimes    = RequestInt("Count the dimes:");		// number of dimes
	int nickels  = RequestInt("Count the nickels:");	// number of nickels
	int pennies  = RequestInt("Count the pennies:");	// number of pennies
	
	int total = CountCoins2(quarters, dimes, nickels, pennies); // compute total amount in cents
	
	int dollars;										// the number of whole dollars
	int cents;											// the remaining change in cents
	
	ConvertMoney(total, dollars, cents);
	
	// print the result
	cout << "There is $" << dollars << "." << cents << " in the pile." << endl; 
		
	// code ends here

};

int ConvertToMinutes(int hours, int minutes){
//
// Input: 			User supplies number of hours and minutes
//					Function makes no error checks for positive values
// Return values:	time in minutes
// Side Effect:		None
	
	return(60 * hours + minutes);
	
};

void ConvertMinutesToHM(int time, int& hours, int& minutes){
//
// Input: 			User supplies time in minutes
//					Function makes no error checks for positive values
// Return values:	time in hours and minutes
//					the number of minutes will be between 0 and 60
// Side Effect:		None

	hours = time / 60;
	minutes = time - hours * 60;
	
};

void Exercise10(){
// ConvertToMinutes and ConvertMinutesToHM
//
// User types in the time when a game started and when the game ended
// Function prints the langth of the game in hours and minutes
//
	// code starts here
	cout << "Input: hour and minute time of the start and end of a game" << endl;;
	cout << "Output: duration of the game" <<  endl;
	cout << "Use ConvertToMinutes(...) and ConvertMinutesToHM(...) functions" << endl << endl; 

	int hourStart = RequestInt("Type in the hour the game started:");
	int minuteStart = RequestInt("Type in the minute the game started:");

	int hourEnd = RequestInt("Type in the hour the game ended:");
	int minuteEnd = RequestInt("Type in the minute the game ended:");
	
	int gametime = ConvertToMinutes(hourEnd, minuteEnd) - ConvertToMinutes(hourStart, minuteStart);
	
	int hours;
	int minutes;
	ConvertMinutesToHM(gametime, hours, minutes);
	
	cout << "The game lasted " << hours << " hours and " << minutes << " minutes." << endl;
	// code ends here

};


int main(int argc, char* argv[]) {

	// Use the following line if you choose NOT to open any graphics windows
	// InitializeConsole();

	// Build graphics window 0
	GraphicsWindow GW0(300, 300);

	// Move the console below graphics window 0
	ConsolePlaceBelow(0);

	// Give the console the focus for user interaction
	MakeConsoleForeground();

//////////

	// Enter the main program here
	int i = 1;
	
	while (Confirm("Another exercise?", true)){		// loop to run another exercise
		i = RequestInt("Exercise number: ", i);		// determine which exercise to run
													// the default moves you to the next one
		cout << endl << "Exercise " << i << endl;	// notify user of the accepted selection
		switch (i){									// un the appropriate exercise
			case 1: {
					Exercise1();
					break;
					}
			case 2: {
					Exercise2();
					break;
					}
			case 3: {
					Exercise3();
					break;
					}
			case 4: {
					Exercise4();
					break;
					}
			case 5: {
					Exercise5();
					break;
					}
			case 6: {
					Exercise6();
					break;
					}
			case 7: {
					Exercise7();
					break;
					}
			case 8: {
					Exercise8();
					break;
					}
			case 9: {
					Exercise9();
					break;
					}
			case 10: {
					Exercise10();
					break;
					}
			default:
					Exercise1();
		
		};
		i = (i+1);									// next time, continue with the next exercise
		if (i > 10)									// we only have 10 exercises - cycle through again
			i = 1;
		cout << endl;

	}
	
//////////

	// The lines below make sure that the graphics windows remain open just
	// before the program terminates

	PressReturn("\nThe main program is about to terminate\n");

	return 0;
}

