Advertisement

06.01.2008 at 08:05PM PDT, ID: 23448818
[x]
Attachment Details

How do I use a Simplify function in a Rational Structure

Asked by geminibabie05 in C Programming Language

Tags: C, C++

I am currently working on Structures in C Programming. I am almost finished with one solving Rational equations. I now have to use a Simplify function using gcd in order to simplify the equations. I am unsure of how to use the funtion with the RationalClass pointers to make this work. Any help would be great. Thank you.Start Free Trial
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:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
#include "stdafx.h"
#include <math.h>
#include <stdlib.h>
 
typedef struct RationalClass
{
	int numPart;
	int denomPart;
}RationalClass;
 
typedef RationalClass* Rational;
Rational initRational( int num, int denom );
Rational cloneRational( Rational orig );
void freeRational( Rational r );
int getNumPart( Rational r );
int getDenomPart( Rational r );
Rational addRational( Rational r1, Rational r2 );
Rational subRational( Rational r1, Rational r2 );
Rational multRational( Rational r1, Rational r2 );
Rational divideRational( Rational r1, Rational r2 );
int gcd( int num, int denom );
Rational simplify( Rational r);
Rational readRational( FILE* f );
void displayRational( Rational r );
 
 
int main()
 
{
	Rational r1, r2, r3, r4, r5, r6, r7;
	r1 = initRational( 2,1 );
	printf( "Enter a rational number\n" );
	r2 = readRational( stdin );
	r3 = addRational( r1, r2 );
	r4 = multRational( r2, r3 );
	r5 = addRational( r1, multRational( r2, r3 ));
	r6 = divideRational( r1, r2 );
	r7 = subRational( r1, r2 );
	
	 
	printf( "(" );
	//displayRational( r1 );			/* r1 + r2 = r3 */
	printf("%d %c %d", r1->numPart, '/', r1->denomPart);
	printf( ") + (" );
	//displayRational( r2 );
	printf("%d %c %d", r2->numPart, '/', r2->denomPart);
	printf( ") = (" );
	//displayRational( r3 );
	printf("%d %c %d", r3->numPart, '/',r3->denomPart);
	printf( ") \n\n" );
	simplify(r3);
	//simplify( r1->numPart, r1->denomPart, r2->numPart, r2->denomPart);
 
	printf( "(" );					/*r2 - r2 = r7 */
	//displayRational( r1 );
	printf("%d %c %d", r1->numPart, '/', r1->denomPart);
	printf( ") - (" );
	//displayRational( r2 );
	printf("%d %c %d", r2->numPart, '/', r2->denomPart);
	printf( ") = (" );
	//displayRational( r7 );
	printf("%d %c %d", r7->numPart, '/', r7->denomPart);
	printf( ") \n\n" );
 
	printf( "(" );					/* r2 * r3 = r4 */
	//displayRational( r2 );
	printf("%d %c %d", r2->numPart, '/', r2->denomPart);
	printf( ") * (" );
	//displayRational( r3 );
	printf("%d %c %d", r3->numPart, '/', r3->denomPart);
	printf( ") = (" );
	//displayRational( r4 );
	printf("%d %c %d", r4->numPart, '/', r4->denomPart);
	printf( ") \n\n" );
 
	printf( "(" );					/* r1 / r2 = r6 */
	//displayRational( r1 );
	printf("%d %c %d", r1->numPart, '/', r1->denomPart);
	printf( ") / (" );		
	//displayRational( r2 );
	printf("%d %c %d", r2->numPart, '/', r2->denomPart);
	printf( ") = (" );
	//displayRational( r6 );
	printf("%d %c %d", r6->numPart, '/', r6->denomPart);
	printf( ") \n\n" );
 
	printf( "(" );
	displayRational( r1 );
	printf( ") + (" );
	displayRational( r2 );
	printf( ") * (" );
	displayRational( r3 );
	printf( ") \n   = (" );
	displayRational( r5 );
	printf( ")\n\n" );
 
	r5 = cloneRational( r1 );
	displayRational( r1 );
	printf( " should be the same as " );
	displayRational( r5 );
	printf( "\n" );
 
	freeRational( r5 );
	printf( "\n\nThis should be a problem." );
	printf( "r5 was freed...\n" );
	printf( "\n" );
	displayRational( r5 );
	printf( "\n");
 
 
 
 
	return 0;
}
Rational initRational( int num, int denom )
{
	Rational r = (Rational) malloc( sizeof( RationalClass ));
	r->numPart = num;
	r->denomPart = denom;
 
	return r;
}
 
Rational cloneRational( Rational orig )
{
	Rational copy = ( Rational ) malloc( sizeof( RationalClass ));
	copy->numPart = orig->numPart;
	copy->denomPart = orig->denomPart;
	return copy;
}
void freeRational( Rational r )
{
	free(r);
}
 
int getNumPart( Rational r )
{
 return r->numPart;
}
 
int getDenomPart( Rational r )
{
	return r->denomPart;
}
 
 
Rational addRational( Rational r1, Rational r2 )
{
	Rational sum = ( Rational ) malloc( sizeof ( RationalClass ));
	sum->numPart = ( r1->numPart  * r2->denomPart ) + ( r1->denomPart * r2->numPart );
	sum->denomPart = ( r1->denomPart * r2->denomPart ); 
	simplify(sum);
	return sum;
	
}
 
Rational subRational( Rational r1, Rational r2 )
{
	Rational sub = ( Rational ) malloc( sizeof ( RationalClass ));
	sub->numPart = ( r1->numPart * r2->denomPart ) - ( r1->denomPart * r2->numPart );
	sub->denomPart = ( r1->denomPart * r2->denomPart );
	return sub;
}
 
Rational multRational( Rational r1, Rational r2 )
{
	Rational mult = ( Rational ) malloc( sizeof ( RationalClass ));
	mult->numPart = ( r1->numPart * r2->numPart );
	mult->denomPart = (r1->denomPart * r2->denomPart );
	return mult;
}
 
Rational divideRational( Rational r1, Rational r2 )
{
	Rational quot = ( Rational ) malloc( sizeof( RationalClass ));
	quot->numPart = ( r1->numPart * r2->denomPart );
	quot->denomPart = (r1->denomPart * r2->numPart );
	return quot;
}
 
int gcd( int num, int denom )
{
	int c;
	do {
	c = num % denom;
	num = denom;
	denom = c;
	} while (denom==0);
	return c;
}
 
Rational simplify( Rational r )
{
	Rational simp = ( Rational ) malloc( sizeof( RationalClass ));
	simp->numPart = r->numPart / gcd(r->numPart, r->denomPart);
	simp->denomPart = r->denomPart / gcd(r->numPart, r->denomPart);
	return simp;
}
 
Rational readRational ( FILE* f )
{
	Rational r = ( Rational ) malloc( sizeof( RationalClass ));
	fscanf( f, "%d %d", &( r->numPart ), &( r->denomPart ));
	return r;
}
 
void displayRational (Rational r )
{
	printf("%d", r->numPart );
	if( r->denomPart < 0 ) printf( "-" );
	else printf( "+" );
	printf( "%d", abs( r->denomPart ));
}
Attachments:
 
My code
 
[+][-]06.01.2008 at 08:25PM PDT, ID: 21689391

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.01.2008 at 09:56PM PDT, ID: 21689654

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.01.2008 at 10:09PM PDT, ID: 21689696

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.01.2008 at 10:25PM PDT, ID: 21689749

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.01.2008 at 10:27PM PDT, ID: 21689754

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.01.2008 at 11:02PM PDT, ID: 21689849

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.01.2008 at 11:04PM PDT, ID: 21689858

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.01.2008 at 11:55PM PDT, ID: 21690017

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.01.2008 at 11:58PM PDT, ID: 21690029

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.02.2008 at 05:07PM PDT, ID: 21696772

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.02.2008 at 08:23PM PDT, ID: 21697370

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.03.2008 at 09:31PM PDT, ID: 21706969

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.03.2008 at 09:34PM PDT, ID: 21706976

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.03.2008 at 10:17PM PDT, ID: 21707147

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.03.2008 at 10:19PM PDT, ID: 21707153

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.03.2008 at 10:31PM PDT, ID: 21707199

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.03.2008 at 10:35PM PDT, ID: 21707210

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.04.2008 at 09:33PM PDT, ID: 21716406

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.04.2008 at 09:38PM PDT, ID: 21716424

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: C, C++
Sign Up Now!
Solution Provided By: sunnycoder
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.04.2008 at 10:00PM PDT, ID: 21716520

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 12:19AM PDT, ID: 21716975

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628