From 2725cdedbc52f2802e7e79ef836c4f3df1932eea Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 22 Nov 2018 13:45:26 +0100 Subject: [PATCH] Added Programs - modified biketour with procedures - modified producttable with functions - modified squares with functions --- biketour/tour.pas | 146 +++++++++++++++++++++++++++--------------- test/big_array.pas | 54 ++++++++++++++++ test/faculty.pas | 34 ++++++++++ test/greater.pas | 22 +++++++ test/producttable.pas | 42 +++++++++--- test/squares.pas | 22 +++++-- 6 files changed, 257 insertions(+), 63 deletions(-) create mode 100644 test/big_array.pas create mode 100644 test/faculty.pas create mode 100644 test/greater.pas diff --git a/biketour/tour.pas b/biketour/tour.pas index 8ee14cd..12fc182 100644 --- a/biketour/tour.pas +++ b/biketour/tour.pas @@ -1,4 +1,4 @@ -program tour; +program login; uses md5, crt; @@ -38,21 +38,67 @@ hotels: array [0..4] of HOTEL = ( var readinput: string; - i, j, distance_total, hnum1, hnum2: integer; - correct, exit_loop: boolean; + j, hnum1, hnum2: integer; + correct: boolean; usersuccess: boolean = false; - velocity, time: real; + time: real; -{ 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; +function distance_sum(hnum1, hnum2 :integer) : integer; +{ Calculate the total distance sum. } var + distance_total : integer = 0; i : integer; begin - while exit_loop = false do - begin - distance_total := 0; + 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 + distance_total := 0; for i := 0 to 4 do begin distance_total += hotels[i].conn.distance @@ -63,44 +109,45 @@ begin Writeln; Write('Second Hotel Number: '); ReadLn(hnum2); + printway(hnum1, hnum2); If ((hnum1 <= 5) and (hnum2 <= 5)) then begin - distance_total := 0; - 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; - Writeln('Distance between hotels is ', distance_total); - Write('Velocity (km/h): '); - ReadLn(velocity); - time := (distance_total / velocity); - WriteLn('Estimated Time: ', time: 10: 2, ' hours'); + distance_total := distance_sum(hnum1, hnum2); + Writeln('Distance between hotels is ', distance_total, ' km'); + calc_time(distance_total); 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)'); ReadLn(readinput); if readinput = 'y' then - exit_loop := true + exit := true else - exit_loop := false; + exit := false; end; end; +procedure login; +var + i : integer; begin - TextColor(DarkGray); - Writeln('Tourplanner'); - Writeln('Written by J.Riegel.'); - TextColor(White); - - { endless loop/main loop of the program. Exit with CTRL-C } - while true do - begin - Write('User: '); + Write('User: '); ReadLn(readinput); usersuccess := false; + correct := false; { Iterate over all 5 user entries to find the one that fits the input. set usersuccess to true if found @@ -125,13 +172,8 @@ begin for j := 0 to 2 do begin - Write('Password: '); - cursoroff; - TextColor(Black); - TextBackground(Black); + WriteLn('Password: '); ReadLn(readinput); - TextColor(White); - cursoron; if MD5Print(MD5String(readinput)) = users[i].pw then begin @@ -140,11 +182,7 @@ begin Break; end else - begin - TextColor(Red); - WriteLn('Wrong password! ', 2 - j, ' trys left.'); - TextColor(White); - end; + WriteLn('Wrong password! ', 2 - j, ' trys left.'); end; { @section userfunctions } if correct = true then @@ -153,10 +191,16 @@ begin users[i].l := true; end; if usersuccess = false then - begin - TextColor(Red); - WriteLn('User not found'); - TextColor(White); - end; - end; + WriteLn('User not found'); +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. diff --git a/test/big_array.pas b/test/big_array.pas new file mode 100644 index 0000000..1000dda --- /dev/null +++ b/test/big_array.pas @@ -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. \ No newline at end of file diff --git a/test/faculty.pas b/test/faculty.pas new file mode 100644 index 0000000..9f9d8a8 --- /dev/null +++ b/test/faculty.pas @@ -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. \ No newline at end of file diff --git a/test/greater.pas b/test/greater.pas new file mode 100644 index 0000000..a137336 --- /dev/null +++ b/test/greater.pas @@ -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. \ No newline at end of file diff --git a/test/producttable.pas b/test/producttable.pas index f1c8397..ee6c672 100644 --- a/test/producttable.pas +++ b/test/producttable.pas @@ -2,16 +2,42 @@ program producttable; var table: array [0..20, 0..20] of integer; - i, j: integer; +procedure fill_table_y(x, sizel, sizeh: integer); +var + j: integer; begin - for i := 0 to 20 do + for j := sizel to sizeh do begin - for j := 0 to 20 do - begin - table[i, j] := i*j; - Write(table[i, j] : 3, ' '); - end; - Writeln; + table[x, j] := x*j; end; +end; + +procedure fill_table(sizel, sizeh : integer); +var + i: integer; +begin + for i := sizel to sizeh do + fill_table_y(i, sizel, sizeh); +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; +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. \ No newline at end of file diff --git a/test/squares.pas b/test/squares.pas index c29c38f..263a7e8 100644 --- a/test/squares.pas +++ b/test/squares.pas @@ -1,26 +1,40 @@ program squares; +procedure squares_for; var i : integer; - -begin; +begin for i := 0 to 20 do Write(i*i, ' '); Writeln; +end; - i := 0; +procedure squares_while; +var + i : integer = 0; +begin while i <= 20 do begin Write(i*i, ' '); i := i+1; end; Writeln; +end; - i := 0; +procedure squares_repeat; +var + i : integer = 0; +begin repeat begin Write(i*i, ' '); i := i+1; end; until i > 20; +end; + +begin; + squares_for; + squares_while; + squares_repeat; end. \ No newline at end of file