📄 pasl1001.html
字号:
<HTML>
<head>
<LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 2</TITLE>
</head>
<BODY background="../tile01.jpg">
<H1><CENTER>Let's Extend !</CENTER></H1>
<H3>Hi !</H3>
<P>
We meet again ! Glad to see you ! Now, let's extend our first program with
a little variation. <TT>
Crt</TT> unit does not consist of <TT>Clrscr</TT> only. It has a lot of interesting functions
and procedures. The ones we will discuss this time are :</P>
<OL>
<LI><TT>TextColor</TT> and <TT>TextBackGround</TT></LI>
<LI><TT>TextAttr</TT></LI>
<LI><TT>GotoXY</TT></LI>
<LI><TT>TextMode</TT></LI>
<LI><TT>Sound</TT>, <TT>Delay</TT>, and
<TT>NoSound</TT></LI></OL>
<P>
This chapter is intended to add your interests in programming so the lesson
wouldn't be so tiresome. If you want to skip this chapter it's OK since it
has no important thing to spot. But beware that every jargon and commands I
use in this lesson will be brought to the next ones.</P>
<P>
OK ! Let's give color to our text on screen ! We use <TT>Crt</TT> unit's procedures:
<TT>TextColor</TT> and <TT>TextBackGround</TT>. Both accepts value ranging 0 to 15. Example :
</P><PRE>
<TT>TextColor(14);
TextBackground(1);</TT>
</PRE><P>
It will set the foreground color to yellow and blue background for the next
<TT>Write</TT>
or
<TT>Writeln</TT>. Yep ! Easy ! Let's look at full example :
</P><HR><PRE>uses crt;
begin
TextColor(14);
TextBackGround(1);
Writeln('Hello there !');
end.</PRE><HR><P>
Run it and see what happens.</P><P>
O ho ! That's great ! Yellow over Blue. Try to figure all color values !
A lot of variation could be applied and it's all yours to choose.
There's even more convenient than above : Use <TT>TextAttr</TT> instead. It's a kind
of a 'combo' variable. You just assign a value that is :</P>
<PRE> (background * 16)+foreground</PRE>
<P>
Suppose you want a cyan background (or light blue), the number is 3, and
black foreground, that is 0. So you would write this:</P>
<PRE>
TextAttr:=(3*16)+0;</PRE><P>
<P>
Then, subsequent writes on screen will be in black over cyan. Easy, eh ?
</P><P>
If you clear screen AFTER you assign colors, you will found that the entire
screen will be wiped to the last assigned color. Suppose you have set color
black over cyan, if you clear the screen, the entire screen will be black
over cyan ! It's useful if you want alternative background instead of black
-- It's boring. Yeah ! Easy !
</P><P>
Want to have big characters on screen ? Try adding <TT>TextMode(CO40);</TT>.
Well,
<TT>TextMode</TT> is used for changing text mode. Valid values to pass is <TT>CO80</TT>,
that
is for return back to normal mode, or <TT>LastMode</TT> -- that is back to last mode
visited, and refer help for more information! Example :</P>
<HR><PRE>uses crt;
begin
TextMode(CO40);
Writeln('A Real BIG Characters on screen !');
Readln;
TextMode(CO80);
Writeln('Back to normal');
Readln;
end.</PRE><HR><P>
Before we continue, I would like to introduce the screen behavior of PCs in
text mode. Screen has a resolution. In text mode, screen has either 80 or
40 horizontal resolution (generally) and 25 vertical resolution. It means,
the screen could be 80 cols by 25 lines or 40 cols by 25 lines. The setting
of <TT>TextMode</TT> reflects to this : <TT>CO40</TT> -- sets the screen to 40 characters
wide and 25 lines -- and <TT>CO80</TT> -- sets the screen to 80 characters wide and
25 lines too. 40 characters wide causes the screen to "stretch" horizontally,
so in result, WIDE characters appear on screen.</P>
<P>
EGA and VGA has a special feature that is also supported by <TT>crt</TT> unit : an
enhanced text mode consists of 43 (when EGA) or 50 lines, instead of 25. We
put the value <TT>Font8x8</TT> inside <TT>TextMode</TT> parameter.</P>
<P>
We can also directs our text output at specified location by using <TT>GotoXY</TT>.
It accepts parameters X and Y (both are bytes). Note that X reflects to
column position, and Y reflects to line position. Both X and Y must not
exceed the range of the current text mode.
Run and view this program <TT>GOTOXY.PAS</TT> to see what's going.</P>
<HR><PRE>uses crt;
Begin
TextMode(CO40);
GotoXY(8,12);
Writeln('At 8,12');
Readln;
GotoXY(35,8);
Writeln('At 35,8');
Readln;
GotoXY(40,20);
Writeln('At 40,20');
Readln;
GotoXY(30,30);
Writeln('Guess');
Readln;
TextMode(CO80);
Writeln('Back to normal');
End.</PRE><HR><P>
Now, let' s play with sounds ! It's easy ! Start playing the sound with
<TT>Sound</TT>, <TT>delay</TT> for several times, then <TT>NoSound</TT>. Let's practice !
Cut and paste this program. Run it in BP.</P><HR><PRE>uses crt;
begin
Sound(440); Delay(100); NoSound;
Sound(550); Delay(100); NoSound;
end</PRE><HR><P>
<TT>Sound</TT> accepts frequency number and it's a word. It is the value of frequency
you want to play. <TT>Delay</TT> accepts value of words. It reflects how many
milliseconds (<sup>1</sup>/<sub>100</sub><sup>th</sup> of a second) you want to delay.
<TT>NoSound</TT> tells the PC
to stop playing the note. If you omit <TT>Delay</TT>, you may not hear any voices.
Try it and figure it out !</P>
<P>
Pascal is able to calculate things and it made programming much easier.
OK guys, here is the convention:</P>
<UL>
<LI>Addition (+), Subtraction(-), and Multiplication(*) :
<dl>
<DD>If both numbers are integers, it yields an integer result. Otherwise, even
there's only one is real, the result becomes real.</DD>
<UL>
<LI>integer with integer = integer</LI>
<LI>integer with real = real</LI>
<LI>real with real = real</LI></UL>
</dl>
<LI>Division(/) : Always yields real result.</LI>
<LI>Special Division (<TT>Div</TT>) :
It's quite the same as (/), but it always yields integer result.</LI>
</UL>
Fine, fine. But how can I apply this ?
<CENTER><TABLE WIDTH=60% BORDER=2>
<TR><TH>Real life equation</TH><TH>Becomes</TH></TR><TR>
<TD>y = 5 x 3</TD><TD>y:=5*3; (y is either integer or real)</TD></TR><TR>
<TD>z = 5 + 4 x 3</TD><TD>z:=5+4*3; (z is either integer or real)</TD></TR><TR>
<TD>a = 3.14 x 7 x 7</TD><TD>a:=3.14*7*7; (a is always real)</TD></TR><TR>
<TD>b = 14 x (5 + a)</TD><TD>b:=14*(5+a);<BR>
(b depends on a, if a real, b is always real. If a integer, b may be real or integer).</TD></TR>
</TABLE></CENTER><P>
Yeah, that's quite an example. But you should know it though. How about the
special division :</P>
<TABLE BORDER=1><TR><TD>
a:=22/7;</TD><TD>when a is real, it yields result 3.142856 .... so on
otherwise, error.</TD></TR><TR><TD>
b:=22 div 7;</TD><TD>b is always integer, and it hold 3.
<TT>Div</TT> always round to the
floor, I mean not to the top nor not to the nearest integer.Even 98 <TT>div</TT> 3
will result 32 though normal people consider 32.66666666..... to 33.
</TD></TR>
</TABLE><P>
Pascal could convert the <TT>real</TT> numbers to integers using
<TT>Trunc</TT> and <TT>Round</TT>.
<TT>Trunc</TT> behaves similarly like <TT>Div</TT>, it rounds to the floor always.
<TT>Round</TT> is
more moderate though, it rounds to the nearest integer. Evaluate the
expressions below and see the result.</P>
<HR><PRE>uses crt;
begin
Clrscr;
Writeln(Trunc(12.31));
Writeln(Round(12.31));
Writeln(Trunc(31.49));
Writeln(Round(31.49));
Writeln(Trunc(44.59));
Writeln(Round(44.59));
Readln;
end;</PRE><HR>
<P>
OK ! That's all for now ! Shall we have the <A HREF="pasq1001.html">
quiz</A> ?</P>
<hr><B><H3>Where to go ?</H3></B>
<p>
<A HREF="../news.html">Back to main page</A><BR>
<A HREF="pasles01.html">Back to Pascal Tutorial Lesson 1 contents</A><BR>
<A HREF="pasq1001.html">To the quiz</A><BR>
<A HREF="pasl1000.html">Back to Chapter 1</A>, 'Hello, World !'<BR>
<A HREF="pasl1002.html">To Chapter 3</A> about branching (<TT>IF</TT>)<BR>
<A HREF="../mylink.html">My page of programming link</A><BR>
<a HREF="../faq.html">Contact me</a></P>
<hr><P class="cpy">By : Roby Joehanes, © 1996, 2000</P>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -