fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. #define HASH_SIZE 4019
  6.  
  7. struct Person {
  8. char first_name[20];
  9. char last_name[20];
  10. char dob[11];
  11. double balance;
  12. };
  13.  
  14. struct HashNode {
  15. struct Person *person;
  16. struct HashNode *next;
  17. };
  18.  
  19. struct HashTable {
  20. struct HashNode *table[HASH_SIZE];
  21. };
  22.  
  23.  
  24. unsigned int hash(char *string) {
  25. unsigned int result = 0;
  26. for (int i = 0; string[i] != '\0'; i++) {
  27. result = 33 * result + string[i];
  28. }
  29. return result % HASH_SIZE;
  30. }
  31. void custom_strcpy(char *dest, const char *src) {
  32. int i = 0;
  33. while (src[i] != '\0') {
  34. dest[i] = src[i];
  35. i++;
  36. }
  37. dest[i] = '\0';
  38. }
  39.  
  40. int custom_strcmp(const char *str1, const char *str2) {
  41. while (*str1 && (*str1 == *str2)) {
  42. str1++;
  43. str2++;
  44. }
  45. return *(unsigned char *)str1 - *(unsigned char *)str2;
  46. }
  47.  
  48.  
  49.  
  50. void insert(struct HashTable *hash_table, struct Person *person, int *counter) {
  51. unsigned int index = hash(person->first_name) + hash(person->last_name) + hash(person->dob);
  52. index %= HASH_SIZE;
  53.  
  54. struct HashNode *new_node = malloc(sizeof(struct HashNode));
  55. new_node->person = person;
  56. new_node->next = NULL;
  57.  
  58. if (hash_table->table[index] == NULL) {
  59. hash_table->table[index] = new_node;
  60. } else {
  61. struct HashNode *current = hash_table->table[index];
  62. while (current->next != NULL) {
  63. if (custom_strcmp(current->person->first_name, person->first_name) == 0 &&
  64. custom_strcmp(current->person->last_name, person->last_name) == 0 &&
  65. custom_strcmp(current->person->dob, person->dob) == 0) {
  66. if(*counter == 0) {
  67. printf("insert failed");
  68. }
  69. else{
  70. printf("\ninsert failed");
  71. }
  72. (*counter)++;
  73. free(new_node->person);
  74. free(new_node);
  75. return;
  76. }
  77. current = current->next;
  78. }
  79. if (custom_strcmp(current->person->first_name, person->first_name) == 0 &&
  80. custom_strcmp(current->person->last_name, person->last_name) == 0 &&
  81. custom_strcmp(current->person->dob, person->dob) == 0) {
  82. if(*counter == 0) {
  83. printf("insert failed");
  84. }
  85. else{
  86. printf("\ninsert failed");
  87. }
  88. (*counter)++;
  89. free(new_node->person);
  90. free(new_node);
  91. return;
  92. }
  93. current->next = new_node;
  94. }
  95. }
  96.  
  97.  
  98.  
  99. struct Person *search(struct HashTable *hash_table, char *first_name, char *last_name, char *dob) {
  100. unsigned int index = hash(first_name) + hash(last_name) + hash(dob);
  101. index %= HASH_SIZE;
  102.  
  103. struct HashNode *current = hash_table->table[index];
  104. while (current != NULL) {
  105. if (custom_strcmp(current->person->first_name, first_name) == 0 &&
  106. custom_strcmp(current->person->last_name, last_name) == 0 &&
  107. custom_strcmp(current->person->dob, dob) == 0) {
  108. return current->person;
  109. }
  110. current = current->next;
  111. }
  112. return NULL;
  113. }
  114.  
  115. void update(struct HashTable *hash_table, char *first_name, char *last_name, char *dob, double new_balance, int *counter) {
  116. struct Person *person = search(hash_table, first_name, last_name, dob);
  117. if (person != NULL) {
  118. if(person->balance + new_balance <0){
  119. if(*counter == 0) {
  120. printf("update failed");
  121. }
  122. else{
  123. printf("\nupdate failed");
  124. }
  125. (*counter)++;
  126. }else{
  127. person->balance += new_balance;
  128. }
  129.  
  130. } else {
  131. if(*counter == 0) {
  132. printf("update failed");
  133. }
  134. else{
  135. printf("\nupdate failed");
  136. }
  137. (*counter)++;
  138. }
  139. }
  140.  
  141. void delete(struct HashTable *hash_table, char *first_name, char *last_name, char *dob, int *counter) {
  142. unsigned int index = hash(first_name) + hash(last_name) + hash(dob);
  143. index %= HASH_SIZE;
  144.  
  145. struct HashNode *current = hash_table->table[index];
  146. struct HashNode *prev = NULL;
  147.  
  148. while (current != NULL) {
  149. if (custom_strcmp(current->person->first_name, first_name) == 0 &&
  150. custom_strcmp(current->person->last_name, last_name) == 0 &&
  151. custom_strcmp(current->person->dob, dob) == 0) {
  152. if (prev == NULL) {
  153. hash_table->table[index] = current->next;
  154. } else {
  155. prev->next = current->next;
  156. }
  157. free(current->person);
  158. free(current);
  159. return;
  160. }
  161. prev = current;
  162. current = current->next;
  163. }
  164. if(*counter == 0) {
  165. printf("delete failed");
  166. }
  167. else{
  168. printf("\ndelete failed");
  169. }
  170. (*counter)++;
  171. }
  172. void print_balance(struct HashTable *hash_table, char *first_name, char *last_name, char *dob, int *counter) {
  173. struct Person *person = search(hash_table, first_name, last_name, dob);
  174. if (person != NULL) {
  175. double balance = person->balance;
  176. int integer_part = (int)balance;
  177. double fractional_part = balance - integer_part;
  178.  
  179. int rounded_fraction = (int)((fractional_part + 0.005) * 100);
  180.  
  181. if(*counter == 0) {
  182. printf("%d,%02d", integer_part, rounded_fraction);
  183. }
  184. else{
  185. printf("\n%d,%02d", integer_part, rounded_fraction);
  186. }
  187. (*counter)++;
  188. } else {
  189. if(*counter == 0) {
  190. printf("search failed");
  191. }
  192. else{
  193. printf("\nsearch failed");
  194. }
  195. (*counter)++;
  196. }
  197. }
  198.  
  199.  
  200.  
  201.  
  202. int main() {
  203. struct HashTable hash_table;
  204. for (int i = 0; i < HASH_SIZE; ++i) {
  205. hash_table.table[i] = NULL;
  206. }
  207.  
  208. char op;
  209. char first_name[20], last_name[20], dob[11];
  210. int whole;
  211. double balance;
  212. int counter = 0;
  213.  
  214. while (scanf(" %c", &op) == 1) {
  215. switch (op) {
  216. case 'i':
  217. scanf("%s %s %s %d,%lf", first_name, last_name, dob, &whole, &balance);
  218. if(whole<0){
  219. balance = whole - balance / 100;
  220. }
  221. else{
  222. balance = whole + balance / 100;
  223. }
  224. if (balance < 0) {
  225. printf("insert failed\n");
  226. break;
  227. }
  228. struct Person *person = malloc(sizeof(struct Person));
  229.  
  230. custom_strcpy(person->first_name, first_name);
  231. custom_strcpy(person->last_name, last_name);
  232. custom_strcpy(person->dob, dob);
  233. person->balance = balance;
  234. insert(&hash_table, person, &counter);
  235. break;
  236. case 's':
  237. scanf("%s %s %s", first_name, last_name, dob);
  238. print_balance(&hash_table, first_name, last_name, dob, &counter);
  239. break;
  240. case 'u':
  241. scanf("%s %s %s %d,%lf", first_name, last_name, dob, &whole, &balance);
  242. if(whole<0){
  243. balance = whole - balance / 100;
  244. }
  245. else{
  246. balance = whole + balance / 100;
  247. }
  248. update(&hash_table, first_name, last_name, dob, balance, &counter);
  249. break;
  250. case 'd':
  251. scanf("%s %s %s", first_name, last_name, dob);
  252. delete(&hash_table, first_name, last_name, dob,&counter);
  253. break;
  254. default:
  255. break;
  256. }
  257. }
  258.  
  259. for (int i = 0; i < HASH_SIZE; ++i) {
  260. struct HashNode *current = hash_table.table[i];
  261. while (current != NULL) {
  262. struct HashNode *temp = current;
  263. current = current->next;
  264. free(temp->person);
  265. free(temp);
  266. }
  267. }
  268.  
  269. return 0;
  270. }
  271.  
  272.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty