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

  How many processes a computer can efficiently support

Show the factors that you think would limit how many processes a computer can efficiently support?

  Catching exception

Create a program that shows how various exceptions are caught with catch Exception

  Information technology security

Assume you are an independent consultant who specializes in IT security issues. You have been retained through the Designer Distributions corporation, a mid-sized and growing customer goods distribution corporation.

  What operating system use to edit audio and video clips

Design and edit audio and video clips for school projects. What operating system must library consider for this facility? Explain why?

  Calculate cocomo effort

Calculate COCOMO effort, TDEV, average sta?ng, and productivity for an organic project that is estimated to be 39,800 lines of code.11. Calculate the unadjusted function points for the problem description of Problem 2.

  Describing the gnu public license and its role

Write a 1- to 2-page paper defining and describing the GNU public license and its role and Write a 1- to 2-page paper defining and comparing open source and closed source operating systems. Provide examples of each

  Determine the exact size of windows kernel

Determine the Exact Size of Windows Kernel [All Versions] give detail description of topic and include different operating systems Like Linux and Solaris.

  Convert the program of project 3 to get its input data file

The following is a sample run in the the case that no input file was specified on the command line and the test file proj3-input.txt was specified by the user. User input is in bold blue:

  What was the average processing time of the 10 new jobs

What was the average processing time of the 10 new jobs and what was the average time in the queue for the 10 new jobs?

  Tasks that a linux administrator performs

It is clear that the HWBS administration staff is a junior staff that has mostly focused on Windows servers. They are concerned that the tasks that they will need to learn will cause a tremendous amount of unneeded stress. Describe to the staff th..

  The lamport''s clock and vector clock equations

If each process uses a different value for d in the Lamport's clock and vector clock equations, will the logical clocks and vector clocks schemes satisfy the total order relation => and the relation: a â--> b iff t^a

  Switching elements in nxn banyan network

A stage of n x n banyan network consists of (n/2) 2x2 switching elements. 1st stage directs packets to the correct half of the network.

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