-
Notifications
You must be signed in to change notification settings - Fork 4
/
map.c
171 lines (159 loc) · 3.15 KB
/
map.c
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
#include "map.h"
#include "boolean.h"
#include <stdio.h>
Map M;
boolean IsMapEmpty(Map M)
{
return (M.NbMapElmt == 0);
}
void CreateEmptyMap(Map *M)
{
(*M).NbMapElmt = 0;
}
void InsertMap(keytype K, infomaptype V, Map *M)
{
int i=1;
boolean found = false;
while ((!found)&&(i<=(*M).NbMapElmt))
{
if ((*M).T[i].key == K)
found = true;
else
i++;
}
if (!found)
{
(*M).T[(*M).NbMapElmt+1].key = K;
(*M).T[(*M).NbMapElmt+1].value = V;
(*M).NbMapElmt++;
}
else
{
(*M).T[i].key = K;
(*M).T[i].value = V;
}
}
void DeleteMap(keytype K, Map *M)
{
int i=1,j;
boolean found = false;
while ((!found)&&(i<=(*M).NbMapElmt))
{
if ((*M).T[i].key == K)
found = true;
else
i++;
}
if (found)
{
for (j=i; j<=(*M).NbMapElmt; j++)
{
(*M).T[j].key=(*M).T[j+1].key;
(*M).T[j].value=(*M).T[j+1].value;
}
(*M).NbMapElmt--;
}
}
void ReassignMap(keytype K, infomaptype V, Map *M)
{
int i=1;
boolean found = false;
while ((!found)&&(i<=(*M).NbMapElmt))
{
if ((*M).T[i].key == K)
found = true;
else
i++;
}
if (found)
(*M).T[i].value = V;
}
infomaptype ValueOfMap (keytype K, Map M)
{
int i=1;
boolean found = false;
while ((!found)&&(i<=M.NbMapElmt))
{
if (M.T[i].key == K)
{
found = true;
return M.T[i].value;
}
else
i++;
}
return 0;
}
void TulisMap(Map M)
/* I.S Sembarang
F.S Mencetak isi Map ke layar */
{
int i;
if (IsMapEmpty(M))
printf("Map kosong\n");
else
{
for (i=1; i<=M.NbMapElmt; i++)
{
printf("(%c,%d)\n",M.T[i].key,M.T[i].value);
}
}
}
/* MAIN PROGRAM RELATED CODE */
void InitScoreMap()
/* I.S. M Terdefinisi
F.S. M berisi map huruf ke skornya */
{
CreateEmptyMap(&M);
InsertMap('E',1,&M);
InsertMap('A',2,&M);
InsertMap('I',2,&M);
InsertMap('N',2,&M);
InsertMap('O',2,&M);
InsertMap('R',2,&M);
InsertMap('S',2,&M);
InsertMap('T',2,&M);
InsertMap('C',3,&M);
InsertMap('D',3,&M);
InsertMap('L',3,&M);
InsertMap('G',4,&M);
InsertMap('H',4,&M);
InsertMap('M',4,&M);
InsertMap('P',4,&M);
InsertMap('U',4,&M);
InsertMap('B',5,&M);
InsertMap('F',5,&M);
InsertMap('Y',5,&M);
InsertMap('K',6,&M);
InsertMap('V',6,&M);
InsertMap('W',6,&M);
InsertMap('Z',8,&M);
InsertMap('X',9,&M);
InsertMap('J',10,&M);
InsertMap('Q',10,&M);
}
int Score (Kata K)
// Mengeluarkan skor yang dihasilkan dari kata K
{
int nilai = 0;
int i = 1;
while (i <= K.Length)
{
nilai = nilai + ValueOfMap(K.TabKata[i],M);
i++;
}
if (K.Length >= 10)
nilai = nilai + 100;
return nilai;
}
int TotalScore(Set S1)
// Mengeluarkan total skor dari seluruh kata dalam Set S1
{
int i;
int skortot = 0;
for (i=1; i<=SetNbElmt(S1); i++)
{
skortot = skortot + Score(S1.T[i]);
}
return skortot;
}