Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cde623b

Browse files
committed
Exercise 27.12
1 parent e3c45f9 commit cde623b

File tree

3 files changed

+315
-0
lines changed

3 files changed

+315
-0
lines changed

‎Chapter_27/C27_Exercise_27.12.cpp

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
/* Exercise 27.12 */
2+
3+
#include<stdio.h>
4+
#include<stdbool.h>
5+
#include<stdlib.h>
6+
#include<string.h>
7+
#include<assert.h>
8+
#include"C27_Exercise_27.12.h"
9+
10+
11+
int main()
12+
{
13+
enum Action {
14+
EXIT = -1, PRINTACTIONLIST,
15+
CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, CASE9, CASE10
16+
};
17+
const char* actionList = "\tList of actions:\n"
18+
" (1) Find(), (2) Insert(), (3) Remove(), (4) PrintTable()\n"
19+
" (-1) Exit, (0) Print the list of actions\n\n";
20+
printf("%s", actionList);
21+
Table tab;
22+
Initialize(&tab, 5);
23+
size_t bufSize = BUFFERSIZE;
24+
char buf[BUFFERSIZE];
25+
int action;
26+
bool cond = true;
27+
while (cond) {
28+
printf("\nPlease enter the action: ");
29+
int ret = scanf("%d", &action);
30+
char ch;
31+
while ((ch = getchar()) != '\n');
32+
if (ret <= 0) {
33+
printf("\n\n\t\tError. Incorrect input\n");
34+
continue;
35+
}
36+
switch (action) {
37+
case CASE1: {
38+
printf("\tEnter a string without a space to search: ");
39+
if (scanf("%199s", buf) <= 0) {
40+
PrintError("Read error");
41+
break;
42+
}
43+
Record* result = Find(&tab, buf);
44+
if (result == NULL) {
45+
printf("\t\tString not found\n");
46+
break;
47+
}
48+
else {
49+
printf("\t\tString \'%s\', value \'%d\'\n", result->str, result->val);
50+
}
51+
break;
52+
}
53+
case CASE2: {
54+
printf("\tEnter a string without a space (max length is %d): ", BUFFERSIZE - 1);
55+
if (scanf("%200s", buf) <= 0) {
56+
PrintError("Read error");
57+
break;
58+
}
59+
int value;
60+
printf("\tEnter value: ");
61+
if (scanf("%d", &value) <= 0) {
62+
PrintError("Read error");
63+
break;
64+
}
65+
if (Insert(&tab, buf, value) == false) {
66+
PrintError("Insert() error");
67+
}
68+
break;
69+
}
70+
case CASE3: {
71+
printf("\tEnter a string without a space: ");
72+
if (scanf("%199s", buf) <= 0) {
73+
PrintError("Read error");
74+
break;
75+
}
76+
if (Remove(&tab, buf) == false) {
77+
PrintError("Remove() error");
78+
}
79+
break;
80+
}
81+
case CASE4:
82+
PrintTable(&tab);
83+
break;
84+
case PRINTACTIONLIST:
85+
printf("%s", actionList);
86+
break;
87+
case EXIT:
88+
cond = false;
89+
break;
90+
default:
91+
printf("\t\tError. Incorrect action number\n");
92+
break;
93+
}
94+
}
95+
return SUCCESS;
96+
}
97+
98+
void PrintError(const char* message)
99+
{
100+
fprintf(stderr, "\t\tError -> ");
101+
fprintf(stderr, message);
102+
fprintf(stderr, "\n");
103+
}
104+
105+
Record* Find(Table* table, const char* string)
106+
{
107+
if (table == NULL) {
108+
PrintError("Find(): arg1 == NULL");
109+
return NULL;
110+
}
111+
if (string == NULL) {
112+
PrintError("Find(): arg2 == NULL");
113+
return NULL;
114+
}
115+
for (int i = 0; i < table->maxsz; ++i) {
116+
if (table->rec[i] != NULL && strcmp(table->rec[i]->str, string) == 0) {
117+
return table->rec[i];
118+
}
119+
}
120+
return NULL;
121+
}
122+
123+
bool Insert(Table* table, const char* string, int value)
124+
{
125+
if (table == NULL) {
126+
PrintError("Insert(): arg1 == NULL");
127+
return false;
128+
}
129+
if (string == NULL) {
130+
PrintError("Insert(): arg2 == NULL");
131+
return false;
132+
}
133+
if (table->sz == table->maxsz) {
134+
if (Extend(table, table->maxsz * 2) == false)
135+
return false;
136+
}
137+
for (int i = 0; i < table->maxsz; ++i) {
138+
if (table->rec[i] == NULL) {
139+
table->rec[i] = CreateRecord(string, value);
140+
if (table->rec[i] == NULL) return false;
141+
++table->sz;
142+
return true;
143+
}
144+
}
145+
return false;
146+
}
147+
148+
bool Remove(Table* table, const char* string)
149+
{
150+
if (table == NULL) {
151+
PrintError("Remove(): arg1 == NULL");
152+
return false;
153+
}
154+
if (string == NULL) {
155+
PrintError("Remove(): arg2 == NULL");
156+
return false;
157+
}
158+
Record* record = Find(table, string);
159+
if (record == NULL) {
160+
PrintError("Remove(): not found");
161+
return false;
162+
}
163+
Record** ptr = NULL;
164+
for (int i = 0; i < table->maxsz; ++i) {
165+
if (record == table->rec[i]) {
166+
ptr = &table->rec[i];
167+
break;
168+
}
169+
}
170+
RemoveRecord(ptr);
171+
--table->sz;
172+
return true;
173+
}
174+
175+
Record* CreateRecord(const char* string, int value)
176+
{
177+
Record* record = (Record*)malloc(sizeof(Record));
178+
if (record == NULL) {
179+
PrintError("CreateRecord(): no memory allocated");
180+
return NULL;
181+
}
182+
record->str = (char*)malloc(sizeof(char) * strlen(string) + 1);
183+
if (record->str == NULL) {
184+
PrintError("CreateRecord(): no memory allocated");
185+
return NULL;
186+
}
187+
strcpy(record->str, string);
188+
record->val = value;
189+
return record;
190+
}
191+
192+
void RemoveRecord(Record** record)
193+
{
194+
free((*record)->str);
195+
free(*record);
196+
*record = NULL;
197+
}
198+
199+
bool Initialize(Table* table, size_t size)
200+
{
201+
if (table == NULL) {
202+
PrintError("Initialize(): arg1 == NULL");
203+
return false;
204+
}
205+
if (size == 0) {
206+
PrintError("Initialize(): arg2 == 0");
207+
return false;
208+
}
209+
table->rec = (Record**)malloc(sizeof(Record*) * size);
210+
if (table->rec == NULL) {
211+
PrintError("Initialize(): no memory allocated");
212+
return false;
213+
}
214+
table->sz = 0;
215+
table->maxsz = size;
216+
for (int i = table->sz; i < table->maxsz; ++i) {
217+
table->rec[i] = NULL;
218+
}
219+
return true;
220+
}
221+
222+
bool Extend(Table* table, size_t newSize)
223+
{
224+
if (table == NULL) {
225+
PrintError("Initialize(): arg1 == NULL");
226+
return false;
227+
}
228+
if (newSize <= table->maxsz) {
229+
PrintError("Initialize(): new size <= old size");
230+
return false;
231+
}
232+
Record** newBlock = (Record**)realloc(table->rec, sizeof(Record*) * newSize);
233+
if (newBlock == NULL) {
234+
PrintError("Initialize(): no memory reallocated");
235+
return false;
236+
}
237+
table->rec = newBlock;
238+
table->maxsz = newSize;
239+
for (int i = table->sz; i < table->maxsz; ++i) {
240+
table->rec[i] = NULL;
241+
}
242+
return true;
243+
}
244+
245+
bool Destroy(Table* table)
246+
{
247+
if (table == NULL) {
248+
PrintError("Destroy(): arg1 == NULL");
249+
return false;
250+
}
251+
for (int i = 0; i < table->maxsz; ++i) {
252+
if (table->rec[i] != NULL) {
253+
RemoveRecord(&table->rec[i]);
254+
}
255+
}
256+
return true;
257+
}
258+
259+
void PrintTable(Table* table)
260+
{
261+
if (table == NULL) {
262+
return PrintError("PrintTable(): arg1 == NULL");
263+
}
264+
for (int i = 0; i < table->maxsz; ++i) {
265+
if (table->rec[i] != NULL) {
266+
printf("\t\t#%d: \"%s\", \'%d\'\n", i + 1, table->rec[i]->str, table->rec[i]->val);
267+
}
268+
}
269+
}

‎Chapter_27/C27_Exercise_27.12.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Exercise 27.12 */
2+
3+
#ifndef C27_Exercise_27_12_H
4+
#define C27_Exercise_27_12_H
5+
6+
#define BUFFERSIZE 201
7+
8+
enum ReturnTypeCommon { FAILURE = -1, SUCCESS };
9+
10+
const char* sp_2 = " ";
11+
const char* sp_4 = " ";
12+
const char* sp_6 = " ";
13+
const char* sp_8 = " ";
14+
const char* vsp_2 = "\n\n";
15+
const char* vsp_3 = "\n\n\n";
16+
const char* vsp_4 = "\n\n\n\n";
17+
18+
typedef struct SRecord {
19+
int val;
20+
char* str;
21+
} Record;
22+
23+
typedef struct STable {
24+
Record** rec;
25+
int sz;
26+
int maxsz;
27+
} Table;
28+
29+
void PrintError(const char* message);
30+
Record* Find(Table* table, const char* string);
31+
bool Insert(Table* table, const char* string, int value);
32+
bool Remove(Table* table, const char* string);
33+
Record* CreateRecord(const char* string, int value);
34+
void RemoveRecord(Record** record);
35+
bool Initialize(Table* table, size_t size);
36+
bool Extend(Table* table, size_t newSize);
37+
bool Destroy(Table* table);
38+
void PrintTable(Table* tab);
39+
40+
#endif

‎Chapter_27/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ set (FILE_NAME6 C27_Exercise_27.8)
2727
set (SOURCE_CXX_LIST6 ${FILE_NAME6}.cpp)
2828
set (FILE_NAME7 C27_Exercise_27.9)
2929
set (SOURCE_CXX_LIST7 ${FILE_NAME7}.cpp)
30+
set (FILE_NAME8 C27_Exercise_27.12)
31+
set (SOURCE_CXX_LIST8 ${FILE_NAME8}.cpp)
3032

3133

3234
# Add source to this project's executable.
@@ -37,6 +39,7 @@ add_executable (${FILE_NAME4} ${SOURCE_CXX_LIST4})
3739
add_executable (${FILE_NAME5} ${SOURCE_CXX_LIST5})
3840
add_executable (${FILE_NAME6} ${SOURCE_CXX_LIST6})
3941
add_executable (${FILE_NAME7} ${SOURCE_CXX_LIST7})
42+
add_executable (${FILE_NAME8} ${SOURCE_CXX_LIST8})
4043

4144
if (${COMPILE_AS_C})
4245
target_compile_options(${FILE_NAME} PRIVATE "/TC")
@@ -45,6 +48,7 @@ target_compile_options(${FILE_NAME3} PRIVATE "/TC")
4548
target_compile_options(${FILE_NAME5} PRIVATE "/TC")
4649
target_compile_options(${FILE_NAME6} PRIVATE "/TC")
4750
target_compile_options(${FILE_NAME7} PRIVATE "/TC")
51+
target_compile_options(${FILE_NAME8} PRIVATE "/TC")
4852
endif()
4953

5054
# TODO: Add tests and install targets if needed.
@@ -62,3 +66,5 @@ install (TARGETS ${FILE_NAME6} CONFIGURATIONS Debug DESTINATION Build/Debug)
6266
install (TARGETS ${FILE_NAME6} CONFIGURATIONS Release DESTINATION Build)
6367
install (TARGETS ${FILE_NAME7} CONFIGURATIONS Debug DESTINATION Build/Debug)
6468
install (TARGETS ${FILE_NAME7} CONFIGURATIONS Release DESTINATION Build)
69+
install (TARGETS ${FILE_NAME8} CONFIGURATIONS Debug DESTINATION Build/Debug)
70+
install (TARGETS ${FILE_NAME8} CONFIGURATIONS Release DESTINATION Build)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /