Fraction Program Version I - Programming Assignment #3 | CS 1410, Lab Reports of Computer Science

Material Type: Lab; Class: SI Object-Oriented Programming; Subject: Computer Science; University: Weber State University; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 07/23/2009

koofers-user-4tl
koofers-user-4tl 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 2
CS 1410 Programming Assignment #3
Fraction Program (Version 1)
Assignment
A fraction is composed of two integers: a numerator and a denominator. There are many
operations in which fractions can participate; four operations are common: addition, subtraction,
multiplication, and division. This is just enough detail (data and functions) to make fractions
interesting demonstrations of structures and functions.
Program Description
1. In a file named fraction.h
a. Create a structure named fraction in with an int numerator and an int
denominator
b. Add function prototypes for the functions defined in step 2 below the struct; also add a
prototype for gcd (included with the assignment)
2. Define (i.e., write the code for) the fraction functions in a file named fraction.cpp
a. Include fraction.h
b. Define four arithmetic functions named add, sub, mult, and div – see Exercise 12, p.
129 for the formulas for the four basic arithmetic operations on fractions). Each function
should accept two fraction structure arguments passed by value and should return the
result as a new fraction structure returned by value.
c. Define a function named print, which accepts one fraction structure passed as a
reference and prints it as num/denom
d. Define function named read that
i. has a fraction pointer argument
ii. reads a fraction from the keyboard (2 separate prompts and 2 separate reads)
(1) prompts for and reads the numerator
(2) prompts for and reads the denominator
iii. returns the fraction through the pointer argument
3. At the end of each arithmetic function, the fraction should be reduced to lowest terms
(however, improper fractions are okay). Reducing a fraction to lowest terms involves finding
the greatest common divisor (i.e., the biggest integer that divides both the numerator and the
denominator evenly). Use the gcd function included with the assignment (copy it into your
program). Assume that the fraction to be reduced is in a fraction structure named f, then the
reduction is done by:
int common = gcd(f.numerator, f.denominator);
f.numerator /= common;
f.denominator /= common;
pf2

Partial preview of the text

Download Fraction Program Version I - Programming Assignment #3 | CS 1410 and more Lab Reports Computer Science in PDF only on Docsity!

Page 1 of 2

CS 1410 Programming Assignment

Fraction Program (Version 1)

Assignment

A fraction is composed of two integers: a numerator and a denominator. There are many operations in which fractions can participate; four operations are common: addition, subtraction, multiplication, and division. This is just enough detail (data and functions) to make fractions interesting demonstrations of structures and functions.

Program Description

  1. In a file named fraction.h a. Create a structure named fraction in with an int numerator and an int denominator b. Add function prototypes for the functions defined in step 2 below the struct; also add a prototype for gcd (included with the assignment)
  2. Define (i.e., write the code for) the fraction functions in a file named fraction.cpp a. Include fraction.h b. Define four arithmetic functions named add, sub, mult, and div – see Exercise 12, p. 129 for the formulas for the four basic arithmetic operations on fractions). Each function should accept two fraction structure arguments passed by value and should return the result as a new fraction structure returned by value. c. Define a function named print, which accepts one fraction structure passed as a reference and prints it as num/denom d. Define function named read that i. has a fraction pointer argument ii. reads a fraction from the keyboard (2 separate prompts and 2 separate reads) (1) prompts for and reads the numerator (2) prompts for and reads the denominator iii. returns the fraction through the pointer argument
  3. At the end of each arithmetic function, the fraction should be reduced to lowest terms (however, improper fractions are okay). Reducing a fraction to lowest terms involves finding the greatest common divisor (i.e., the biggest integer that divides both the numerator and the denominator evenly). Use the gcd function included with the assignment (copy it into your program). Assume that the fraction to be reduced is in a fraction structure named f, then the reduction is done by:

int common = gcd(f.numerator, f.denominator); f.numerator /= common; f.denominator /= common;

Page 2 of 2

  1. Create a simple calculator in a file named calc.cpp a. The program should loop, printing a menu showing the four arithmetic operations and the option of exiting: A add S subtract M multiply D divide E exit b. cin a character (char) to determine which operation the program should carry out. Make it so that your program accepts either upper or lower case letters. c. If the user selects the exit option, the program should terminate (either a break or the exit function may be used). d. Only after the user selects one of the arithmetic operations will the program the prompt for the input of two fractions (numerator first and then the denominator). It then carries out the selected operation and displays the results of the operation.

Implementation Notes

Strange things happen when you cin a char. The user enters a character and presses the enter key. This places both the entered character AND a new line character in the input stream. The cin reads the input character but leaves the new line. The sequence of operations prescribed here works because a numeric read follows each character read. The numeric read discards the new line while searching for something that can be converted to a number.

A productive way of approaching the assignment is to begin (ASAP) with the chapter 3portion (the loops, branches, input, etc.). In this case it might be useful to explicitly remove the new line character with the ignore function, which may be left in the completed calc function without causing errors. The following three statements accomplish this:

char command; cin >> command; cin.ignore();

Grading

Upload three files (fraction.h, fraction.cpp, and calc.cpp) to WSU Online for grading. Please do not zip the files. Please make a sub-directory on WSU Online when uploading these files.