RIsagara 0 Newbie Poster
/*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;
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;
}
yellowSnow 607 Posting Whiz in Training
Hi
Modify this where possible
And what exactly do you want to modify? Ask a specific question.
Beg.CProgrammer 0 Newbie Poster
"interger" is spelled "integer"
"valve" is spelled "value"
RIsagara 0 Newbie Poster
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,
RIsagara 0 Newbie Poster
/*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;
}
Edited by Dani because: Formatting fixed
RIsagara 0 Newbie Poster
Gentle thanks for the modification but I did not really mean sp
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.