Pascal provides several standard libraries (also known as units) that offer a wide range of functionality, from basic input/output to mathematical computations and string manipulation. Understanding and utilizing these libraries can significantly enhance your productivity and the capabilities of your programs. This chapter will cover some of the most commonly used Pascal standard libraries: crt
, SysUtils
, and Math
.
The crt
unit provides functions and procedures for handling console input and output, as well as managing the screen.
To use the crt
unit, include it in the uses
clause of your program.
uses crt;
begin
ClrScr;
writeln('Screen cleared!');
end.
begin
GotoXY(10, 5);
writeln('Cursor moved to position (10, 5)');
end.
begin
TextColor(Red);
writeln('This text is red.');
TextColor(White);
end.
begin
writeln('Press any key to continue...');
ReadKey;
end.
The SysUtils
unit provides a variety of system-related utilities, including string manipulation, file handling, date and time operations, and error handling.
To use the SysUtils
unit, include it in the uses
clause of your program.
uses SysUtils;
var
str: string;
num: integer;
begin
str := '123';
num := StrToInt(str);
writeln('Converted string to integer: ', num);
end.
var
num: integer;
str: string;
begin
num := 456;
str := IntToStr(num);
writeln('Converted integer to string: ', str);
end.
var
fileName: string;
begin
fileName := 'test.txt';
if FileExists(fileName) then
writeln('File exists.')
else
writeln('File does not exist.');
end.
var
fileName: string;
begin
fileName := 'test.txt';
if DeleteFile(fileName) then
writeln('File deleted.')
else
writeln('Failed to delete file.');
end.
var
now: TDateTime;
begin
now := Now;
writeln('Current date and time: ', DateTimeToStr(now));
end.
var
now: TDateTime;
begin
now := Now;
writeln('Formatted date: ', FormatDateTime('yyyy-mm-dd', now));
writeln('Formatted time: ', FormatDateTime('hh:nn:ss', now));
end.
The Math
unit provides mathematical functions and constants.
To use the Math
unit, include it in the uses
clause of your program.
uses Math;
var
x, result: real;
begin
x := 16.0;
result := Sqrt(x);
writeln('Square root of ', x:0:2, ' is ', result:0:2);
end.
var
base, exponent, result: real;
begin
base := 2.0;
exponent := 3.0;
result := Power(base, exponent);
writeln(base:0:2, ' raised to the power of ', exponent:0:2, ' is ', result:0:2);
end.
begin
writeln('Value of Pi: ', Pi:0:10);
end.