Ejemplo n.º 1
0
int
main(void)
{
	int n1, d1;	/* numerator, denominator of first fraction */
	int n2, d2;	/* numerator, denominator of second fraction */
	char op;	/* arithmetic operator + - * or /  */
	char again;	/* y or n depending on user's desire to continue */
	int n_ans,	/* numerator, denominator of answer */
	    d_ans;

	/* While the user wants to continue, gets and solves arithmetic
	   problems with common fractions. */

	do
	{
       	   /* Gets a fraction problem */
	   scan_fraction(&n1, &d1);
	   op = get_operator();
	   scan_fraction(&n2, &d2);

	   /* Computes the result */
	   switch (op)
	   {
	  	case '+':
			add_fractions(n1, d1, n2, d2, &n_ans, &d_ans);
			break;

		case '-':
			add_fractions(n1, d1, -n2, d2, &n_ans, &d_ans);
			break;

		case '*':
			multiply_fractions(n1, d1, n2, d2, &n_ans, &d_ans);
			break;

		case '/':
			multiply_fractions(n1, d1, d2, n2, &n_ans, &d_ans);
	   }
	   reduce_fraction(&n_ans, &d_ans);

	   /* Displays problem and result */
	   printf("\n");
	   print_fraction(n1, d1);
	   printf(" %c ", op);
	   print_fraction(n2, d2);
	   printf(" = ");
	   print_fraction(n_ans, d_ans);

	   /* Asks user about doing another problem */
	   printf("\nDo another problem? (y/n) > ");
	   scanf(" %c", &again);
	}
	while (again == 'Y' || again == 'y');
	return(0);
}
Ejemplo n.º 2
0
static _TKN_TYPE_ scan_number(_NIL_TYPE_)
 { do
     scan_store_next_ch();
   while (SCAN_CHECK(digit_mask));
   if (SCAN_CHECK(period_mask))
     return scan_fraction();
   else
     if (SCAN_CHECK(exponent_mask))
       return scan_exponent();
     else
       { scan_terminate_text();
         return _NBR_TOKEN_; }}