-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx1
210 lines (197 loc) · 5.8 KB
/
Ex1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
LIST
LIST is a set of jointed data elements.
One element has data and the pointer to next element.
Sample program 9 show you how to use LIST structure.
Data : banana
Pointer : p2
Pointer : p1
Data : orange
Pointer : p3
Data : apple
Pointer : NULL
Last pointer is NULL
(1)Creates the LIST from array data
(2)Print the LIST
(3)Delete the LIST.
Sample data are fruit names.
There are 3 fruits in the LIST.
struct leaf {
char data[16]; /* string data */
struct leaf *next; /* pointer to next*/
};
One element of LIST
element 1
element 2
element 3
Top
0 apple
1 orange
2 banana
Array data
LIST structure
Sample program 9 action
struct leaf {
char data[16]; /* string data */
struct leaf *next; /* pointer to the next leaf */
};
struct leaf *addLeaf(char*, struct leaf *);
void printLeaves(struct leaf *p);
void freeLeaves(struct leaf *p);
int main(void)
{
struct leaf *top; /* top of the leaves */
char *fruits[3] = {"apple", "orange", "banana" } ;
top = NULL;
top = addLeaf(fruits[0], top);
top = addLeaf(fruits[1], top);
top = addLeaf(fruits[2], top);
printLeaves(top); /* print elements*/
freeLeaves(top); /* delete all elements */
}
void printLeaves(struct leaf *pL)
{
while (pL != NULL) {
printf("%s\n", pL->data);
pL = pL->next;
}
}
struct leaf *addLeaf(char *data, struct leaf *top)
{
struct leaf *pL;
pL = (struct leaf*) malloc(sizeof(struct leaf));
strcpy(pL->data, data);
/* change pointer to the top */
pL->next = top; /* Old top is new leaf's next */
top = pL;/* New top is new leaf's address */
return top;
}
void freeLeaves(struct leaf *pL)
{
struct leaf *pLnext;
while (pL != NULL) {
pLnext = pL->next;
free(pL);
pL = pLnext;
}
}
List create step (addLeaf)
Pointer : p3
Data : apple
Pointer : NULL
element 3
Top
Pointer : NULL
Top
Pointer : p2
Data : orange
Pointer : p3
Data : apple
Pointer : NULL
element 2
element 3
Top
Data : banana
Pointer : p2
Pointer : p1
Data : orange
Pointer : p3
Data : apple
Pointer : NULL
Regarding the sample program 9, add following function.
(1) After creating 3 initial data (prepared in program 9),
insert element 4 after element 1. (element 4 data is "grape")
[Expand exercise]
(2) Get “insert number” and “fruit name” by "scanf", and insert the element to the LIST.
Add new element to the LIST
LIST structure is more easy to add element than array.
Data : banana
Pointer : p2
Pointer : p1
Data : orange
Pointer : p3
Data : apple
Pointer : NULL
(1)Create element4 data.
(2)Copy element1's next pointer to element4's next pointer.
(3)Set element4 's address to the element1's next pointer.
Exercise 4-2
Regarding the sample program 9, add following function.
(1) After create initial 3 data (prepared in program 9),
remove element 2.
[Expand exercise]
(2) Get “remove number” by "scanf", and remove the element from the LIST
LIST structure is more easy to remove element than array.
Data : banana
Pointer : p2
Pointer : p1
Data : orange
Pointer : p3
Data : apple
Pointer : NULL
How to remove element 2
(1) Copy element2's next pointer to element1's next pointer.
(2) Free memory the data of element 2.
element 1
element 2
element 3
Free
Top
p3
Ex 6
Make your original program and confirm that function pointer can be use in function parameter.(Find any effective case. )
Say in other words,
(1) Make the main routine and any subroutines.
(2) Declare function pointer in the main routine.
(3) Call subroutine1 which has function pointer argument.
(4) Call subroutine2 by function pointer.
Read following explanation and make a program.
There is a mathematical game which named guessNumber:
How to play the game
Computer creates the 4 digits random number (target number), each digit character is different from other digits, and it's hidden from game player.
Player must find target number with some hints.
At first, player must guess the target number and answer to the computer.
If player's answer is not correct, computer gives some hints.
And player must guess the number again from the hints. (repeat)
Finally player will find target number.
E 1
Make following subroutine and debug by HEW simulator.
int isMatch(char target[], char num[]);
This function compares 2 argument and check hit count and blow count,
and replies TRUE or FALSE.
1st argument is target number and 2nd argument is guess number.
Both are 4 byte character string.
Hit count and blow count are displayed by printf.
When target number and guess number is same, function returns TRUE.
When target number and guess number is different, function returns FALSE.
E 2
Make following subroutine and debug by HEW simulator.
int isValidNumber(char num[]);
This function check if "num"(4 digit number string) is valid or not.
If each digit character is different from other digits, return TRUE.
If one of digit character and other digit character is same, return FALSE.
Example:
When "num" is "2661"
The function returns FALSE. (There are two '6' in the 4 digit.)
When "num" is "0523"
The function returns TRUE.
E 3
Make subroutine and debug by HEW simulator.
void createRandomNumber(char num[]);
This function creates the 4 digits random number.
Each digit character is different from other digits.
Created number is returned with parameter.
Notice
In the HEW simulator, The function rand() replies same answer every time.
So, use another way. For example, player input any number and computer
modify the input number and make the 4 digits random number.
Add your original invention.
E 4
All subroutines are prepared in Exercise 1, 2, 3.
Make main routine and debug by HEW simulator.
Test your program and complete the GuessNumber program.
Make a pair with another person, and check your partner's program each other.
(1) Is your partner's program satisfy the specification?
When you find bad point in the program, make your partner correct them.
Don't correct your partner's program but tell him bad point.
(2) Find good point of your partner's program.
(3) Find weak point of your partner's program.