I was really disappointed of the lack of information about Pascal. I know that it is not very popular language now and there are many better languages around us. However, I do believe that many people need information and examples of Pascal tasks. Moreover, there are still some schools where students learn this language as their first programming experience.
Today I will try to introduce you with some fractures and how to divide them properly in Pascal.So the main point is to add some fractures and then get integer part and a new adjusted fracture.
We will try to write a procedure which will add to fractures.
procedure add_two(var a, b:integer; c, d:integer); var numerator, denominator:integer; begin numerator := a * d + c * b; denominator := b * d; a := numerator; b := denominator; end;
The second procedure which will we use is going to get the integer part of whole number and make nominator and denominator as small as we can get.
procedure exclude(var a, b, int_part:integer); var gcd:integer; // GCD value begin gcd := greatest_common_divider(abs(a), abs(b)); a := a div db; b := b div db; int_part := (a div b); a := a - (int_part * b); end;
In this example we are using and algorithm to find the greatest common divider which code you can found at my last entry on this blog.
And now we just need to make everything to one working and quite useful Pascal program which code you can see here or download from here.
WriteLn('How many fractures we will have'); ReadLn(n); WriteLn('Input fractures: '); ReadLn(a, b); for i := 2 to n do begin ReadLn(c, d); add_two(a, b, c, d); end; exclude(a, b, int_part); if int_part &lt;&gt; 0 then WriteLn('Result: ', int_part,' ', a, '/', b) else WriteLn('Result: ', a, '/', b); Readln;
The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. Cookies are only used for statistical purposes.