Gliwice, 27.05.2012
Semester: 2
Group: 3
Section: 1
Computer Programming Laboratory
Author: Przemysław Kozieł
E-mail: miltiades11@wp.pl
Tutor: dr inż. Piotr Fabian
1. TASK TOPIC
Fractions. Write a program operating on fractions like 1/2 or 8/7. The program should at least add them, subtract, multiply and divide. The program should include a the object
that is a fraction, all operations executed by fractions should be in the class.
These operations should be implemented as overloaded operators. Additionally, the program should be able to manually enter the values of fractions and execution actions, and ask the user for any required value and what operation you want to accomplish.
2. PROJECT ANALYSIS
a) data structures used in the program with brief explanation why were chosen:
Before starting the implementation of a program, I thought to use only a few variables, which would include the numerators and denominators of fractions. But it isn't good idea, because when I would start writing a function, we can commit easy mistake in numerators
or denominators of fractions. This way produces a lot of lines of code. After analysis I rejected this idea. In second way I thought to use the structure as a fraction, and operations with fractions described as separate functions. But it does not satisfy all targets the task. After the lecture I saw how to use class and other method create a function and overloading operators. And I decided that I use class and overloading operators, I think it is the best way to complete targets from task. I think that class is very useful in this case, because first of all we create a object "fraction" and do a operations on this fraction. We have all variables and function in one class, and the possibility of errors is reduced.
b) algorithms used to solve a problem:
The program not needed any complex algorithms. Add, subtract, multiply and divide fractions are elementary operations. The only interesting algorithms in this program may be looking for Least Common Multiple and Greatest Common Divisor. These algorithms are used for adding, subtracting, and the simplification of fractions.
c) problem analysis:
The first problem in my analysis, it was like to show the fraction of the programming. Because there are many possibilities of interpretation, but I had to choose the best. After a thorough analysis, I decided to use the classes as much facilitated the work. The problem was that the variable used, I decided to use "signed long long int" because it has greatest range. When creating operations with fractions were no problem. There was a problem with the situation when the numerator or denominator exceeds range of a variable, so I had to take care of this exception.
3. EXTERNAL SPECIFICATION
Program is used for do operation of fractions. Program can add, subtract, multiply and divide fractions. We can use program in two ways:
a) manually,
For manual use of the program requires basic programming skills. We need to create a fraction, then using the basic mathematical operations (characters are the same, through the use of overloaded operators do not need additional functions). To add a "+", subtraction "-", multiplication "*" and division "/". The result of actions we can see by displaying functions pertaining to the class for a fraction. If our results will have the name "Final" is the result we can show by using the "Final.Show ()".
b) automatically,
If we turn on the program. We see screen, on screen is menu, first of all we must decide how we want to do with fractions. When we decide, we must select right option, 1- for add, 2- for subtraction, 3- multiplication, 4- for division, and q- for quit. Every option are clear. Program asking about all required value, and when finish operation show result. The program is designed so that the person having no programming knowledge can use it without problem. Program asking when need numerator or denominator of fraction, and waiting or user. When user write value in incorrect form, program asking again about it. We see this on screenshots, on them we can see all possibilities screens in program.
Screenshots:
Main menu:
Add:
Substraction:
Multiplication:
Division:
4. INTERNAL SPECIFICATION
The fundamental element of program is a class Fraction, which contains the primary variable which is the fraction and function for operation on fractions. Fraction consists of numerator, denominator and total(that is emitted after the operations). This class contains function such as:
- constructor without parameters - this constructor set all variable of fractions on zero value.
- deconstructor without parameters - it empty and unused.
- function Show without parameters - this function printing on screen value of fraction from which is used. For this is use cout stream.
- function SimplifyFraction without parameters- this function simply fraction. If numerator is higher than denominator, from this function we get total value, and numerator is simplify when we can emit total.
- function SimplifyNumDen without parameters- this function is used to simplify numerator and denominator to simplier form. When in the numerator and denominator we have big value, for operation is better to simplify this, and this function do it. Use additionally variable which is equal to denominator and in loop decreases and check whether it is a divisor of numerator and denominator.
- function SetFraction without parameters- this function asking about value for numerator and denominator, and check and checks whether the values have been properly given if it is incorrect asks again.
- function SetFraction with two parameters- this function is use for manually set value of fraction, first parameter is numerator and second is denominator.
- function CheckNegative without parameters- check what values are in the numerator and denominators and what are signs if numerator and denominator are negative, converts them into positive values, if the denominator is negative it moves minus to numerator, if the numerator is negative, ignores and leaves it there.
Function with overloading operators for operations on fractions:
- function for operator "+" take one parameter, it is second needed fraction to this operation. Function check value of denominator and works in two ways, when denominators has the same value, only add numerator and returning value of this operation. Or second way if denominators has different value, in this case program multiply denominators, and set numerators for this new value of denominators. And after that operation is simply, only adding numerators and returning result.
- function for operator "-" take one parameter- it is second needed fraction to this operation. Function check value of denominator and works in two ways, when denominators has the same value, only substract numerator and returning value of this operation. Or second way if denominators has different value, in this case program multiply denominators, and set numerators for this new value of denominators. And after that operation is simply, only substract numerators and returning result.
- function for operator "*" take one parameters- is simply, in this case function multiply numerators, and denominators, and returning value. The parameter is the second fraction.
- function for operator "/" take one parameters- the parameter is second fraction to this operation. Function multiply numerator from first fraction with denominator from second, and denominator from first and numerator from second fraction. Function returning result of this operation.
All this function are secured if either of the variables exceeds the range of the variable.
5. TESTING
The tests consisted of a set different possibilities as a fraction and the conduct of operations. On a piece of checked whether the calculated values are correct. During testing I noticed a lot of mistakes and problems, such as accepting the wrong value(letters), slow search for a common denominator, minuses the denominator, the need to extract a total, and the ability to minimize and speed up the functions. All problems are fixed. The most important problem was with when variable get a value which exceed the range of this variable. I secured program with variable exceeds the range. The only limit for the operation of the calculator is the range variable. The input values in tests stated the everything: letters, zero values and negative, and huge numbers.
6.CONCLUSIONS
When creating the program drew a lot of applications, it is important to take into account all kinds of exceptions while running programs. Through this single exception there are many errors. I learned a lot of new features and syntax in C + +. Above all about classes, overloaded operators, and many smaller features. Work on this project was very enjoyable, it seemed at the beginning of a very simple, but at some point require a longer consider to choose the right solution. The program is designed to perform operations with fractions.