Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Operating System Expert

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

Operating System, Computer Science

  • Category:- Operating System
  • Reference No.:- M91614421
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Operating System

Discussion question this research assignment will give

Discussion Question : This research assignment will give further information on the nature and workings of multi-tasking and multi-processing operating systems. All information reported in this assignment is to be in the ...

Research types of operating systems that are currently

Research types of operating systems that are currently available and provide a scenario in which the operating system you chose would be appropriate to be used in this situation. Explain why you think the choice you made ...

Question you are a security administrator responsible for

Question: You are a security administrator responsible for providing secure configuration requirements for new laptop deployments. After reading Module 2 of Certified Secure Computer User v2exercises, apply the configura ...

Taskyour job in this assignment is to create two virtual

Task Your job in this assignment is to create two Virtual machines each running a different but the latest distribution of Linux e.g. Ubuntu Server and CentOS. Each of these VM's is to offer services to a user base. The ...

Catalog course descriptionin this course students carry out

Catalog Course Description In this course students carry out independent research in a significant technical area of information, network, and computer security. The student is to investigate a technical area, research i ...

Question note apa format 250 words and three reference

Question: Note: APA format 250 words and three reference without plagarism Computerized Operating Systems (OS) are almost everywhere. We encounter them when we use out laptop or desktop computer. We use them when we use ...

Question topic computerized operating systems os are almost

Question: Topic: Computerized Operating Systems (OS) are almost everywhere. We encounter them when we use out laptop or desktop computer. We use them when we use our phone or tablet. Find articles that describes the diff ...

Question 1answer the following questions 10 marks a

Question 1 Answer the following questions: 10 marks a. Consider the following page reference string: 3, 1, 4, 1, 2, 3, 5, 3, 2, 1, 2,5, 4, 3, 5, 2, 4,2, 5,3 Using the above page reference string display the contents of t ...

Question state the required answer precisely and then

Question : State the required answer precisely and then provide proper explanation. It is not enough to provide one- word or one-line answers. What is the purpose of the command interpreter? Why is it usually separate fr ...

State the required answer precisely and then provide proper

State the required answer precisely and then provide proper explanation. It is not enough to provide one- word or one-line answers. Consider a computer embedded in the control of a manned spacecraft. Would it require an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As