/************************************************************************
* Program: Converting Egyptian Fractions to Fraction * * * * File: Egy_lib.c ==> Library File for Egyptian Fraction * * * * * *************************************************************************/#include"Egy_head.h" //User-defined header file
void EgyFrac (egy_frac EGY, char *output_file) /* Calculation of Egyptian fractions */
{
res_frac Res_EGY;
int prod_den=1,sum_den=0,gcd_prod_sum_den;
int i,j;
int *p_units;
FILE *ofp;
ofp=fopen (output_file,"a+");
if (ofp == NULL)
{
perror("Output file\n");
exit(2);
}
for (i=0; i < EGY.no_units_fractions; i++)
{
prod_den = prod_den * EGY.p_den[i];
}
p_units = (int *)malloc(sizeof(int) * EGY.no_units_fractions);
if (p_units == NULL)
{
printf("Error in Data_units\n");
exit(3);
}
for (i=0; i < EGY.no_units_fractions; i++)
{
p_units[i] = prod_den
Library file for Egyptian fraction
| Application file for Egyptian fraction | |
|---|---|
| 1 | /************************************************************************ |
| 2 | * Program: Converting Egyptian Fractions to Fractions * |
| 3 | * * |
| 4 | * File: Egy_app.c ==> Main File for Egyptian * |
| 5 | * * |
| 6 | * * |
| 7 | *************************************************************************/ |
| 8 | |
| 9 | |
| 10 | #include"Egy_head.h" |
| 11 | |
| 12 | int main(int argc, char *argv[]) |
| 13 | { |
| 14 | egy_frac egy_ip ; |
| 15 | int no_instances; |
| 16 | int i,j; |
| 17 | FILE *ifp; |
| 18 | |
| 19 | if (argc != 3) |
| 20 | { |
| 21 | printf("Insufficient data\n"); |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | ifp = fopen(argv[1],"r"); |
| 27 | if (ifp==NULL) |
| 28 | { |
| 29 | perror("open"); |
| 30 | exit(1); |
| 31 | } |
| 32 | |
| 33 | fscanf(ifp,"%d",&no_instances); /* No of instances */ |
| 34 | |
| 35 | for (j=0;j<no_instances;j++) |
| 36 | { |
| 37 | fscanf(ifp,"%d",&egy_ip.no_units_fractions); /* No of units fractions */ |
| 38 | |
| 39 | |
| 40 | egy_ip.p_den = (int *)malloc( sizeof(int) * egy_ip.no_units_fractions ); |
| 41 | if (egy_ip.p_den == NULL) |
| 42 | { |
| 43 | perror("Error in allocating space\n"); |
| 44 | exit(2); |
| 45 | } |
| 46 | |
| 47 | for (i=0;i<egy_ip.no_units_fractions;i++) |
| 48 | { |
| 49 | fscanf(ifp,"%d",egy_ip.p_den + i); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | EgyFrac (egy_ip, argv[2]); /* Calling the function Egyptian fraction */ |
| 54 | free(egy_ip.p_den); |
| 55 | } |
| 56 | |
| 57 | fclose(ifp); |
| 58 | |
| 59 | return 0; |
| 60 | |
| 61 | } |