Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-s9k8s Total loading time: 0 Render date: 2024-09-27T03:45:56.654Z Has data issue: false hasContentIssue false

Program 3 - Rational Reckoner

Published online by Cambridge University Press:  05 July 2014

Get access

Summary

This program illustrates the use of a hierarchical library to create an abstract data type and its associated operations. It is based on the type Rational of Exercise 12.2(3).

The root package Rational_Numbers declares the type Rational and the usual arithmetic operations. The private child package Rational_Numbers.Slave contains the functions Normal and GCD as in Exercise 13.4(1). Thus Normal cancels any common factors in the numerator and denominator and returns the normalized form. It is called by various operations in the parent package. It would be possible to restructure Normal so that it was a private child function.

Appropriate input-output subprograms are declared in the public child Rational_Numbers.IO.

In order to exercise the rational subsystem a simple interpretive calculator is added as the main subprogram. This enables the user to read in rational numbers, perform the usual arithmetic operations upon them and print out the result.

The numbers are held in a stack which is operated upon in the reverse Polish form (Polish after the logician Jan/Lucasiewicz). The root package Rat_Stack contains subprograms Clear, Push and Pop. The private child package Rat_Stack.Data contains the actual data, and the public child procedure Rat_Stack.Print_Top prints the top item. This structure is somewhat similar to that described in Section 13.4 and enables the stack size to be changed with minimal effect on the rest of the system.

The main subprogram is decomposed into subunits Process and Get_Rational in order to isolate the dependencies and clarify the structure.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2014

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Rational Reckoner
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.021
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Rational Reckoner
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.021
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Rational Reckoner
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.021
Available formats
×