1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/************************************************************************
* Author: M.J. Leslie
* Purpose: Add two fractions together.
* Date: 04-Oct-94
************************************************************************/
/******************* Structures *****************************************/
/* Define a structure to hold the
* fraction */
struct fract
{
int num; /* Numerator */
int den; /* Denominator */
};
/******************* Declare Functions **********************************/
void print_title(void); /* O/P the title information. */
/* Get the fraction from the
* user. */
void read_fract(struct fract *);
/* Add fractions by passing the
* whole structure to the function.
*/
struct fract add_fraction1(struct fract, struct fract);
/* Add fractions by passing
* pointers to the structures.
*/
void add_fraction2(struct fract *, struct fract *, struct fract *);
/* Print fractions */
void print_fraction(char *, struct fract fraction, char *);
/****************** Main ************************************************/
main ()
{
/* Declare the structures that
* hold the fractions */
struct fract fract1, fract2, out_fract;
print_title(); /* airy-fairy title */
/* Get the fraction values
* from the user */
read_fract(&fract1);
read_fract(&fract2);
puts("\n Passing structure to function gives:");
print_fraction(" ",fract1, " + ");
print_fraction("",fract2, " = ");
/* add the fractions together */
out_fract = add_fraction1(fract1, fract2);
print_fraction("",out_fract, "\n");
puts("\n Passing structure pointers to function gives:");
print_fraction(" ",fract1, " + ");
print_fraction("",fract2, " = ");
add_fraction2(&fract1, &fract2, &out_fract);
print_fraction("",out_fract, "\n");
return(1);
}
/************************************************************************/
void print_title(void)
{
puts("\n This is a C education program that will add fractions.");
puts(" It demonstrates the technic of copying structures to ");
puts(" functions and copying pointers to structures to functions.");
puts("\n As a by-product the program will ask for two fractions");
puts(" that are added together and the result put on the screen");
puts(" ");
}
/************************************************************************/
void read_fract(struct fract *fract)
{
char value[80];
puts(" ");
printf(" Please enter the numerator ===> ");
gets(value);
fract->num = atoi(value);
printf(" Please enter the denominator => ");
gets(value);
fract->den = atoi(value);
}
/************************************************************************/
struct fract add_fraction1(struct fract f1, struct fract f2)
{
struct fract answer;
/* get the comman denominator */
answer.den = f1.den * f2.den;
/* sort out the numerators */
f1.num = (answer.den / f1.den) * f1.num;
f2.num = (answer.den / f2.den) * f2.num;
/* Add up the numerators */
answer.num = f1.num + f2.num;
return (answer);
}
/************************************************************************/
void add_fraction2(struct fract *f1, struct fract *f2, struct fract *answer)
{
/* Use temps so we do not corrupt
* The original values of f1 and
* f2 */
int temp1, temp2;
/* get the comman denominator */
answer->den = f1->den * f2->den;
/* sort out the numerators */
temp1 = (answer->den / f1->den) * f1->num;
temp2 = (answer->den / f2->den) * f2->num;
/* Add up the numerators */
answer->num = temp1 + temp2;
}
/************************************************************************/
void print_fraction(char * string1, struct fract fraction, char * string2)
{
/* Format the fraction with
* information strings around it
*/
printf("%s%d/%d%s", string1, fraction.num, fraction.den, string2);
}
|