Basic arithemetic on rational numbers

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 6
Reputation: RIsagara is an unknown quantity at this point 
Solved Threads: 1
RIsagara RIsagara is offline Offline
Newbie Poster

Basic arithemetic on rational numbers

 
0
  #1
Oct 27th, 2009
Hi
Modify this where possible
Attached Files
File Type: txt FRACTIONS.txt (3.6 KB, 8 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 27th, 2009
Originally Posted by RIsagara View Post
Hi
Modify this where possible
And what exactly do you want to modify? Ask a specific question.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: Beg.CProgrammer is an unknown quantity at this point 
Solved Threads: 2
Beg.CProgrammer Beg.CProgrammer is offline Offline
Newbie Poster
 
0
  #3
Oct 27th, 2009
"interger" is spelled "integer"
"valve" is spelled "value"
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: RIsagara is an unknown quantity at this point 
Solved Threads: 1
RIsagara RIsagara is offline Offline
Newbie Poster
 
0
  #4
Oct 27th, 2009
Originally Posted by yellowSnow View Post
And what exactly do you want to modify? Ask a specific question.
you realize that it cannot give 0/4=0,
I also limited the user from entering 0,
modify the Gcd method to reduce the fraction recursively,
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: RIsagara is an unknown quantity at this point 
Solved Threads: 1
RIsagara RIsagara is offline Offline
Newbie Poster
 
0
  #5
Oct 27th, 2009
/*A PROGRAM PERFORMING BASIC ARITHMETIC ON RATIONAL NUMBERS*/

/*BY ISAGARA K ROBERT*/
/*26th october 2009*/
/*THIS WAS MY ASSIGNMENT(COMPUTER SCIENCE YEAR II) TO MR PAUL BAGYENDA ON BASIC ARITHMETIC OPERATION ON RATIONAL NUMBERS*/

#include <stdio.h>
#include <stdlib.h>



typedef struct rational_t {
int numer;
int denom;
} rational_t;
/*modify this for me*/
static int gcd(int a, int b)
{ int c;
while(a>0)
{
if(a<b)
{ c=a;
a=b;
b=c;
}
else
a=a%b;
}
return b;
}

/* r_init: creates rational number representing numer/denom and returns it*/
rational_t r_init(int numer, int denom)
{
rational_t fraction;
int GcD=gcd(numer,denom);
fraction.numer= numer/GcD;
fraction.denom= denom/GcD;
return fraction;
}

/* r_add: Returns a rational_t representing x+y */
rational_t r_add(rational_t x, rational_t y)
{
rational_t result;
result.numer=(x.numer*y.denom+ x.denom* y.numer);
result.denom=(x.denom*y.denom);
return result;
}

/* r_sub: Returns a rational_t representing x-y */
rational_t r_sub(rational_t x, rational_t y)
{
rational_t result;
result.numer=(x.numer*y.denom - x.denom* y.numer);
result.denom=(x.denom*y.denom);
return result;
}

/* r_mul: Returns a rational_t representing x*y */
rational_t r_mul(rational_t x, rational_t y)
{ rational_t result;
result.numer=(x.numer*y.numer);
result.denom=(x.denom*y.denom);
return result;
}

/* r_div: Returns a rational_t representing x/y */
rational_t r_div(rational_t x, rational_t y)
{
rational_t result;
result.numer=(x.numer*y.denom);
result.denom=(x.denom*y.numer);
return result;
}

/* r_toreal: Returns a double representing x */
double r_toreal(rational_t x)
{
}

/* r_print: prints rational as x/y (using printf */
void r_print(rational_t x)
{
int p=gcd(x.numer,x.denom);
printf("%d/%d",x.numer/p,x.denom/p);
}

int main(void)
{
printf("Rational arithmetic test program\n");
do {
int u,v, oper;
rational_t x, y, z;
here:
printf("x - numerator: ");
scanf("%d", &u);
if(u==0){
printf("Enter interger greater than Zero\n");
goto here;}
There1:
printf("x - denominator: ");
scanf("%d", &v);
if(v==0){
printf("Zero is undefined for a fraction ,Enter valve above Zero\n");
goto There1;
}
x = r_init(u,v);
here2:
printf("y - numerator: ");
scanf("%d", &u);
if(u==0){
printf("Enter interger greater than Zero\n");
goto here2;}
Thereback:
printf("y - denominator: ");
scanf("%d", &v);
if(v==0){
printf("Zero is undefined for a fraction ,Enter valve above Zero\n");
goto Thereback;
}
y = r_init(u,v);

printf("x is: ");
r_print(x);
printf("\n");

printf("y is: ");
r_print(y);
printf("\n");

printf("Operation (1=add,2=Sub,3=Mul,4=Div,0=End): ");
scanf("%d", &oper);

switch (oper) {
case 1:
z = r_add(x,y);
break;
case 2:
z = r_sub(x,y);
break;
case 3:
z = r_mul(x,y);
break;
case 4:
z = r_div(x,y);
break;
case 0:
return 0;
default:
printf("Unknown operation!\n");
break;
}

printf("z is: ");
r_print(z);
printf("\n");
} while(1);

return 0;
}
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 229 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC