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

  Operating system that supports a one-to-one relationship

Given an operating system that supports a one-to-one relationship between user-level threads and kernel-level threads and allows one or more threads from a process to issue blocking system calls while other threads (within that process) continue to r..

  Java application using the java semaphore

Implementation for problem must only use the semaphore methods to control the concurrency of your solution - Implement a solution to this problem as a Java application using the Java semaphore

  Determine the hardware to be used and installation options

Determine the hardware to be used and the installation options. How will users log onto the systems? Explain

  Identify various hardware components and network topologies

Describe protocols at the different layers of the OSI model and explain their functionality

  Configuration and implementation of a cluster computing

Develop and submit a project plan including implementation steps for the configuration and implementation of a cluster computing solution to support a mission critical application

  Show the output of the screen when the program executes

Show the output of the screen when the program executes with a value of 200.

  Write a paper describing the operating systems

Write a paper describing the operating systems used by Huffman Trucking

  Question about network security

Suppose that you are the network security officer of one corporation, what will be your plan to enhance your network security firewall? VPN, Content level protection or combination of them?

  What are wireless solutions

Will this proposed solution meet the requirements? Would you propose the same? Explain your answer.

  Compare and contrast closed-source mobile operating systems

Compare and contrast open-source and closed-source mobile operating systems (OSs), and explain the main reasons why you would consider utilizing one (1) of these types of OSs over the other. Provide at least two (2) reasons to justify your decisio..

  Automatic diagnostic system

Sommerville recommended that objects manipulated through users should be drawn from their own domain rather that an computer domain.

  Purpose to apply the compensation knowledge in real-world

purpose to apply the compensation knowledge in real-world situation. the task formulating a compensation strategy for

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