/*

EE5C programming assignment 6.1

file    :       p6-1.c
compiler:       gcc
name    :       Cedric F. Lam
        :       Duy Nguyen (modified to C++)
S.I.D.  :       911-911-911
date    :       3-1-97
	   :       2-2-98 (modified)
class   :       EE5C (Lect1)

*/

#include <iostream.h>
#include <ctype.h>

/* function prototypes */
int gcd(int, int);
int yes_no(void);
int coprime(void);

/*
 *  This function returns the gcd of two non-negative integers u, and v 
 */
int gcd(int u, int v)
{
	int temp;
 
 	while (v != 0) {
 		temp = u % v;
 		u = v;
 		v = temp;
 	}
 	return (u);
}

/*
 * This function gets the y/n answer from the user.
 * It waits until a valid y/n answer is typed by the user.
 * The function returns 1 for yes and 0 for no.
 *
 * Note: This is the same yes_no() function we used in assigment #5.
 * We take advantage of code reuse in modulo programming.
 */
int yes_no(void)
{
        char c;
        int yes = -1; /* initialized to an invalid input character */
        
        do{
                while(isspace(c=cin.get())); /* skip leading blank characters */
                if(c == 'y' || c =='Y')
                        yes = 1;
                else if(c == 'n' || c =='N')
                        yes = 0;
                while((c=cin.get())!='\n'); /* skip rest of the line */ 
        }while (yes == -1);
        return yes; 
}

/*
 * The provision of the gcd function makes a separate co-prime function 
 * as described in the assignment sheet obsolete.
 *
 * The behaviour of the co-prime function has been modified from the
 * initial description in the assignment sheets.
 */
int coprime(void)
{
	int 	u, v, 	/* input numbers */
		factor,	/* gcd of u and v */
		error;	/* error flag for input validation */
	char ch; 	/* dummy variable */

        /* Here is a simple input validation. This do-while loop may worth
         * a separate function to make the code clearer.
	 * Alternatively, a full validation as in assignment #5 can be
	 * written as a function for each input.
         */
        do{
                 error = 0;
                 cout << "Enter the first number: ";
			  cin >> u;
                 if(error = !cin)
                 do{/* skip the rest of the line. */
					cin >> ch;
                 }while ( ch!='\n');
        }while(error);
        do{
                 error = 0;
                 cout << "Enter the second number: ";
			  cin >> v;
                 if(error = !cin)
                 do{/* skip the rest of the line. */ 
                         cin >> ch; 
		 }while ( ch!='\n');
        }while(error);
	if (u <= 0 || v <= 0){
		cout << "Error: numbers entered must be positive integers.\n";
		return 0;
	}
 	if( (factor = gcd(u, v)) > 1 ){
		cout << "\n" << u << " and " << v << " are not co-prime, their ";
		cout << "GCD is " << factor << endl;
	}
	else
		cout << "\n" << u << " and " << v << " are co-prime.\n";
	return 1;
}
	
int main(void)
{
	do{
		coprime();
		cout << "\nDo you want to continue (Y/N)? ";
	} while(yes_no());
	cout << "Thank you! Good-bye!\n";
}

