File

advertisement
Tutorial 9 String and Structure
NUS SCHOOL OF COMPUTING
CS1010E PROGRAMMING METHODOLOGY
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
1
Quick Summary
1. Structure introduces the concept of an Object. The Object can
contain many attributes. (Person: Gender, Age, etc.)
1. Two ways to declare a structure:
typedef struct{…}[identifier];
struct [identifier]{…};
2. To retrieve the attributes of a structure use “.” operator:
Person.Gender = “male”;
Person.Age = 20;
3. For Structures with pointers:
Person->Gender = “male”;
Person->Age = 20;
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
2
Q1: Self Implementation of <string.h>
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
3
Q1: Self Implementation of <string.h>
R
A
D
A
Left
R
\0
Right
strlen = right – left;
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
4
Q1: Self Implementation of <string.h>
R
A
D
A
R
\0
s
2
s
1
t
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
5
Q1: Self Implementation of <string.h>
R
A
s
2
s
1
t
R
D
A
R
\0
my_strcpy(s1,s2);
A
\0
R
A
CS1010E TUTORIAL SLIDES
D
A
R
\0
PREPARED BY WU CHAO
6
Q1: Self Implementation of <string.h>
R
A
D
A
R
\0
Loop until (s1 becomes \0) or (a difference is found):
s
2
s
1
R
Return the difference
A
D
A
CS1010E TUTORIAL SLIDES
R
\0
PREPARED BY WU CHAO
7
Q2: Christmas Carol
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
8
Q2: Christmas Carol
On any particular day:
Constant strings:
“On the ”
“day of Christmas,\n”
“my true…\n”
“and ”
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
9
Q2: Christmas Carol
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
10
Q2: Christmas Carol
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
11
Q2: Christmas Carol
Because a word ends with either of the following:
Space, Comma, Fullstop.
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
12
Q2: Christmas Carol
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
13
Q3: Overlapping Rectangles
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
14
Q3: Overlapping Rectangles
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
15
Q3: Overlapping Rectangles
If the points do not fulfil the pre-condition required,
we just need to swap the two points.
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
16
Q3: Overlapping Rectangles
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
17
Q3: Overlapping Rectangles
It is easier to check if the two rectangles do not overlap:
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
18
Q3: Overlapping Rectangles
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
19
Q4: Snapback Problem
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
20
Q4: Snapback Problem
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
21
Q4: Snapback Problem
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
22
Q4: Snapback Problem
Bubble sort algorithm is used.
This is an improved version of the
bubble sort.
What is the difference?
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
23
Q4: Snapback Problem
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
24
Q4: Snapback Problem
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
25
Q4: Snapback Problem
1
0
0
1
1
…
1
1
0
In the binary sequence, 1 means take the item number j, 0 means omit
the item number j.
We use this binary sequence approach to enumerate all the possible
ways to take the items available and compare the value.
CS1010E TUTORIAL SLIDES
PREPARED BY WU CHAO
26
Download