#include <iostream>
using namespace std;
/*
Below function is the heart of the prog & actually does the dirty work
*/
int div(int numer, int denom, int *remainder){
if (denom == 0 ) return (-1);
if (numer >= denom) {
return (1 + div(numer-denom , denom , remainder));
}
else{
*remainder = numer;
return 0;
}
}
//Below func will handle -ve inputs
int fix_negative_input( int n, int d){
if ( (n < 0 && d < 0 ) || (n > 0 && d >0 ) ){
return 1;
}
return -1;
}
//This func is an interface to look pretty & clean ,but delegates actual work to others
//will take input as the numerator & denominator
//but after completion if will return the quotient & remainder in the same args
//return value will indicate if the operation was successful or exception occured
bool recursive_quo_remain( int *numer , int *denom){
int r=0;
int n=*numer>0?*numer:*numer*-1; // we could use abs(x) also
int d=*denom>0?*denom:*denom*-1;
int q = div(n,d,&r);
if (q >= 0 ) {
*numer = fix_negative_input(*numer, *denom) * q ;
*denom = r;
return true;
}
return false;
}
int main()
{
//printf("Hello World\n");
int r =0;
// int q = div(9,0,&r);
// printf ("q:%d , r:%d\n" , q,r);
int numerator = 10;
int denominator = 0;
bool done = recursive_quo_remain(&numerator , &denominator );
if (done) {
std::cout << "q:"<< numerator <<" " << "r:"<<denominator<<std::endl;
}
else{
cout <<"something messed up in computation\n";
}
return 0;
}