Compile the code below and test it

Assignment Help Operating System
Reference no: EM13943555

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);

Reference no: EM13943555

Questions Cloud

Review the resources for victims & witnesses : Go to the Federal Bureau of Prisons' (BOP) Website, and review the "Resources For Victims & Witnesses,", Examine the significant manner in which the Crime Victims' Rights Act protects the rights of crime victims. Provide your opinion of the effecti..
How does a soaring dollar affect its profitability : Black & Decker Manufacturing Co. of Towson, Maryland, has roughly 45% of its assets and 40% of its sales overseas. How does a soaring dollar affect its profitability, both at home and abroad?
Heat transfer problems : HEAT TRANSFER PROBLEMS NO16- Water flowing in a steel pipe of diameter 0.02 m is to be cooled from 40 to 30 oc . The velocity of water in the steel pipe is 1.5 m/s .
Evaluate ethical issues faced by technology companies : Support your paper with a minimum of five (5) scholarly resources. In addition to these specified resources, other appropriate scholarly resources, including older articles, may be included.
Compile the code below and test it : 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 pr..
An asbestos pad, square in cross section : HEAT TRANSFER PROBLEM NO 17- An asbestos pad, square in cross section , measure 0.05 m on a side and increases linearly to 0.1 m on the other end ( see the figure given below.
Difference between substantive and procedural criminal law : What is the essential difference between substantive criminal law and procedural criminal law? Provide examples for each. Should morality, in and of itself, be a sufficient basis for defining particular conduct as criminal
How much additional transmitter power would be needed : Using the graph of BER vs CNR for BPSK, estimate the BER in clear air and in the specified rain conditions when no error correction is used, and when convolutional encoding with k = 7 is applied to the transmission. The bit rate is the same in eac..
Provide a relating table of instructor id name : Build a connection string that allows for a connection object to use the ODBC data source AriMiddle, which is bound to the database AriMiddle. ODBC Data Source Administrator is located on the control panel in Windows 98 or NT.

Reviews

Write a Review

Operating System Questions & Answers

  Implementation of algorithms for process management

The Shortest Job Next (SJN) algorithm queues processes in a way that the ones that use the shortest CPU cycle will be selected for running rst.

  Develop a user mode command interpreter

Develop a user mode command interpreter which support list-short.

  Memory allocation in operating system

Analysis and implementation of algorithms for memory allocation in operating system, Explain First- t and best- t methods are used in memory allocation in operating systems.

  Stand alone child process

Forking the child process

  Write a multi-threaded program

Write a multi-threaded program to solve producer and consumer problem

  Marginal and average cost curves

n a competitive market place (pure competition) is it possible to continually sell your product at a price above the average cost of production.

  Simulating operating systems scheduling

Simulate the long-term scheduler, the short-term scheduler and the I/O scheduler of the computer using the First-Come-First-Serve algorithm.

  Issues with trusted platform module

Research paper discussing the issues with Trusted Platform Module (TPM)

  Threads

Explain a complication that concurrent processing adds to an operating system.

  Design and programming

Use the semaphore methods to control the concurrency of the solution

  Virtual machines

Virtual machines supported by a host operating system

  Discuss an application that benefits barrier synchronization

Discuss an application that would benefit from the use of barrier synchronization

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd