(yielding 'abcfghij')
Let's give names to the different parts of "text":
FirstPart = 'abc'
Remainder =
'fghij'
So: text = FirstPart + part to be deleted + Remainder
&: updated text = FirstPart + Remainder
FirstPart depends on:
text, ptr
Remainder depends on:
text, ptr, deletelength
a) TDD:
{move pointer to place where
deletion begins}
writeln('DELETE HOW MANY
CHAR?');
readln(deletelength);
text :=
FirstPart(text,ptr)
+ Remainder(text,ptr,deletelength)
writeln(text)
Call this section of code "DELETECODE"
Note: We haven't defined
"Remainder" yet,
But we know what it must depend on;
So now we can do SWR on it:
b) SWR:
Need to figure out how to
describe Remainder
using "substr".
Therefore, need to know 2
things:
* where
it starts
* how
long it is
Remainder starts at:
ptr + deletelength
length(Remainder) =
length(text) - length(FirstPart) - deletelength
Therefore:
Remainder(text,ptr,deletelength) =def
substr(text, ptr+deletelength,
length(text)-length(FirstPart)-deletelength)
i.e.)
function Remainder(word : string;
loc, len : integer) : string;
{I/P = word, location, length}
{O/P = substring of "word" starting from where
deleted chars edn (= loc + len) to end}
begin
Remainder := substr(word, loc+len,
length(word)-length(FirstPart(word,loc)-len)
end;
Trace this on word = 'windows', loc = 4, len = 3
(i.e., on a text = 'windows', deleting 3 chars,
at ptr location = 4; answer should be 'wins')
c) Implementation (final SWR):
program DeleteTest;
type string = {etc.}
var text, command, target : string;
ptr, deletelength
: integer;
function FirstPart {etc.};
function Remainder {etc.};
begin
writeln('INPUT TEXT:');
readln(text);
writeln('COMMAND:');
readln(command);
if command = 'd'
then begin DELETECODE end
else writeln('SORRY {etc.})
end.
d) Click here for complete program.
* other features (change one word to another,
insert a space, etc.) are either special cases
or combinations of "insert" & "delete"
b) Suppose we want to:
* Create new text (= insert into empty "text")
- e.g.)
'How~our~you?'
* Then delete "our" (-> 'How~~you?')
* Then insert "are" (-> 'How~are~you?')
Now we can't really do it:
* need to run 2-3 different programs,
& retype text each time.
c) Want to do it with a single program
i.e.)
When done with one command,
want program to ask us for another.
Solution: while loop
e.g.) program loop;
begin
while true do
begin
writeln('This is an infinite loop');
writeln('To stop it, hit control-c')
end
end.
Click
here for downloadable/runnable version.
d) Skeleton of text-editor program:
{input initial text};
{initialize ptr};
writeln('COMMAND');
readln(command);
if command = 'i'
then begin {code for insert goes here} end;
if command = 'd'
then begin {code for delete goes here} end;
{etc.}
e) Want to be able to have user keep inputting
new commands without stopping
& restarting
program.
Therefore, use a loop:
while true do
begin
writeln('COMMAND');
readln(command);
if command = 'i'
then begin {code for insert goes here} end;
if command = 'd'
then begin {code for delete goes here} end;
{etc.}
end
f) BUT: this is an infinite loop, like
program loop
above!
Therefore, we need a way
to stop it:
*
a way to change the test to false.
i
:= 1; {initializes the test}
while i <= 10 do {an arithmetic test}
begin {while loop}
writeln('hello');
i := i+1
end {while loop}
end.
Click
here for a downloadable/runnable version.