http:^^cs.nyu.edu^cs^dept_info^course_home_pages^fall96^g221.2110^pl^l5^l5.html

来自「This data set contains WWW-pages collect」· HTML 代码 · 共 746 行 · 第 1/2 页

HTML
746
字号
  <LI> Constraint checking is at run time.  <LI> Violation raises <code>constraint</code> <em> exception</em>.<BR>      (Program is <b> not</b> illegal.) </H4> </UL></UL><P><P>---Slide 12---<BR><em> Examples</em><P><P><P><H5> <PRE>  subtype NATURAL is INTEGER range 1..INTEGER'LAST;  A: INTEGER;  B: FLOAT;  C: NATURAL;  D: INTEGER range 0..INTEGER'LAST;  A := B;              --illegal  A := INTEGER(B);     --type conversion, legal  A := C;              --legal  A := D - 3;          --legal  A := C + INTEGER(B); --legal  C := D;              --constraint exception  C := A;              --constraint exception</PRE></H5><P><P>---Slide 13---<BR><em> Arrays</em><P><P><P><UL><LI> <b> Arrays in Ada:</b> <UL><H4>  <LI> Fixed Size---Type may be <em> unconstrained</em> at definitionBut bounds must be given at object declaration.  <LI> Elements are all of same <em> subtype</em>  <LI> Permits: Assignment, Equality Testing, Slicing, <!WA7><IMG  ALIGN=BOTTOM ALT="" SRC="http://cs.nyu.edu/cs/dept_info/course_home_pages/fall96/G221.2110/PL/L5/img8.gif"> </H4> </UL><H5> <PRE>subtype NATURAL is INTEGER range 1..INTEGER'LAST;type SHORT_STRING is array(1..10) of CHARACTER;type STRING is     array (NATURAL range &lt;&gt;) of CHARACTER;NAME: STRING(1..10);</PRE></H5></UL><P>---Slide 14---<BR><em> Array Assignment</em><P><P><P><UL><LI> Assigning array <code>B</code> to <code>A</code>: <tt> A := B</tt><H4> <OL><LI> Legal, if type of <code>A</code> is same as type of <code>B</code>.<P> <LI> If <code>A</code> has same number of elements as <code>B</code>, then<code>B</code> is copied into <code>A</code> <em> positionally</em>---Otherwise, <tt>constraint-error exception</tt> is raised. </OL></H4><P><H5> <PRE>declare  A: STRING(1..10); B: STRING(11..20);begin  A := B;end;</PRE></H5><P><LI> <b> Array Attributes</b><BR> <H4><P><!WA8><IMG  ALIGN=BOTTOM ALT="" SRC="http://cs.nyu.edu/cs/dept_info/course_home_pages/fall96/G221.2110/PL/L5/img9.gif"><P></H4></UL><P>---Slide 15---<BR><em> Array Indexing &amp; Slicing</em><P><P><P><UL><LI> Array Indexing:<H5> <PRE> S: STRING(1..10); S(3) := S(2);</PRE></H5><P> <LI> Array Slicing (1D arrays only)<H5> <PRE> S(3..7)              --an array object S(3..7) := S(4..8); S := S(2..10) &amp; S(1) -- &amp; == concatenation opn</PRE></H5><P> <LI> Array Aggregates:<H5> <PRE> type SYM_TAB is array (CHARACTER range &lt;&gt;) of INTEGER; TABLE: SYM_TAB('a'..'c') := (0, 1, 2); TABLE := ('c' =&gt; 2, 'b' =&gt; 1, 'a' =&gt; 0); TABLE := ('c' | 'b' =&gt; 1, others =&gt; 0);</PRE></H5></UL><P>---Slide 16---<BR><em> Records</em><P><P><P><UL><LI> <b> Records in Ada:</b> <UL><H4>  <LI> <em> Heterogeneous</em>: Components need not be of same type.  <LI> Fields are accessed by component names: E.g.,<code>MY_CAR.CAR_MAKE</code>  <LI> <em> Variant Records</em> Tag (discriminant) fields cannot bechanged at run-time.  <LI> Permits: Assignment and Equality Testing. </H4> </UL><H5> <PRE> type CAR_MAKE is (FORD, GM, HONDA); subtype CAR_YEAR is INTEGER range 1900..1999; type CAR is   MAKE: CAR_MAKE;   YEAR: CAR_YEAR; end record; MY_CAR: CAR;</PRE></H5></UL><P>---Slide 17---<BR><em> Records (Contd)</em><P><P><P><UL><H4><LI> Records may be <em> nested</em>...<em> initialized</em> atdeclaration. <LI> A record <code>B</code> may be assigned to record <code>A</code>, providedthey have same type.<H5> <PRE>  A, B: CAR;  A := B;</PRE></H5><P> <LI> Record Aggregates:</H4><H5> <PRE>  YOUR_CAR: CAR :=           YOUR_CAR: CAR :=     (GM, 1981);                (MAKE =&gt; GM,                                 YEAR =&gt; 1981);</PRE></H5></UL><P>---Slide 18---<BR><em> Variant Records</em><P><P><P><UL><H4><LI> Similar to PASCAL variant records: <LI> Except---Type declaration only defines a template; When objectis declared, <em> discriminant value must be supplied</em>.<PRE>     type VEHICLE_TAG is (CAR, TRUCK);     type VEHICLE(TAG: VEHICLE_TAG) is record       YEAR: MODEL_YEAR := 93;       case TAG is         when CAR =&gt; COLORS: COLOR_SCHEME;         when TRUCK =&gt; AXLES: NATURAL;       end case;     end record;     YOUR_TRUCK: VEHICLE(TRUCK);     REFRIGERATOR: VEHICLE;      --Illegal</PRE><P> <LI> There may be more than one discriminant...But they must all beof discrete types....Discriminant can be used as an uninitializedconstraint.<PRE>     type BUFFER(LENGTH: NATURAL) is record       POOL: STRING(1..LENGTH);     end record;</PRE><P></H4></UL><P>---Slide 19---<BR><em> Access Types</em><P><P><P><UL><H4> <LI> Allow manipulation of pointers. <LI> Allow control of object creation.</H4><H5> <PRE>  type STRING_PTR is access STRING;  type STRING_10_PTR is access STRING(1..10);  P, Q: STRING_PTR; P10: STRING_10_PTR;  P10 := new STRING(1..10);  P10 := new STRING(2..11);    --Constraint Error  P10 := new STRING;           --Illegal  P := new STRING(1..3);       --OK  P.all := &quot;BUD&quot;;  Q := new STRING(&quot;MUD&quot;);  P := Q;  P.all := Q.all</PRE></H5><H4> <LI> <code>new</code> creates a new object that can be designated by theaccess type.</H4><P></UL><P>---Slide 20---<BR><em> Recursive Types</em><P><P><P><PRE>  type NODE;  --Incomplete Declaration;  type NODE_PTR is access NODE;  type NODE is    record      DATUM: CHARACTER;      NEXT: NODE_PTR;    end record;</PRE><P>---Slide 21---<BR><em> Generalized Access Types</em><P><P><P><UL><H4> <LI> Inherent Access to declared objects<BR> 	(Not just objects created by allocators)</H4><H5> <PRE>  type INT_PTR is access all INTEGER;  IP: INT_PTR;  I: aliased INTEGER;  IP := I'Access</PRE></H5><H4> <LI> <b> Note:</b> Designated variable must be marked <code>aliased</code>. <LI> <code>Access</code> attribute is only applicable to an object whose	lifetime is at least that of the access type. <LI> Avoids ``dangling reference'' problem.</H4><H5> <PRE>  type CONST_INT_PTR is access constant INTEGER;  CIP: CONST_INT_PTR;  C: aliased constant INTEGER := 1815;  CIP := C'Access</PRE></H5><H4> <LI> Access is restricted to read-only</H4><P></UL><P><P>---Slide 22---<BR><em> Control Structures</em><P><P><P><UL><LI> Assignment Statements<H5> <PRE>     DISCRIM := (B**2 - 4.0*A*C);     TABLE(J) := TABLE(J) + 1;     VECTOR := (1..10 =&gt; 0);</PRE></H5><P> <LI> Conditional Statements<H5> <PRE>   if (A=1) then     ...   end if;                    case A is                                when 1 =&gt; --...;   if (A=1) then                when 2 =&gt; --...;     --...                      when others =&gt; null;   elsif (A=2) then           end case;     --...   else     --...   end if;</PRE></H5></UL><P>---Slide 23---<BR><em> Control Structures: Iteration Clause</em><P><P><P><UL><LI> Iteration Statements---Basic Loop<H5> <PRE>  loop    -- Statements to be repeated  end loop;</PRE></H5><P> <LI> <em> Iteration Clause</em><BR> <H4>	Execution of a basic <code>loop</code> terminates when <OL><LI> <em> The iteration is completed</em> or   <LI> <em> A <tt> loop exit</tt> statement is executed</em> </OL></H4><P><H5> <PRE>SUM := 0;                SUM := 0;for I in 1..10 loop      for I in reverse 1..10 loop SUM := SUM + A(I);        SUM := SUM + A(I);end loop;                end loop;SUM := 0; I := 1;        SUM := 0; I := 1;while I &lt;= 10 loop       loop  SUM := SUM + A(I);       exit when I &gt; 10;  I := I + 1;              SUM := SUM + A(I);end loop;                  I := I + 1;                         end loop;</PRE></H5></UL><P>---Last Slide---<BR><em> A Complete Ada Program</em><P><P><P><H5> <PRE>  with I_O_PACKAGE;  procedure TEMPERATURE_CONVERSION is    use I_O_PACKAGE;    -- Convert temp in Fahrenheit to Celsius    FAHRENHEIT_TEMP; CELSIUS_TEMP: FLOAT;  begin    GET(FAHRENHEIT_TEMP);    CELSIUS_TEMP := (FAHRENHEIT_TEMP - 32.0)*5.0/9.0;    PUT(CELSIUS_TEMP);  end TEMPERATURE_CONVERSION;</PRE></H5><P>[End of Lecture #5]<P></H3><P><BR> <HR><P><ADDRESS><I>Bud Mishra <BR>Thursday October 10 1996</I></ADDRESS></BODY>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?