CMSI 182 - Introduction to Computer Science
Week 3
References
Biermann's class notes:
Examples
page 57 Exercise 1
program G3; { switch first and last characters in s1 }
uses WinCrt;
var
s1 : string;
begin
readln(s1);
s1 := copy(s1, length(s1), 1) + copy(s1, 2, length(s1) - 2) + copy(s1,
1, 1);
writeln(s1);
end.
page 58 Exercise 2
program deleteFirstLast; { delete first and last characters in s1 }
uses WinCrt;
var
s1 : string;
begin
readln(s1);
s1 := copy(s1, 2, length(s1) - 2);
writeln(s1);
end.
page 58 Exercise 3
program firstChars; { get first i chars in s1 }
uses WinCrt;
var
s1, s2 : string;
i : integer;
begin
readln(s1);
readln(i);
s2 := copy(s1, 1, i);
writeln(s2);
end.
page 62 Exercise 2
program delete; { delete characters }
uses WinCrt;
var
text, command : string;
ptr, n : integer;
begin
writeln('Input text:');
readln(text);
writeln('Input the pointer value:');
readln(ptr);
writeln('Command:');
readln(command);
if command = 'd' then
begin
writeln('Delete how many characters?');
readln(n);
text := copy(text, 1, ptr - 1) + copy(text, ptr +
n, length(text) - ptr + 1 - n);
end;
writeln(text);
writeln(ptr);
end.
page 63 Exercise 4
program space; { insert a space }
uses WinCrt;
var
text, command : string;
ptr : integer;
begin
writeln('Input text:');
readln(text);
writeln('Input the pointer value:');
readln(ptr);
writeln('Command:');
readln(command);
if command = 's' then
begin
text := copy(text, 1, ptr - 1) + ' ' + copy(text,
ptr, length(text) - ptr + 1);
end;
writeln(text);
writeln(ptr);
end.
a simple loop
program loop1; { loop1.pas -- a simple loop }
uses WinCrt;
var
i : integer;
begin
i := 0;
while i < 3 do
begin
writeln('i = ', i);
i := i + 1;
end;
end.
another simple loop
program loop2; { loop2.pas -- another simple loop }
uses WinCrt;
var
command : string;
begin
command := '*'; { initialize command }
while length(command) > 0 do
begin
readln(command);
writeln('command is ', command);
end;
end
page 66 Exercise 4
program placeCursor; { place cursor }
uses WinCrt;
var
cursor : string;
i, ptr : integer;
begin
writeln('How many characters?');
readln(ptr);
cursor := '#';
i := 1;
while i < ptr do
begin
cursor := ' ' + cursor;
i := i + 1;
end;
writeln(cursor);
end.
page 71 Exercise 1
program editor; { this is the editor program for chapter 2 }
uses WinCrt;
var
text, { text to
be edited }
new, { text
to be inserted }
target, { string to be searched
for }
command, { the command to be executed
}
cursor : string; { the string with the # indicating
where operations occur }
i,
{ an index / counter }
ptr, { position
in text where operations occur }
n : integer; { number of characters to delete
}
begin
writeln('Enter Editor.'); { opening message to user }
text := '';
{ initialize some variables }
ptr := 1;
command := '*';
while command <> 'q' do
begin
writeln('Command:');
readln(command);
{ get command from user }
if command = 'i' then { insert a string
}
begin
writeln('Insert what?');
readln(new);
text := copy(text, 1, ptr - 1) +
new + copy(text, ptr, length(text) - ptr + 1);
end;
if command = 'p' then { set pointer }
begin
writeln('This is homework exercise.');
writeln('Temporary code: Enter value
for ptr.');
readln(ptr);
end;
if command = 'd' then { delete characters
}
begin
writeln('Delete how many characters?');
readln(n);
text := copy(text, 1, ptr - 1) +
copy(text, ptr + n, length(text) - ptr + 1 - n);
end;
if command = 'c' then { change some of
the text }
begin
writeln('This is homework exercise.');
writeln('Not implemented here.');
end;
if command = 's' then { insert a space
}
begin
text := copy(text, 1, ptr - 1) +
' ' + copy(text, ptr, length(text) - ptr + 1);
end;
writeln(text);
{ write the edited text }
{ place cursor }
cursor := '#';
i := 1;
while i < ptr do
begin
cursor := ' ' + cursor;
i := i + 1;
end;
writeln(cursor);
end;
{ end of loop; get another command }
writeln('Exit Editor.'); { closing message }
end.
{ end of program }
Homework
Note well: Be sure to save your files in your folder within
the cmsi182 folder.
p. 58 Exercise 4
p. 58 Exercise 5
p. 61 Exercise 1
p. 62 Exercise 3
Ken Geddes Home Page |
CMSI
182 Index | Week 2 | Week
4
Last Modified September 11, 1999
kengeddes@earthlink.net