Transcript Lecture 3

Lecture 20
0011 0010 1010 1101 0001 0100 1011
1
2
4
The last function of reason is to recognize that
there are an infinity of things which surpass it.
Blaise Pascal
State machines
0011 0010 1010 1101 0001 0100 1011
• Lets take a look at the programs that we’ve
written so far. We should realize that we
haven’t been to strict about erroneous input.
• Our programs lack fundamental error
handling. What are some standard ways for
dealing with this problems?
1
2
4
The phone conversion program
0011 0010 1010 1101 0001 0100 1011
• Given a phone number in the letter form,
print it out using digits. (This is one of the
problems from last time quiz).
1
2
4
• We need to design a robust program which
can perform this task.
Defining the input format
0011 0010 1010 1101 0001 0100 1011
First thing to do is to enforce user input. There
are many different ways to format phone
number, so just pick one:
(XXX)XXX-XXXX
We have area code in parenthesis then three
digits, dash, then four last digits.
1
2
4
Observe the details to extract
states.
0011 0010 1010 1101 0001 0100 1011
(XXX)XXX-XXXX
Area code - enclosed in ()
First three digits are followed by ‘-’
Finally four last digits.
1
2
4
What are the states and what is a state
machine?
State machine
0011 0010 1010 1101 0001 0100 1011
State machine is a part of the program, usually
used to keep track of where we are in the
certain process. For example, in the phone
number problem, we can be reading the area
code, which is one state, or we can be
reading first three digits of a phone number
which is a different state.
1
2
4
State transition diagram
0011 0010 1010 1101 0001 0100 1011
S1
a
b
S0
If you are in state 0
and you see a transition
to state 1
If you are in state 0
and you see b transition
to state 2
If you are in state 0
and you see c transition
to state 3
S2
c
S3
1
2
4
Parsers (essentially a ‘formatenforcer’)
0011 0010 1010 1101 0001 0100 1011
- Read next input character
- Look in what state you are and what is the
character that you read. Decide if this
character is a valid input at the moment,
figure out what is the next state. If the input
was not valid, issue an error message.
1
2
4
Phone number parser
0011 0010 1010 1101 0001 0100 1011
(XXX)XXX-XXXX
States:
1. Init (didn’t see any input)
2. Area code (reading area code)
3. First three digits (reading first three digits)
4. Last four digits (reading last four digits)
1
2
4
Phone number parser
0011 0010 1010 1101 0001 0100 1011
(XXX)XXX-XXXX
What are the transitions?
When we see ‘(‘
transition from State 1 to State 2
(Init ----> read area code)
When we see ‘)’
transition from State 2 to State 3
( Read area code --> read first 3 digits)
1
2
4
Phone number parser
0011 0010 1010 1101 0001 0100 1011
(XXX)XXX-XXXX
What are the transitions?
When we see ‘-‘
transition from State 3 to State 4
( Read first 3 digits --> read last four digits)
The characters ‘(‘, ‘)’ and ‘-’ are signal
characters that help us identify transitions.
1
2
4
How do we code the state
machine?
0011 0010 1010 1101 0001 0100 1011
State machines are usually encoded using case
statement, in pseudo code:
case iState of
State1 : do something
State2 : do something
State3 : do something
etc.
1
2
4
Why use state machines?
0011 0010 1010 1101 0001 0100 1011
• They provide logical, methodical
approaches to sequential input processing. It
is easier to design state machine, then have
a a lot of if-then-else statements. Lets try to
design the state machine for the phone
number problem.
1
2
4
Phone number example state machine
(XXX)XXX-XXXX
Input is digit
and count <= 3
0011 0010 1010 1101 0001 0100 1011
Input is ‘(‘
Area
code
Input is ’)’
and count = 4
Init
First three
letters
Last four
Letters
Input is digit
Input is ’-’
and count = 4
1
2
Input is digit
and count <= 3
4
In every state, if the input is not
correct issue the error and halt.
Optional (hard) problem for next
time
0011 0010 1010 1101 0001 0100 1011
• Write a program that converts letter phone
numbers into digit phone numbers using the
state machine that we discussed.
1
2
4
While loops
0011 0010 1010 1101 0001 0100 1011
• We have already encountered construct in
Pascal which are not necessary but
convenient. For example case statement.
• Pascal provides two more for-loop-like
constructs of which we will learn one.
1
2
4
Format
0011 0010 1010 1101 0001 0100 1011
While ( <BOOLEAN EXPRESSION> ) DO
BEGIN
…
END; { of while loop }
1
2
4
While loop
0011 0010 1010 1101 0001 0100 1011
This simply allows us to perform set of
operations while certain condition is true.
Why is this useful? Lets see an example.
1
2
4
While loop is actually more
powerful than for-loop
0011 0010 1010 1101 0001 0100 1011
• Can we simulate while-loop with for-loop?
• Can we simulate for-loop with while loop?
1
2
4
While loop is actually more
powerful than for-loop
0011 0010 1010 1101 0001 0100 1011
• We can’t simulate while loop with for-loop
because we will not know when we should
terminate.
• On the other hand, we can simulate for-loop
with while loop like this:
1
2
4
Simulating the for-loop with
while loop
0011 0010 1010 1101 0001 0100 1011
For I := 1 to 10 do
…
I := 1;
while ( I != 10 ) do begin
…
I := I + 1;
end;
1
2
4
Home work
0011 0010 1010 1101 0001 0100 1011
• Program 6 is due Monday, November 30
• Read chapter 8, sections 1,2 ignore
whatever you don’t understand.
• Attempt the optional problem (spend at
least 1/2 hour thinking about it)
1
2
4
Program from this lecture
0011 0010 1010 1101 0001 0100 1011
• Count words
1
2
4