CS320 - OBJECT ORIENTED SOFTWARE DEVELOPMENT WITH C++
The following shows the exact same program written in both Pascal and C++.
The program has been contrived to illustrate a wide range of features, so don't
take it as a paradigm of efficiency or style (or usefulness)!
program gcd_lcm(input, output); |
|
(* This program inputs a specified | /* This program inputs a specified
number of pairs of integers, and | number of pairs of integers, and
for each prints the gcd and/or lcm | for each prints the gcd and/or lcm
as specified by the user. *) | as specified by the user. */
|
| #include <iostream.h>
var |
noPairs: integer; | int noPairs;
choice: char; | char choice;
oneA, oneB: integer; | int oneA, oneB;
i: integer; | int i;
|
function gcd(a, b: integer): integer; | int gcd(int a, int b)
(* Compute and return gcd of a and b *) | // Compute and return gcd of a and b
| {
var |
temp: integer; | int temp;
begin |
while b <> 0 do | while (b != 0)
begin | {
temp := a mod b; | temp = a % b;
a := b; | a = b;
b := temp | b = temp;
end; | }
gcd := a | return a;
end; | }
|
function lcm(a, b: integer): integer; | int lcm(int a, int b)
(* Compute and return lcm of a and b *) | // Compute and return lcm of a and b
| {
var |
temp, prod: integer; | int temp, prod;
begin |
(* The lcm is product div gcd | // The lcm is product / gcd
We'll compute gcd a different | // We'll compute gcd a different
way here just for fun! *) | // way here just for fun!
prod := a * b; | prod = a * b;
if (a = 0) or (b = 0) then | if (a == 0 || b == 0)
lcm := 0 | return 0;
else | else
begin | {
repeat | do
| {
temp := a mod b; | temp = a % b;
a := b; | a = b;
b := temp | b = temp;
| }
until b = 0; | while (b != 0);
lcm := prod div a; | return prod / a;
end | }
end; | }
|
procedure doIt(choice: char; | void doIt(char choice, int x, int y)
x, y: integer); |
(* | /*
Does the requested computations(s) | Does the requested computations(s)
for one pair of numbers. | for one pair of numbers.
*) | */
begin | {
case choice of | switch (choice)
| {
'g', 'G': | case 'g': case 'G':
writeln('gcd = ', | cout << "gcd = "
gcd(oneA,oneB)); | << gcd(oneA, oneB)
| << endl;
| break;
|
'l', 'L': | case 'l': case 'L':
writeln('lcm = ', | cout << "lcm = "
lcm(oneA, oneB)); | << lcm(oneA, oneB)
| << endl;
| break;
|
'b', 'B': | case 'b': case 'B':
writeln('gcd, lcm = ', | cout << "gcd, lcm = "
gcd(oneA, oneB), | << gcd(oneA, oneB)
', ', | << ", "
lcm(oneA, oneB)); | << lcm(oneA, oneB)
| << endl;
| break;
|
otherwise | default:
writeln('Unknown choice'); | cout << "Unknown choice"
| << endl;
end | }
end; | }
|
begin | main()
| {
write('How many pairs? '); | cout << "How many pairs? ";
readln(noPairs); | cin >> noPairs;
| while (cin.get() != '\n') ;
|
for i := 1 to noPairs do | for (i = 1; i <= noPairs; i++)
begin | {
|
write('Enter first letter of ', | cout<<"Enter first letter of "
'choice (Gcd,Lcm,Both) ', | <<"choice (Gcd,Lcm,Both) "
'then two numbers: '); | <<"then two numbers: ";
readln(choice, oneA, oneB); | cin >> choice >> oneA >> oneB;
| while (cin.get() != '\n') ;
doIt(choice, oneA, oneB) | doIt(choice, oneA, oneB);
|
end | }
end. | }