/****************************************************************
* Program: Converting Egyptian Fractions to Fractions *
* *
* File: Egy_head.h ==> Header File for Egyptian *
* created on: 21-Aug *
* *
*****************************************************************/
#include<stdio.h>
#include<stdlib.h>
typedef struct egy_fractions /* No of unit fractions and points to denominator */
{
int no_units_fractions;
int *p_den;
}egy_frac;
typedef struct resultant_fraction /* Resultant of fraction numerator and denominator */
{
int num;
int den;
}res_frac;
void EgyFrac (egy_frac ,char *); /* Calculating Egyptian fractions */
int Gcd (int , int ); /* Calculating the Gcd for prod_denominator and sum_denominator */
Header file for Egyptian Fraction
/************************************************************************
* Program: Converting Egyptian Fractions to Fractions *
* *
* File: Egy_app.c ==> Main File for Egyptian *
* *
* *
*************************************************************************/
#include"Egy_head.h"
int main(int argc, char *argv[])
{
egy_frac egy_ip ;
int no_instances;
int i,j;
FILE *ifp;
if (argc != 3)
{
printf("Insufficient data\n");
return 0;
}
ifp = fopen(argv[1],"r");
if (ifp==NULL)
{
perror("open");
exit(1);
}
fscanf(ifp,"%d",&no_instances); /* No of instances */
for (j=0;j<no_instances;j++)
{
fscanf(ifp,"%d",&egy_ip.no_units_fractions); /* No of units fractions */
egy_ip.p_den = (int *)malloc( sizeof(int) * egy_ip.no_units_fractions );
if (egy_ip.p_den == NULL)
{
perror("Error in allocating space\n");
exit(2);
}
for (i=0;i<egy_ip.no_units_fractions;i++)
{
fscanf(ifp,"%d",eg
Application file for Egyptian fraction
This change has been discarded.
Library file for Egyptian fraction | |
---|---|
1 | /************************************************************************ |
2 | * Program: Converting Egyptian Fractions to Fraction * |
3 | * * |
4 | * File: Egy_lib.c ==> Library File for Egyptian Fraction * |
5 | * * |
6 | * * |
7 | *************************************************************************/ |
8 | #include"Egy_head.h" //User-defined header file |
9 | |
10 | |
11 | void EgyFrac (egy_frac EGY, char *output_file) /* Calculation of Egyptian fractions */ |
12 | { |
13 | res_frac Res_EGY; |
14 | int prod_den=1,sum_den=0,gcd_prod_sum_den; |
15 | int i,j; |
16 | int *p_units; |
17 | FILE *ofp; |
18 | |
19 | |
20 | ofp=fopen (output_file,"a+"); |
21 | if (ofp == NULL) |
22 | { |
23 | perror("Output file\n"); |
24 | exit(2); |
25 | } |
26 | |
27 | for (i=0; i < EGY.no_units_fractions; i++) |
28 | { |
29 | prod_den = prod_den * EGY.p_den[i]; |
30 | } |
31 | |
32 | p_units = (int *)malloc(sizeof(int) * EGY.no_units_fractions); |
33 | if (p_units == NULL) |
34 | { |
35 | printf("Error in Data_units\n"); |
36 | exit(3); |
37 | } |
38 | |
39 | for (i=0; i < EGY.no_units_fractions; i++) |
40 | { |
41 | p_units[i] = prod_den / EGY.p_den[i]; |
42 | } |
43 | |
44 | for (i=0; i < EGY.no_units_fractions; i++) |
45 | sum_den = sum_den + p_units[i]; |
46 | |
47 | |
48 | gcd_prod_sum_den = Gcd (sum_den,prod_den); //calling the gcd function |
49 | |
50 | Res_EGY.num = sum_den / gcd_prod_sum_den; |
51 | Res_EGY.den = prod_den / gcd_prod_sum_den; |
52 | |
53 | |
54 | for (i=0; i < EGY.no_units_fractions; i++) |
55 | { |
56 | fprintf(ofp,"1/%d+",EGY.p_den[i]); |
57 | } |
58 | fprintf(ofp,"\b=%d/%d\n", Res_EGY.num,Res_EGY.den); |
59 | fclose(ofp); |
60 | |
61 | prod_den = 1; |
62 | sum_den = 0; |
63 | free(p_units); |
64 | } |
65 | |
66 | |
67 | int Gcd (int x, int y) // GCD function |
68 | { |
69 | int i; |
70 | for ( i = x; i >= 1; i--) |
71 | { |
72 | if( x % i == 0 && y % i == 0) |
73 | break; |
74 | } |
75 | return i; |
76 | |
77 | } |