Added Programs

- modified biketour with procedures
- modified producttable with functions
- modified squares with functions
develop
Julius 6 years ago
parent fbfe1e89ef
commit 2725cdedbc

@ -1,4 +1,4 @@
program tour; program login;
uses uses
md5, crt; md5, crt;
@ -38,19 +38,65 @@ hotels: array [0..4] of HOTEL = (
var var
readinput: string; readinput: string;
i, j, distance_total, hnum1, hnum2: integer; j, hnum1, hnum2: integer;
correct, exit_loop: boolean; correct: boolean;
usersuccess: boolean = false; usersuccess: boolean = false;
velocity, time: real; time: real;
{ Calculates the total distance between hotels based on the hotel number input. function distance_sum(hnum1, hnum2 :integer) : integer;
(Only numbers accepted!). After printing the total distance a velocity can be { Calculate the total distance sum. }
input to calculate the estimated time. }
procedure hotelmain;
var var
distance_total : integer = 0;
i : integer; i : integer;
begin begin
while exit_loop = false do if (hnum1 > hnum2) then
for i := (hnum1-2) downto (hnum2-1) do
distance_total := distance_total + hotels[i].conn.distance
else
for i := (hnum1-1) to (hnum2-2) do
distance_total := distance_total + hotels[i].conn.distance;
distance_sum := distance_total;
end;
procedure calc_time(disttot : integer);
{ Calculate the time needed for the way and print it. }
var
velocity : integer;
begin
Write('Velocity (km/h): ');
ReadLn(velocity);
time := (disttot / velocity);
WriteLn('Estimated Time: ', time: 10: 2, ' hours');
end;
procedure printway(hnum1, hnum2 : integer);
{ Print the whole way to the console. }
var
i : integer;
begin
Writeln('Tour:');
if hnum1 > hnum2 then
for i := hnum1 downto hnum2 do
begin
if (i <> hnum1) then
Write(' -> ');
Write(hotels[i-1].name)
end
else
for i := hnum1 to hnum2 do
begin
if (i <> hnum1) then
Write(' -> ');
Write(hotels[i-1].name)
end;
Writeln;
end;
procedure calcway;
{ Calculate the sum of all distances and call functions
that use this value. }
var
i, distance_total : integer;
begin begin
distance_total := 0; distance_total := 0;
for i := 0 to 4 do for i := 0 to 4 do
@ -63,44 +109,45 @@ begin
Writeln; Writeln;
Write('Second Hotel Number: '); Write('Second Hotel Number: ');
ReadLn(hnum2); ReadLn(hnum2);
printway(hnum1, hnum2);
If ((hnum1 <= 5) and (hnum2 <= 5)) then If ((hnum1 <= 5) and (hnum2 <= 5)) then
begin begin
distance_total := 0; distance_total := distance_sum(hnum1, hnum2);
if (hnum1 > hnum2) then Writeln('Distance between hotels is ', distance_total, ' km');
for i := (hnum1-2) downto (hnum2-1) do calc_time(distance_total);
distance_total := distance_total + hotels[i].conn.distance end;
else
for i := (hnum1-1) to (hnum2-2) do
distance_total := distance_total + hotels[i].conn.distance;
Writeln('Distance between hotels is ', distance_total);
Write('Velocity (km/h): ');
ReadLn(velocity);
time := (distance_total / velocity);
WriteLn('Estimated Time: ', time: 10: 2, ' hours');
end; end;
{ Calculates the total distance between hotels based on the hotel number input.
(Only numbers accepted!). After printing the total distance a velocity can be
input to calculate the estimated time. }
procedure hotelmain;
var
i : integer;
exit : boolean = false;
begin
while exit = false do
begin
calcway;
Write('Exit? (y/n)'); Write('Exit? (y/n)');
ReadLn(readinput); ReadLn(readinput);
if readinput = 'y' then if readinput = 'y' then
exit_loop := true exit := true
else else
exit_loop := false; exit := false;
end; end;
end; end;
begin procedure login;
TextColor(DarkGray); var
Writeln('Tourplanner'); i : integer;
Writeln('Written by J.Riegel.');
TextColor(White);
{ endless loop/main loop of the program. Exit with CTRL-C }
while true do
begin begin
Write('User: '); Write('User: ');
ReadLn(readinput); ReadLn(readinput);
usersuccess := false; usersuccess := false;
correct := false;
{ Iterate over all 5 user entries to find the one { Iterate over all 5 user entries to find the one
that fits the input. set usersuccess to true if found that fits the input. set usersuccess to true if found
@ -125,13 +172,8 @@ begin
for j := 0 to 2 do for j := 0 to 2 do
begin begin
Write('Password: '); WriteLn('Password: ');
cursoroff;
TextColor(Black);
TextBackground(Black);
ReadLn(readinput); ReadLn(readinput);
TextColor(White);
cursoron;
if MD5Print(MD5String(readinput)) = users[i].pw then if MD5Print(MD5String(readinput)) = users[i].pw then
begin begin
@ -140,11 +182,7 @@ begin
Break; Break;
end end
else else
begin
TextColor(Red);
WriteLn('Wrong password! ', 2 - j, ' trys left.'); WriteLn('Wrong password! ', 2 - j, ' trys left.');
TextColor(White);
end;
end; end;
{ @section userfunctions } { @section userfunctions }
if correct = true then if correct = true then
@ -153,10 +191,16 @@ begin
users[i].l := true; users[i].l := true;
end; end;
if usersuccess = false then if usersuccess = false then
begin
TextColor(Red);
WriteLn('User not found'); WriteLn('User not found');
TextColor(White);
end;
end; end;
begin
TextColor(8);
Writeln('Tourplanner');
Writeln('Written by J.Riegel.');
TextColor(White);
{ endless loop/main loop of the program. Exit with CTRL-C }
while true do
login; { Login starts all the other functions }
end. end.

@ -0,0 +1,54 @@
program big_array;
uses dateutils, sysutils;
type
tup = record
num : integer;
sq : integer;
end;
var
a : array[0..10000] of tup;
procedure fill_array;
var
i: integer;
begin
for i := 0 to 10000 do
begin
a[i].num := i;
a[i].sq := i*i;
end;
end;
procedure do_something(arr: array of tup);
begin
arr[0].num := 1;
end;
procedure do_something_ref(var arr: array of tup);
begin
arr[2].num := 1;
end;
procedure measurement;
var
i : integer;
time : TdateTime;
begin
time := Timeof(NOW);
for i := 1 to 10000 do
do_something(a);
WriteLn('By Value', MilliSecondSpan(time, Timeof(now)):4);
time := Timeof(NOW);
for i := 1 to 10000 do
do_something_ref(a);
WriteLn('By Reference: ', MilliSecondSpan(time, Timeof(now)):4);
end;
begin
Writeln;
fill_array;
measurement;
end.

@ -0,0 +1,34 @@
program faculty;
function faculty_it(num: integer) : longint;
var
val: longint = 1;
i: integer;
begin
for i := 2 to num do
begin
val := val * i;
end;
faculty_it := val;
end;
function faculty_rec(num: integer): longint;
begin
if num = 1 then
faculty_rec := num
else
faculty_rec := num * faculty_rec(num-1);
end;
var
input : integer;
begin
Writeln('Number: ');
Read(input);
Writeln;
Writeln('Faculty IT: ', faculty_it(input));
Writeln('Faculty REC: ', faculty_rec(input));
end.

@ -0,0 +1,22 @@
program greater;
function greatest(num1, num2:integer) : integer;
{ Returns the greater of two values. }
begin
if (num1 > num2) then
greatest := num1
else
greatest := num2;
end;
var
num1, num2, ret : integer;
begin
Writeln('Number 1: ');
Read(num1);
Writeln('Number 2: ');
Read(num2);
Writeln;
Writeln(greatest(num1, num2));
end.

@ -2,16 +2,42 @@ program producttable;
var var
table: array [0..20, 0..20] of integer; table: array [0..20, 0..20] of integer;
i, j: integer;
procedure fill_table_y(x, sizel, sizeh: integer);
var
j: integer;
begin begin
for i := 0 to 20 do for j := sizel to sizeh do
begin begin
for j := 0 to 20 do table[x, j] := x*j;
end;
end;
procedure fill_table(sizel, sizeh : integer);
var
i: integer;
begin begin
table[i, j] := i*j; for i := sizel to sizeh do
Write(table[i, j] : 3, ' '); fill_table_y(i, sizel, sizeh);
end; end;
procedure print_table_y(x, sizel, sizeh: integer);
var
j: integer;
begin
for j := sizel to sizeh do
Write(table[x, j]:4);
Writeln; Writeln;
end; end;
procedure print_table(sizel, sizeh :integer);
var i : integer;
begin
for i := sizel to sizeh do
print_table_y(i, sizel, sizeh);
end;
begin
fill_table(0, 20);
print_table(3, 7);
end. end.

@ -1,26 +1,40 @@
program squares; program squares;
procedure squares_for;
var var
i : integer; i : integer;
begin
begin;
for i := 0 to 20 do for i := 0 to 20 do
Write(i*i, ' '); Write(i*i, ' ');
Writeln; Writeln;
end;
i := 0; procedure squares_while;
var
i : integer = 0;
begin
while i <= 20 do while i <= 20 do
begin begin
Write(i*i, ' '); Write(i*i, ' ');
i := i+1; i := i+1;
end; end;
Writeln; Writeln;
end;
i := 0; procedure squares_repeat;
var
i : integer = 0;
begin
repeat repeat
begin begin
Write(i*i, ' '); Write(i*i, ' ');
i := i+1; i := i+1;
end; end;
until i > 20; until i > 20;
end;
begin;
squares_for;
squares_while;
squares_repeat;
end. end.
Loading…
Cancel
Save