Musterlösung zur Kurvendiskussion für quadratische Funktionen f(x) =ax² + bx+c

MODULE kdeinfach;

IMPORT Display, ColorPlane, Float;

VAR a,b,c: REAL;

PROCEDURE Einlesen* (VAR a,b,c:REAL);
VAR ch: CHAR;
BEGIN
      Display. WriteStr ("Programm zur Kurvendiskussion quadratischer Funktionen ");
      Display. WriteLn;
      REPEAT
            Display. WriteStr ("Bitte a angeben: ");
            Display. ReadReal (a, 5, ch);
            Display. WriteLn;
      UNTIL a#0;
      Display. WriteStr ("Bitte b angeben: ");
      Display. ReadReal (b, 5, ch);
      Display. WriteLn;
      Display. WriteStr ("Bitte c angeben: ");
      Display. ReadReal (c, 5, ch);
      Display. WriteLn;
END Einlesen;

PROCEDURE Nullstellen* (a,b,c:REAL);
VAR d, e: REAL;
BEGIN
      Display. SetForeColor (255,0,0);
      ColorPlane. SetForeColor (255,0,0);
      IF b*b-4*a*c<0 
            THEN Display. WriteStr ("Keine Nullstellen! ");
             ELSE IF b*b-4*a*c=0 
                              THEN Display. WriteStr ("Einzige Nullstelle: ");
                                          d:=-b/(2*a);
                                          Display. WriteReal (d, 5);
                                          ColorPlane. Dot (SHORT(ENTIER(d)),0,1);
                              ELSE Display. WriteStr ("Nullstelle 1: ");
                                          d:=(-b+Float. Sqrt (b*b-4*a*c))/(2*a);
                                          Display. WriteReal (d,5);
                                          Display. WriteLn;
                                          Display. WriteStr ("Nullstelle 2: ");
                                          e:=(-b-Float. Sqrt (b*b-4*a*c))/(2*a);
                                          Display. WriteReal (e,5);
                                          ColorPlane. Line (SHORT(ENTIER(50+(d+5)*30)),37,SHORT(ENTIER(50+(d+5)*30)),43,1);
                                          ColorPlane. Line (SHORT(ENTIER(50+(e+5)*30)),37,SHORT(ENTIER(50+(e+5)*30)),43,1);
                         END;
      END;
      Display. WriteLn;
END Nullstellen;

PROCEDURE Scheitelpunkt* (a,b,c:REAL);
VAR xs, ys: REAL;
BEGIN

      Display. SetForeColor (0,255,0);
      ColorPlane. SetForeColor (0,255,0);
      xs:=-b/(2*a);
      ys:=c-b*b/(4*a);
      Display. WriteStr ("Die Koordinaten des Scheitelpunktes sind: xs = ");
      Display. WriteReal (xs, 5);
      Display. WriteStr (" und ys = ");
      Display. WriteReal (ys, 5);
      Display. WriteLn;
      ColorPlane. Line (SHORT(ENTIER(50+(xs+5)*30)),SHORT(ENTIER(40+30*ys))-3,SHORT(ENTIER(50+(xs+5)*30)),SHORT(ENTIER(40+30*ys))+3,1);
END Scheitelpunkt;

PROCEDURE Schnittpunkty* (c: REAL);
VAR yabschnitt:REAL;
BEGIN
      Display. SetForeColor (100,100,255);
      ColorPlane. SetForeColor (100,100,255);
      yabschnitt:=c;
      Display. WriteStr ("Der Schnittpunkt mit der y-Achse liegt bei ");
      Display. WriteReal (yabschnitt, 5);
      Display. WriteStr (".");
      Display. WriteLn;
      ColorPlane. Line (197,SHORT(ENTIER(40+30*yabschnitt)),203,SHORT(ENTIER(40+30*yabschnitt)),1);
END Schnittpunkty;

PROCEDURE ZeichneAchsen;
VAR i:INTEGER;
BEGIN
      ColorPlane. Line (50,40, 500,40,1); (*Achsen*)
      ColorPlane. Line ( 200,40,200 ,250,1);
      ColorPlane. Line ( 200,250,195 ,245,1); (*Achsenpfeile*)
      ColorPlane. Line ( 200,250,205 ,245,1);
      ColorPlane. Line ( 500,40,495 ,45,1);
      ColorPlane. Line ( 500,40,495 ,35,1);
      ColorPlane. GotoXY (185, 255); (*Achsenbeschriftung*)
      ColorPlane. WriteStr ("y");
      ColorPlane. GotoXY (504, 45); 
      ColorPlane. WriteStr ("x");
      FOR i:=0 TO 6 DO (*Einteilung y-Achse*)
            ColorPlane. Line (197,40+i*30,200,40+i*30,1);
            ColorPlane. GotoXY (185,45+i*30);
            CASE i OF
                  0: ColorPlane. WriteStr ("0");
                  |1: ColorPlane. WriteStr ("1");
                  |2: ColorPlane. WriteStr ("2"); 
                  |3: ColorPlane. WriteStr ("3");
                  |4: ColorPlane. WriteStr ("4");
                  |5: ColorPlane. WriteStr ("5");
                  |6: ColorPlane. WriteStr ("6"); 
            END;
      END;
      FOR i:=0 TO 14 DO (*Einteilung y-Achse*)
            ColorPlane. Line (50+i*30,37,50+i*30,40,1);
            ColorPlane. GotoXY (45+i*30,30);
            CASE i OF
                  0: ColorPlane. WriteStr ("-5");
                  |1: ColorPlane. WriteStr ("-4");
                  |2: ColorPlane. WriteStr ("-3"); 
                  |3: ColorPlane. WriteStr ("-2");
                  |4: ColorPlane. WriteStr ("-1");
                  |5: ColorPlane. WriteStr ("0");
                  |6: ColorPlane. WriteStr ("1"); 
                  |7: ColorPlane. WriteStr ("2");
                  |8: ColorPlane. WriteStr ("3"); 
                  |9: ColorPlane. WriteStr ("4");
                  |10: ColorPlane. WriteStr ("5");
                  |11: ColorPlane. WriteStr ("6");
                  |12: ColorPlane. WriteStr ("7");
                  |13: ColorPlane. WriteStr ("8");
                  |14: ColorPlane. WriteStr ("9"); 

            END;
      END;
END ZeichneAchsen;

PROCEDURE ZeichnePunkte* (a,b,c:REAL);
VAR xakt,yakt: LONGREAL;
      xdot,ydot,i:INTEGER;
      ydotl:LONGINT;

BEGIN
      FOR i:=0 TO 420 DO
            xakt:=-5+i/30;
            yakt:=a*xakt*xakt+b*xakt+c;
            xdot:=50+i;
            ydot:=SHORT(ENTIER(40+30*yakt));
            ColorPlane. Dot (xdot,ydot,1); 
      END;
END ZeichnePunkte;


PROCEDURE Zeichnen*(a,b,c:REAL);
BEGIN
      ColorPlane. Open ();
      ZeichneAchsen;
      ZeichnePunkte(a,b,c);

END Zeichnen;

PROCEDURE ProgMain*;
VAR a,b,c: REAL;
      ch:CHAR;
BEGIN
      REPEAT 

            Einlesen (a,b,c);
            Zeichnen(a,b,c);
            Nullstellen (a,b,c);
            Scheitelpunkt (a,b,c); 
            Schnittpunkty (c);
            Display. WriteLn;
            Display. SetForeColor (0,0,0);
            REPEAT Display. WriteStr ("Weitermachen (w) oder beenden (e)? "); Display. ReadChar (ch);Display. WriteLn;
                  UNTIL ((ch='e') OR (ch='E')) OR ((ch='W') OR (ch='w'));
            ColorPlane.Close(); Display. ClrScr;
      UNTIL (ch='e') OR (ch='E')
END ProgMain;

END kdeinfach.

Zurück zur Kurvendiskussion quadratischer Funktionen