CMSC 313 Project 2

Assigned |
Wednesday Sept 21, 2011 |

Program Due |
Sunday Oct 2, 2011 |

Points |
70 |

Update |
The original description of the commands
in the command file inadvertently included a terminating semicolon. This was
a typo. There are no semicolons in the command file. The command file section has
been updated.
23 Sep |

The Objective

The objective of this assignment is to become familiar using structs, writing and using functions and function prototypes, formatting ouput and reading input from a text file in a C application. Program design, creating a library of related functions, guarding header files, separate compilation, code reuse and makefiles are also important aspects of this project.The Task

Two of the basic units of elementary arithmetic are fractions and mixed numbers. In this project you will implement a structure to represent a fraction and a structure to represent a mixed number. Your program will read commands from a text file and perform the arithmetic operations specified.Note that while a correct implementation of fractions and mixed numbers is important, correct program design to achieve maximum abstraction and code reuse, creating and implementing an appropriate library of fraction functions, using guarded header file(s), proper use of "static" and creating a makefile are equally important in this project. Your project grade will be equally divided between functionality and these coding/design concepts.

How does your program work?

- Your program prompts the user for the name of the command file to process.
- Your program reads the command file and executes each command until end-of-file is reached. All commands must be echoed to the screen. See sample output below.

The command file

Each command in the command file will be on a separate line. Command arguments are separated by whitespace. All command names will be in upper-case as shown below. All command arguments will be valid. In this description, n1 is the numerator of the first fraction operand, d1 is the denominator of the first fraction operand, n2 is the numerator of the second fraction operand and d2 is the denominator of the second fraction operand.**ADD n1 d1 n2 d2**-- Add the two fractions specified by the arguments then print the result in the format

`(n1 / d1) + (n2 / d2) = x / y`where`x / y`is their sum.**SUBTRACT n1 d1 n2 d2**-- sbtract the two fractions specified by the arguments then print the result in the format

`(n1 / d1) - (n2 / d2) = x / y`where`x / y`is their difference..**MULTIPLY n1 d1 n2 d2**-- multiply the two fractions specified by the arguments then print the result in the format

`(n1 / d1) * (n2 / d2) = x / y`where`x / y`is their product.**DIVIDE n1 d1 n2 d2**-- divide the two fractions specified by the arguments then print the result in the format

`(n1 / d1) / (n2 / d2) = x / y`where`x / y`is their quotient.

Hints, Notes and Requirements

- You
use a struct to model a fraction.*must* - You
use a struct to model a mixed number.*must* - The numerators and denominators in the file may be negative. If both are negative, treat them both as positive. If only one of them is negative, treat the numerator as negative and the denominator as positive (being consistent makes your code easier).
- The numerator of a fraction may be zero (except for the DIVIDE command).
- The denominator of a fraction may be one. We guarantee that denominators are not zero.
- The results of all operations must be output in lowest terms (i.e. "reduced").

If the result of an operation is zero, print zero; do not print a fraction.

If the result of an operation is an improper fraction, output both the improper fraction in lowest terms and the equivalent mixed number. - Mixed numbers must be output in the simplest form possible.
If the the improper fraction is equivalent to an integer,
print the integer; do not print the fraction.
If the improper fraction is negative, print the whole number as negative.

For exampleThe improper fraction 7/3 would be printed as 2 1/3 The improper fraction 6/3 would be printed as 2 The improper fraction -7/3 would be printed as -2 1/3

- Your code must be separated into multiple .c files as follows.
Appropriate .h files are also required.
You are free to choose any names for your files.
- main( ) and helper functions specific to this project may be in the same .c file
- Since fraction-oriented functions are reusable, they must be placed in a separate .c file and their prototypes placed in an appropriately named .h file.
- Since mixed numbers are closely related to fractions their functions may be in the same file. You may choose to make a separate library for mixed numbers if you wish.

- You must submit a makefile that creates an executable named Project2 just by typing make at the Unix prompt. The compiler flags in our makefile must include -Wall.
- Proper use of static, typedef and #define required. Do not abuse the use of static to create variables with file scope in order to avoid passing arguments to functions.
- If you think about how you would implement a fraction class and a mixed number class in Java/OOP, the "methods" of the fraction and mixed number classes become the fraction and mixed number"library" that go into a separate .c file.
- Use incremental development.
- Implement the basic fraction functions first and unit test them.
- Write the primary processing part of main to handle just one command. Create a simple command file to test this code in main and the polynomial/term functions.
- Add more commands and functions.
- Rinse and repeat.
- Worry about the mixed numbers last.

- Several small fraction functions are better than a few large ones (think "code reuse"). Our implementation of this project resulted in more than 10 library functions almost all of which were less than 10 lines of code and some with as few as a single line. One or more application specific helper functions are also possible.
- gdb is your friend. Use it.
- Visit blackboard discussion board regularly. Create and share your own command files via blackboard.
- Struct assignment is your friend.
- Structs are passed to functions by value and may be used as function return types.
- You cannot use a string in a switch statement.
- Adding fractions does not require finding a common denominator. Use the general rule
(a / b) + (c / d) = (a * d + c * b) / (b * d) and then simplify to lowest terms. - We guarantee that the command file will be well-formed and that all data in the command file will be valid.
- Your code in main will be ugly and repetitive. Sorry, there's no good way to avoid it.
- Changing a fraction to lowest terms requires finding the greatest common divisor (gcd) of the numerator
and denominator. Pseudo code for Eucid's gcd algorithm using the subtraction method is given below.
It assumes that
and**a**are non-negative and that**b**is not zero.`b`function gcd(a, b) if a is zero, return b; while b is not zero if a > b subtract b from a else subtract a from b return a; end function

Hints

linux1[384]% Project2 Command File Name: cmds.txt ADD: 1, 2, 3, 4 (1/2) + (3/4) = 5/4 As mixed number: 1 1/4 MULTIPLY 2, 3, 4, 5 (2/3) * (4/5) = 8/15 ADD: 0, 2, 6, 10 (0) + (6/10) = 3/5 SUBTRACT 1, 2, 1, 4 (1/2) - (1/4) = 1/4 SUBTRACT 1, 2, 1, 2 (1/2) - (1/2) = 0 DIVIDE 1, 3, 4, 5 (1/3) / (4/5) = 5/12 ADD: -3, 6, -4, 12 (-3/6) + (-4/12) = -5/6 linux1[385]%

Project Grading

The project grade will be broken approximately evenly between functionality and coding as described below.- Functionality (35 points)

Note that your functionality score will be zero if your code does not compile or create an executable.- Basic cases - This might be a command file that performs each operation but uses fractions with the same denominator, or does not result in mixed numbers or simpilification to lowest terms.
- More complex cases - This might be a command file with commands whose numerators and/or denominators are negative, whose numerator is zero, whose result requires simplifying to lowest terms, whose result is an improper fraction, etc.
- Atypical cases - This might be an empty file, or a file that cannot be opened.
- Stress cases - This might be operations that use fractions with very large or very small numerators or denominators.

- Code (35 points)
- Compilation Requirements -
All .c files must compile without errors or warnings.

Your makefile must compile all .c files and create an executable named Project2. - Coding Requirements - we expect that you adhere to the project requirements listed above with regard to the use of #define, const variables, structs, typedefs, and static. We expect that your code produces all required output in the required format.
- Design - we expect that your code shows sufficient decompostion into appropriate functions used to create a fraction and mixed number library in a separate .c file. An appropriate header file should also be created.
- Style - we expect that your code adheres to the course coding standards, particularly with respect to function and file comments and to naming conventions.

- Compilation Requirements -
All .c files must compile without errors or warnings.

Submitting the Program

You can submit your project using the **submit** command.

submit cs313 Proj2 <list of .c and .h files> makefile

See this page for a description of other project submission related commands. To verify that your project was submitted, you can execute the
following command at the Unix prompt. It will show all files that
you submitted in a format similar to the Unix **'ls'** command.

submitls cs313 Proj2