Question
All files are located below. Please copy and paste all source code once complete. Also take screenshot of the compiled output which should look like
All files are located below. Please copy and paste all source code once complete. Also take screenshot of the compiled output which should look like the sample output.
//---------------------------------------------------------
// FRACTION header
// Fraction.h
//---------------------------------------------------------
#ifndef FRACTION_H
#define FRACTION_H
typedef struct
{
int sign; /* sign = -1 (negative), = +1 (non-negative) */
int N; /* (N)umerator */
int D; /* (D)enominator */
} FRACTION;
//---------------------------------------------------------
// Public Operations
//---------------------------------------------------------
void ConstructFRACTION(FRACTION *fraction,const int N,const int D);
FRACTION SumOfFRACTIONs(const FRACTION *LHS,const FRACTION *RHS);
FRACTION DifferenceOfFRACTIONs(const FRACTION *LHS,const FRACTION *RHS);
FRACTION ProductOfFRACTIONs(const FRACTION *LHS,const FRACTION *RHS);
FRACTION QuotientOfFRACTIONs(const FRACTION *LHS,const FRACTION *RHS);
void InputFRACTION(FRACTION *fraction,FILE *IN);
void OutputFRACTION(const FRACTION *fraction,FILE *OUT);
//---------------------------------------------------------
// Private (Utility) Operations
//---------------------------------------------------------
//int GCD(const int x,const int y);
//void SetSign(FRACTION *fraction);
//void Reduce(FRACTION *fraction);
#endif
//---------------------------------------------------------
// FRACTION function definitions
// Fraction.c
//---------------------------------------------------------
#include
#include
#include
#include ".\Fraction.h"
//---------------------------------------------------------
void ConstructFRACTION(FRACTION *fraction,const int N,const int D)
//---------------------------------------------------------
{
void SetSign(FRACTION *fraction);
void Reduce(FRACTION *fraction);
fraction->N = N;
fraction->D = D;
SetSign(fraction); Reduce(fraction);
}
//---------------------------------------------------------
FRACTION SumOfFRACTIONs(const FRACTION *LHS,const FRACTION *RHS)
//---------------------------------------------------------
{
void SetSign(FRACTION *fraction);
void Reduce(FRACTION *fraction);
FRACTION fraction;
fraction.N = LHS->sign*LHS->N*RHS->D + RHS->sign*RHS->N*LHS->D;
fraction.D = LHS->D*RHS->D;
SetSign(&fraction); Reduce(&fraction);
return( fraction );
}
//---------------------------------------------------------
FRACTION DifferenceOfFRACTIONs(const FRACTION *LHS,const FRACTION *RHS)
//---------------------------------------------------------
{
void SetSign(FRACTION *fraction);
void Reduce(FRACTION *fraction);
FRACTION fraction;
fraction.N = LHS->sign*LHS->N*RHS->D - RHS->sign*RHS->N*LHS->D;
fraction.D = LHS->D*RHS->D;
SetSign(&fraction); Reduce(&fraction);
return( fraction );
}
//---------------------------------------------------------
FRACTION ProductOfFRACTIONs(const FRACTION *LHS,const FRACTION *RHS)
//---------------------------------------------------------
{
void SetSign(FRACTION *fraction);
void Reduce(FRACTION *fraction);
FRACTION fraction;
fraction.N = LHS->sign*LHS->N * RHS->sign*RHS->N;
fraction.D = LHS->D * RHS->D;
SetSign(&fraction); Reduce(&fraction);
return( fraction );
}
//---------------------------------------------------------
FRACTION QuotientOfFRACTIONs(const FRACTION *LHS,const FRACTION *RHS)
//---------------------------------------------------------
{
void SetSign(FRACTION *fraction);
void Reduce(FRACTION *fraction);
FRACTION fraction;
fraction.N = LHS->sign*LHS->N * RHS->sign*RHS->D;
fraction.D = LHS->D * RHS->N;
SetSign(&fraction); Reduce(&fraction);
return( fraction );
}
//---------------------------------------------------------
void InputFRACTION(FRACTION *fraction,FILE *IN)
//---------------------------------------------------------
{
void SetSign(FRACTION *fraction);
void Reduce(FRACTION *fraction);
fscanf(IN,"%d/%d",&fraction->N,&fraction->D);
SetSign(fraction);
Reduce(fraction);
}
//---------------------------------------------------------
void OutputFRACTION(const FRACTION *fraction,FILE *OUT)
//---------------------------------------------------------
{
fprintf(OUT,"%c%d/%d",((fraction->sign == -1) ? '-' : '+'),fraction->N,fraction->D);
}
//---------------------------------------------------------
void SetSign(FRACTION *fraction)
//---------------------------------------------------------
{
if ( ((fraction->N >= 0) && (fraction->D >= 0)) ||
((fraction->N D
fraction->sign = +1;
else
fraction->sign = -1;
fraction->N = abs(fraction->N);
fraction->D = abs(fraction->D);
}
//---------------------------------------------------------
void Reduce(FRACTION *fraction)
//---------------------------------------------------------
{
int GCD(const int x,const int y);
int gcd = GCD(fraction->N,fraction->D);
fraction->N /= gcd;
fraction->D /= gcd;
}
//---------------------------------------------------------
int GCD(const int x,const int y)
//---------------------------------------------------------
{
if ( y == 0 )
return( x );
else
return( GCD(y,x % y) );
}
//---------------------------------------------------------
// Chapter #8, Problem #16
// Problem16.c
//---------------------------------------------------------
#include
#include
#include
#include ".\Fraction.h"
//---------------------------------------------------------
int main()
//---------------------------------------------------------
{
FRACTION r,LHS,RHS;
printf("LHS? "); InputFRACTION(&LHS,stdin);
ConstructFRACTION(&RHS,4,-12);
printf("LHS = "); OutputFRACTION(&LHS,stdout); printf(" ");
printf("RHS = "); OutputFRACTION(&RHS,stdout); printf(" ");
r = SumOfFRACTIONs(&LHS,&RHS);
printf("LHS+RHS = "); OutputFRACTION(&r,stdout); printf(" ");
r = DifferenceOfFRACTIONs(&LHS,&RHS);
printf("LHS-RHS = "); OutputFRACTION(&r,stdout); printf(" ");
r = DifferenceOfFRACTIONs(&RHS,&LHS);
printf("RHS-LHS = "); OutputFRACTION(&r,stdout); printf(" ");
r = ProductOfFRACTIONs(&LHS,&RHS);
printf("LHS*RHS = "); OutputFRACTION(&r,stdout); printf(" ");
r = QuotientOfFRACTIONs(&LHS,&RHS);
printf("LHS/RHS = "); OutputFRACTION(&r,stdout); printf(" ");
r = QuotientOfFRACTIONs(&RHS,&LHS);
printf("RHS/LHS = "); OutputFRACTION(&r,stdout); printf(" ");
system("PAUSE");
return( 0 );
}
Introduction A rational number is any number that can be expressed as the quotient or fraction N/D of two integers, with the denominator D not equal to zero. Since D may be equal to 1, every integer is a rational number The abstract data type, FRACTION (implemented in the C programming language), can be used to represent rational numbers. Problem Convert the C programming language implementation of the FRACTION abstract data type (found in source files Fraction.h and Fraction.c) into a Java class definition and convert the C application program Problem16.c into a Java console application. Sample Program Dialo Command Prompt C:NCOURSES CS2323NCode Chapter8>javac Problem16.java CNCOURSES CS2323NCode Chapter8>java Problemi6 LHS RHS LHS-RHS+2/15 LHSRHS+1/15 RHS/LHS+5/3 C:NCOURSES CS2323NCode Chapter8>Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started