Purchase Solution

Rational Numbers

Not what you're looking for?

Ask Custom Question

This code is not compiling, it consists of a header file (rational.h) and the code itself (rational.c). Please provide comment (like what the while loop does ...etc, so that the program is more understandable by anyone reading it). The original problem has been attached in this posting and the program is below.

------------------------------------------------------------------------------------------------
#include <stdio.h>
#include "rational.h"

const int ADD = 1 ;
const int SUBTR = 2 ;
const int MULT = 3 ;
const int DIV = 4 ;
const int EQ = 5 ;
const int LT = 6 ;
const int END = 7 ;

char* menu [] = {
"1. Add",
"2. Subtract",
"3. Multiply",
"4. Divide",
"5. Equals",a
"6. Less Than",
"7. End expression" } ;

int sign = 1 ; // sign is positive at start

static int prompt ();
static void show_result (rational* rat, rational rat1, int op) ;
static int GCD (int n, int d) ;
// int _tmain(int argc, _TCHAR* argv[])
int main ()
{
int num, denom ;

printf ("Enter a rational: ") ;
scanf ("%d %d", &num, &denom) ;

rational rat ;
rat = new_rat (num, denom) ;

printf ("Answer: ") ;
print_rat (rat) ;
printf ("n") ;

int op ;

while ((op = prompt ())!= END)
{
printf ("Enter a rational: ") ;
scanf ("%d %d", &num, &denom) ;
rational rat1 ;
rat1 = new_rat (num, denom) ;
show_result (&rat, rat1, op) ;
}

return 0;
}

static void show_result (rational* rat, rational rat1, int op)
{
rational res ;

int rel ;

switch (op)
{
case ADD: res = add_rat(*rat, rat1) ;break ;
case SUBTR: res = sub_rat (*rat, rat1) ; break ;
case MULT: res = mul_rat (*rat, rat1) ; break ;
case DIV: res = div_rat(*rat, rat1) ;break ;
case EQ: rel = eq_rat (*rat, rat1) ; break ;
case LT: rel = lt_rat (*rat, rat1) ;break ;
default: printf ("Wrong operation. Try again."); break;
}

printf ("Answer: ") ;

if (EQ == op || LT == op)
{
if (TRUE == rel )
printf ("TRUE") ;
else
printf ("FALSE") ;
}
else
{
print_rat (res) ;
*rat = res ;
}
printf ("n") ;
}

static int prompt ()
{
int o ;
int i ;
for (i = 0 ; i < sizeof (menu)/ (sizeof(char*)); i++ )
{
printf (menu[i]) ; printf ("n") ;
}
printf ("Enter an operation: ") ;
scanf ("%d", &o) ;

return o ;
}

rational new_rat(int numerator, int denominator)
{
rational r ;

int g ;
g = GCD (numerator, denominator) ;

numerator /= g ;
denominator /= g ;

r = numerator ;
r = r << 32 ;
r += denominator ;
return r ;
}

static int GCD (int n, int d)
{
int rem ;

if (n < 0) n = -n ;

while (1)
{
rem = n % d ;
if (0 == rem)
{
return d ;
}

n = d ;
d = rem ;
}
}

void print_rat(rational x)
{
int num, denom ;
num = x >> 32 ;
denom = x & 0xffffffff ;
if (-1 == sign)
printf ("-") ;
printf ("%d/%d", num, denom) ;
}

rational add_rat(rational x, rational y)
{
int n1, d1, n2, d2 ;

n1 = x >> 32 ;
n1 *= sign ;
d1 = x & 0xffffffff ;

n2 = y >> 32 ;
d2 = y & 0xffffffff ;

int lcm ; // denominator

lcm = (d1 * d2 ) / GCD (d1, d2) ;

int num ;

num = (lcm / d1) * n1 + (lcm / d2) * n2 ;

if (num < 0)
{
num = -num ;
sign = -1 ;
}
else
sign = 1 ;

rational rat ;

rat = new_rat (num, lcm) ;

return rat ;
}

rational sub_rat(rational x, rational y)
{
int n1, d1, n2, d2 ;

n1 = x >> 32 ;
n1 *= sign ;
d1 = x & 0xffffffff ;

n2 = y >> 32 ;
d2 = y & 0xffffffff ;

int lcm ; // denominator

lcm = (d1 * d2 ) / GCD (d1, d2) ;

int num ;

num = (lcm / d1) * n1 - (lcm / d2) * n2 ;

if (num < 0)
{
num = -num ;
sign = -1 ;
}
else
sign = 1 ;

rational rat ;

rat = new_rat (num, lcm) ;

return rat ;
}

rational mul_rat(rational x, rational y)
{
int n1, d1, n2, d2 ;

n1 = x >> 32 ;
n1 *= sign ;
d1 = x & 0xffffffff ;

n2 = y >> 32 ;
d2 = y & 0xffffffff ;

rational rat ;

rat = new_rat (n1 * n2, d1 * d2) ;

return rat ;

}

rational div_rat(rational x, rational y)
{
int n1, d1, n2, d2 ;

n1 = x >> 32 ;
d1 = x & 0xffffffff ;

n2 = y >> 32 ;
d2 = y & 0xffffffff ;

rational rat ;

rat = new_rat (n1 * d2, d1 * n2) ;
return rat ;
}

int eq_rat(rational x, rational y)
{
int n1, d1, n2, d2 ;

n1 = x >> 32 ;
d1 = x & 0xffffffff ;

n2 = y >> 32 ;
d2 = y & 0xffffffff ;

if (n1 == n2 && d1 == d2)
return TRUE ;
else
return FALSE ;

}
int lt_rat(rational x, rational y)
{
int s = sign ;
sub_rat (x, y) ;

int ret ;
if (-1 == sign) ret = TRUE ;
else ret = FALSE ;

sign = s ;

return ret ;
}
------------------------------------------------------------------------------------------------

header file (rational.h)
------------------------------------------------------------------------------------------------
#define TRUE 1
#define FALSE 0

typedef long long int rational;

rational new_rat(int numerator, int denominator);
void print_rat(rational x);
rational add_rat(rational x, rational y);
rational sub_rat(rational x, rational y);
rational mul_rat(rational x, rational y);
rational div_rat(rational x, rational y);
int eq_rat(rational x, rational y);
int lt_rat(rational x, rational y);
------------------------------------------------------------------------------------------------

Attachments
Purchase this Solution

Solution Summary

The expert examines rational numbers midifying codes.

Purchase this Solution


Free BrainMass Quizzes
Javscript Basics

Quiz on basics of javascript programming language.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.