示例#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);
}
示例#2
0
文件: main.c 项目: dustan1/Things
void test_multiply_fractions(void)
{

    fraction_t fr1, fr2, product;
    int numerator, denominator;

    // Test multiply_fractions: 2/3 * 1/2
	//fr1.num = 2;
	//fr1.den = 3;
    //fr2.num = 1;
    //fr2.den = 2;

    make_fraction(2, 3, &fr1);
    make_fraction(1, 2, &fr2);

	product.num = 0;
	product.den = 0;
    multiply_fractions(&fr1, &fr2, &product);
    numerator = product.num;
    denominator = product.den;
    sput_fail_unless(numerator == 1,
                     "multiply_fractions: 2/3 * 1/2 ==> 1/3, "
                     "check numerator");  
    sput_fail_unless(denominator == 3,
                     "multiply_fractions: 2/3 * 1/2 ==> 1/3, "
                     "check denominator"); 

    // Test multiply_fractions: 1/2 * 2/1
	//fr1.num = 1;
	//fr1.den = 2;
    //fr2.num = 2;
    //fr2.den = 1;

    make_fraction(1, 2, &fr1);
    make_fraction(2, 1, &fr2);

	product.num = 0;
	product.den = 0;
    multiply_fractions(&fr1, &fr2, &product);
    numerator = product.num;
    denominator = product.den;
    sput_fail_unless(numerator == 1,
                     "multiply_fractions: 1/2 * 2/1 ==> 1/1, "
                     "check numerator");  
    sput_fail_unless(denominator == 1,
                     "multiply_fractions: 1/2 * 2/1 ==> 1/1, "
                     "check denominator"); 
}
示例#3
0
文件: ex7.c 项目: DataParisian/knkc
/*
 * d)  Multiply the fractions f1 and f2.
 */
void test_part_d(void){
	struct fraction product, f1, f2;
		
	printf("Enter first fraction (a/b): ");
	scanf("%d / %d", &f1.numerator, &f1.denominator);
	
	printf("Enter second fraction (a/b): ");
	scanf("%d / %d", &f2.numerator, &f2.denominator);
	
	product = multiply_fractions(f1, f2);
	product = reduce_fraction(product);
	
	printf("Product of the fractions: %d/%d\n", product.numerator, product.denominator);
}
示例#4
0
void test_fraction(int numerator1, int denominator1, 
                   int numerator2, int denominator2,
                   enum FRACTION_OPERATION operation, 
                   int mixed)
{
  struct FRACTION a = make_fraction(numerator1, denominator1);
  struct FRACTION b = make_fraction(numerator2, denominator2);
  struct FRACTION c;
  char op;

  if (operation == foAdd)
  {
    c = add_fractions(&a, &b);
    op = '+';
  }
  else if (operation == foSubtract)
  {
    c = subtract_fractions(&a, &b);
    op = '-';
  }
  else
  {
    c = multiply_fractions(&a, &b);
    op = '*';
  }

  printf("%i/%i %c %i/%i = %i/%i", a.numerator, a.denominator,
                                   op,
                                   b.numerator, b.denominator,
                                   c.numerator, c.denominator);
  if (mixed)
  {
    struct MIXED_FRACTION mixed = reduced_to_mixed(&c);

    printf(" ==> ");
      /* There's a whole number portion */
    if (mixed.whole_number)
      printf("%i ", mixed.whole_number);

      /* There's a proper fraction portion */
    if (mixed.proper_fraction.numerator)
      printf("%i/%i", mixed.proper_fraction.numerator, 
                      mixed.proper_fraction.denominator);

    if (!mixed.whole_number && !mixed.proper_fraction.numerator)
      printf("0");
  }
  printf("\n");
}
示例#5
0
文件: lab5.c 项目: tmzhuang/carleton
int main(void)
{
	struct fraction fr;

	/* Test print_fraction. */
	printf("\nTesting print_fraction.\n");

	fr.num = 1;
	fr.den = 3;
	printf("Calling print_fraction to print 1/3. Actual result = ");
	print_fraction(fr);
	printf(".\n");

	fr.num = -1;
	fr.den = 3;
	printf("Calling print_fraction to print -1/3. Actual result = ");
	print_fraction(fr);
	printf(".\n");

	/* Test gcd. */
	printf("\nTesting gcd.\n");

	if (gcd(42,30) != 6)
		printf("Calling gcd(42, 30). Expected result = 6. Actual result = %d.\n",
		    gcd(42, 30));

	if (gcd(30,42) != 6)
		printf("Calling gcd(30, 42). Expected result = 6. Actual result = %d.\n",
		    gcd(30, 42));

	if (gcd(-42,30) != 6)
		printf("Calling gcd(-42, 30). Expected result = 6. Actual result = %d.\n",
		    gcd(-42, 30));

	if (gcd(42,-30) != 6)
		printf("Calling gcd(42, -30). Expected result = 6. Actual result = %d.\n",
		    gcd(42, -30));

	if (gcd(-42,-30) != 6)
		printf("Calling gcd(-42, -30). Expected result = 6. Actual result = %d.\n",
		    gcd(-42, -30));

	if (gcd(144,55) != 1)
		printf("Calling gcd(144, 55). Expected result = 1. Actual result = %d.\n",
		    gcd(144, 55));

	/* Test reduce. */
	printf("\nTesting reduce.\n");

	fr.num = 1;
	fr.den = 3;
	printf("Reduce 1/3. Expected result = 1/3. Actual result = ");
	print_fraction(reduce(fr));
	printf(".\n");

	fr.num = 6;
	fr.den = 8;
	printf("Reduce 6/8. Expected result = 3/4. Actual result = ");
	print_fraction(reduce(fr));
	printf(".\n");

	fr.num = -2;
	fr.den = 3;
	printf("Reduce -2/3. Expected result = -2/3. Actual result = ");
	print_fraction(reduce(fr));
	printf(".\n");

	fr.num = 4;
	fr.den = -5;
	printf("Reduce 4/-5. Expected result = -4/5. Actual result = ");
	print_fraction(reduce(fr));
	printf(".\n");

	fr.num = -6;
	fr.den = -8;
	printf("Reduce -6/-8. Expected result = 3/4. Actual result = ");
	print_fraction(reduce(fr));
	printf(".\n");

	fr.num = 0;
	fr.den = 7;
	printf("Reduce 0/7. Expected result = 0/1. Actual result = ");
	print_fraction(reduce(fr));
	printf(".\n");

	/* Test make_fraction. */
	printf("\nTesting make_fraction.\n");

	struct fraction result;
	
	result = make_fraction(1,3);
	if (result.num != 1 || result.den != 3) {
		printf("Calling make_fraction(1, 3). Expected result = 1/3. Actual result = ");
		print_fraction(make_fraction(1, 3));
		printf(".\n");
	}

	result = make_fraction(6,8);
	if (result.num != 3 || result.den != 4) {
		printf("Calling make_fraction(6, 8). Expected result = 3/4. Actual result = ");
		print_fraction(make_fraction(6, 8));
		printf(".\n");
	}

	result = make_fraction(-2,3);
	if (result.num != -2 || result.den != 3) {
		printf("Calling make_fraction(-2, 3). Expected result = -2/3. Actual result = ");
		print_fraction(make_fraction(-2, 3));
		printf(".\n");
	}

	result = make_fraction(4,-5);
	if (result.num != -4 || result.den != 5) {
		printf("Calling make_fraction(4, -5). Expected result = -4/5. Actual result = ");
		print_fraction(make_fraction(4, -5));
		printf(".\n");
	}

	result = make_fraction(-6,-8);
	if (result.num != 3 || result.den != 4) {
		printf("Calling make_fraction(-6, -8). Expected result = 3/4. Actual result = ");
		print_fraction(make_fraction(-6, -8));
		printf(".\n");
	}

	result = make_fraction(0,7);
	if (result.num != 0 || result.den != 1) {
		printf("Calling make_fraction(0, 7). Expected result = 0/1. Actual result = ");
		print_fraction(make_fraction(0, 7));
		printf(".\n");
	}

	/* Test multiply_fractions. */
	printf("\nTesting multiply_fractions.\n");
	result = multiply_fractions(make_fraction(0,1), make_fraction(0,2));
	if (result.num != 0 || result.den != 1) {
		printf("Calling multiply_fractions(make_fraction(0,1), make_fraction(0,2)). Expected result = 0/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = multiply_fractions(make_fraction(1,1), make_fraction(0,2));
	if (result.num != 0 || result.den != 1) {
		printf("Calling multiply_fractions(make_fraction(1,1), make_fraction(0,2)). Expected result = 0/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = multiply_fractions(make_fraction(0,1), make_fraction(2,2));
	if (result.num != 0 || result.den != 1) {
		printf("Calling multiply_fractions(make_fraction(0,1), make_fraction(2,2)). Expected result = 0/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = multiply_fractions(make_fraction(3,1), make_fraction(4,2));
	if (result.num != 6 || result.den != 1) {
		printf("Calling multiply_fractions(make_fraction(3,1), make_fraction(4,2)). Expected result = 6/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = multiply_fractions(make_fraction(3,2), make_fraction(2,9));
	if (result.num != 1 || result.den != 3) {
		printf("Calling multiply_fractions(make_fraction(3,2), make_fraction(2,9)). Expected result = 1/3. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = multiply_fractions(make_fraction(3,12), make_fraction(4,9));
	if (result.num != 1 || result.den != 9) {
		printf("Calling multiply_fractions(make_fraction(3,12), make_fraction(4,9)). Expected result = 1/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}


	/* Test add_fractions. */
	printf("\nTesting add_fractions.\n");

	result = add_fractions(make_fraction(0,1), make_fraction(0,2));
	if (result.num != 0 || result.den != 1) {
		printf("Calling add_fractions(make_fraction(0,1), make_fraction(0,2)). Expected result = 0/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = add_fractions(make_fraction(1,1), make_fraction(0,2));
	if (result.num != 1 || result.den != 1) {
		printf("Calling add_fractions(make_fraction(1,1), make_fraction(0,2)). Expected result = 1/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = add_fractions(make_fraction(0,1), make_fraction(2,2));
	if (result.num != 1 || result.den != 1) {
		printf("Calling add_fractions(make_fraction(0,1), make_fraction(2,2)). Expected result = 1/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = add_fractions(make_fraction(3,1), make_fraction(4,2));
	if (result.num != 5 || result.den != 1) {
		printf("Calling add_fractions(make_fraction(3,1), make_fraction(4,2)). Expected result = 5/1. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = add_fractions(make_fraction(3,2), make_fraction(2,9));
	if (result.num != 31 || result.den != 18) {
		printf("Calling add_fractions(make_fraction(3,2), make_fraction(2,9)). Expected result = 31/18. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}

	result = add_fractions(make_fraction(3,12), make_fraction(4,9));
	if (result.num != 25 || result.den != 36) {
		printf("Calling add_fractions(make_fraction(3,12), make_fraction(4,9)). Expected result = 25/36. Actual result = ");
		print_fraction(result);
		printf(".\n");
	}



	return 0;
}