Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2012-01-25T14:40:49+00:00 → 2012-01-26T07:38:41+00:00
<language>Pascal</language> | |
<b>Quadratische Lösungsformel</b> | |
Entwickle ein Programm, dass bei der Vorgabe von <math>p</math> und <math>q</math> für eine quadratische Gleichung der Form <math>x^2 + px + q = 0</math> die Lösungen berechnet und ausgibt. | |
Beachte, dass es auch genau eine oder keine Lösung geben kann. | |
<i>Tipp:</i> | |
Verwende die Lösungsformel <math>x_{1,2} = - \frac{p}{2} \pm \sqrt{(\frac{p}{2})^2 - q}</math>. Bestimmt zunächst die Diskriminante <math>D</math>. | |
<code id="4f200de2dfe59"> | |
procedure QuadratischeLoesung(p, q : real); | |
var | |
D : real; | |
x1, x2 : real; | |
begin | |
writeln('Berechnung von: x^2 + '+FloatToStr(p)+'x + '+FloatToStr(q)+' = 0'); | |
D := sqr(p / 2.0) - q; | |
writeln('Diskriminante: '+FloatToStr(D)); | |
if (D < 0) then | |
begin | |
writeln('Es gibt keine Lösung!'); | |
end | |
else if (D = 0) then | |
begin | |
writeln('Es gibt genau eine Lösung:'); | |
x1 := - p / 2.0; | |
writeln('x = '+FloatToStr(x1)); | |
end | |
else | |
begin | |
writeln('Es gibt genau zwei Lösungen:'); | |
x1 := - p / 2.0 + sqrt(D); | |
x2 := - p / 2.0 - sqrt(D); | |
writeln('x1 = ' + FloatToStr(x1)); | |
writeln('x2 = ' + FloatToStr(x2)); | |
end; | |
end; | |
</code> | |
<run id="4f200bfc77a43"> | |
QuadratischeLoesung(2.0, 1.0); | |
</run> | |
<code id="4f201334d3f4a"> | |
procedure ZahlenRaetsel(Maximum: integer); | |
var | |
GeheimZahl, GerateneZahl: integer; | |
AnzahlTipps : integer; | |
begin | |
AnzahlTipps := 1; | |
GerateneZahl := -1; | |
GeheimZahl:= random(Maximum) + 1; | |
while (GerateneZahl <> GeheimZahl) do | |
begin | |
| − | GerateneZahl := StrToInt(InputDialog('Ich habe mir eine Zahl zwischen 1 und '+IntToStr(Maximum)+' ausgedacht. Gib Deinen '+AnzahlTipps+'. Tipp ein:'); |
| + | GerateneZahl := StrToInt(InputDialog('Ich habe mir eine Zahl zwischen 1 und '+ |
| + | IntToStr(Maximum)+' ausgedacht. Gib Deinen '+AnzahlTipps+'. Tipp ein:'); |
AnzahlTipps := AnzahlTipps + 1; | |
end; | |
end; | |
</code> | |
| + | Anmerkung: Beim InputDialog ganz am Ende fehlt eine ")" für das StrToInt :-) |
| + | |
<run id="4f20134c8971a"> | |
ZahlenRaetsel(10); | |
</run> |