Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik...

22
Vorkurs Programmierungstechnik Vorkurs Programmierungstechnik Einführung in Pascal Einführung in Pascal Michael Gellner Michael Gellner Lehrstuhl für Lehrstuhl für Softwaretechnik am Softwaretechnik am Institut für praktische Institut für praktische Informatik der Informatik der Universität Rostock Universität Rostock

Transcript of Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik...

Page 1: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Vorkurs ProgrammierungstechnikVorkurs ProgrammierungstechnikEinführung in PascalEinführung in Pascal

Michael GellnerMichael Gellner

Lehrstuhl für Softwaretechnik Lehrstuhl für Softwaretechnik am Institut für praktische am Institut für praktische Informatik der Universität Informatik der Universität

RostockRostock

Page 2: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 2

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 1:Ein Programm, das die Zeichenkette ‘Hello World’ ausgibt.

Page 3: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 3

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program hello;

uses dos;

begin

writeln('Hello World');

readln;

end.

Page 4: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 4

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 2:Aufgabe 2:Ein Programm, das- zwei Zahlen entgegen nimmt, - diese addiert und - das Ergebnis ausgibt.

Page 5: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 5

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program summe;(*======Deklarationsteil=====*)uses dos;var a : real; b : real; c : real;(*=========Hauptteil=========*)begin Writeln(’Ein Programm zur Addition zweier Zahlen'); Writeln('Geben Sie die erste Zahl ein: '); Readln(a); Writeln('Geben Sie die zweite Zahl ein'); Readln(b);

c := a + b (* Berechnung *)

Writeln('Das Ergebnis ist: ', c:4:2); readln;end.

Page 6: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 6

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 3:Aufgabe 3:Ein Programm soll eine eingegebene Zahl darauf hin analysieren, ob sie gerade ist oder nicht, und das Ergebnis der Analyse ausgeben.

Page 7: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 7

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program Gerade;uses dos;var x : integer;begin writeln('Eine Zahl bitte:'); readln(x); if ((x mod 2) = 0) then begin write(x ,' ist'); writeln(' gerade'); end else begin write(x ,' ist'); writeln(' ungerade'); end; readln;end.

Page 8: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 8

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 4:Aufgabe 4:Ein Programm soll eine Zahl n entgegennehmen, bis zu der das Programm die Summe der Zahlen von 1 bis n durch Aufsummieren bildet.

Page 9: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 9

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program ForLoop;

uses dos;

var sum : integer;

i, n : integer;

begin

sum := 0;

readln(n);

for i := 0 to n do

begin

sum := sum + i;

end;

writeln(sum);

readln;

end.

Page 10: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 10

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 5Aufgabe 5Ein Programm, das Zahlen entgegen nimmt und die Summe der eingegebenen Zahlen bildet sowie ausgibt, bis der Anwender das Programm beenden will.

Page 11: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 11

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program RepeatLoop;uses dos;var x, y : integer; abbruch : string;begin x := 0; writeln('Dieses Programm addiert alle Zahlen,'); writeln('die von Ihnen eingegeben werden,'); writeln('bis Sie abbrechen'); repeat readln(y); x := x + y; writeln('x ist ',x); writeln; writeln('Abbrechen? (j/n)'); readln(abbruch); until (abbruch = 'J') or (abbruch = 'j');end.

Page 12: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 12

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 6:Aufgabe 6:Ein Programm, das OC in OF konvertiert.

VoraussetzungVoraussetzung

325

9

Celsius

Fahrenheit

tt

Page 13: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 13

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program Beispiel_6_Fahrenheit_Celsius;

var Fahrenheit : real;

Celsius : real;

begin

Readln(Celsius);

Fahrenheit := ((9 * Celsius) / 5) + 32;

Writeln(Fahrenheit:4:2);

Readln;

end.

Page 14: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 14

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 7:Aufgabe 7:Ein Programm, das OF in OC konvertiert.

VoraussetzungVoraussetzung

9

532

Fahrenheit

Celsius

tt

Page 15: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 15

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program Beispiel_7_Celsius_Fahrenheit;

var Fahrenheit : real;

Celsius : real;

begin

Readln(Fahrenheit);

Celsius := ((Fahrenheit - 32) * 5) / 9;

Writeln(Celsius:4:2);

Readln;

end.

Page 16: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 16

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 8:Aufgabe 8:Ein Programm, das OF in OC oder OC in OF konvertiert, je nachdem, was der Benutzer gerade eingibt.

Page 17: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 17

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program Beispiel_8_FahrToCels_and_CelsToFahr;

var Temperatur : real;

Eingabe : char;

weiter : char;

function FahrToCels(Fahrenheit : real) : real;

begin

FahrToCels := ((Fahrenheit - 32) * 5) / 9;

end;

function CelsToFahr(Celsius : real) : real;

begin

CelsToFahr := ((9 * Celsius) / 5) + 32;

end;

Page 18: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 18

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

begin

repeat

Writeln('Geben Sie eine Temperatur ein: ');

Readln(Temperatur);

Writeln('(a) Von Celsius zu Fahrenheit rechnen?');

Writeln('(b) Von Fahrenheit zu Celsius rechnen?');

Readln(Eingabe);

case Eingabe of

'a', 'A': Temperatur := CelsToFahr(Temperatur);

'b', 'B': Temperatur := FahrToCels(Temperatur);

else Writeln('Ein falscher Buchstabe!');

end;

Writeln(Temperatur:4:2);

Writeln('Weiter? (j oder n)');

Readln(weiter)

until ((weiter = 'n') or (weiter = 'N'))

end.

Page 19: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 19

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

Aufgabe 9:Aufgabe 9:Ein Programm, das (i) ein Feld mit Werten initialisiert, (ii) die Werte aufsummiert und (iii) die Werte ausgibt.Jeder der Schritte ist in einer eigenen Routine vorzunehmen.

Page 20: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 20

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

program ArraySum;

uses dos;

const max = 10;

type vector = array[0..max] of integer;

var a : vector;

x : integer;

procedure init(var feld : vector; size : integer);

var i : integer;

begin

for i := 0 to size do

begin

feld[i] := random(20);

end;

end;

Page 21: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 21

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

function sum(feld : vector; size : integer) : integer;var i : integer; s : integer;begin s := 0; for i := 0 to size do begin s := s + feld[i]; end; sum := s;end;

Page 22: Vorkurs Programmierungstechnik Einführung in Pascal Michael Gellner Lehrstuhl für Softwaretechnik am Institut für praktische Informatik der Universität.

Michael GellnerMichael Gellner 22

Vorkurs - Einführung in die Sprache PascalVorkurs - Einführung in die Sprache Pascal

procedure show(sumfeld : integer; feld : vector; size : integer);var i : integer;begin for i := 0 to size do begin writeln(feld[i]); end; writeln('Summe: ', sumfeld); readln;end;

begin Init(a, max); x := Sum(a, max); Show(x, a, max);end.