|
| 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 | +} |
0 commit comments