Guest User

Untitled

a guest
Feb 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1.  
  2. // 1回生向け課題のめも
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <errno.h>
  9.  
  10. const int DEFAULT_RECORDS_LIST_SIZE = 16;
  11. const int MAX_BUF_SIZE = 256;
  12.  
  13. typedef struct {
  14. char code[10];
  15. char name[40];
  16. int price;
  17. } Record;
  18.  
  19. typedef struct {
  20. Record **list;
  21. int size;
  22. int _mem_size;
  23. } Records;
  24.  
  25. void usage() {
  26. fprintf(stderr, "usage: kadai1-3 inputfile\n");
  27. }
  28.  
  29. void error(const char *msg) {
  30. if (errno) {
  31. fprintf(stderr, "kadai1-3: %s: %s \n", msg, strerror(errno));
  32. }
  33. else {
  34. fprintf(stderr, "kadai1-3: %s\n", msg);
  35. }
  36. usage();
  37. }
  38.  
  39. Record *create_record(char const *code, char const *name, int price) {
  40. Record *record = NULL;
  41.  
  42. if ( (record = (Record *)(malloc(sizeof(Record)))) == NULL) {
  43. error("Memory allocation error");
  44. return NULL;
  45. }
  46.  
  47. strncpy(record->code, code, strlen(code));
  48. strncpy(record->name, name, strlen(name));
  49. record->price = price;
  50.  
  51. return record;
  52. }
  53.  
  54. Records *create_records() {
  55. Records *records;
  56. records = (Records *)malloc(sizeof(Records));
  57. if (records == NULL) {
  58. error("Memory allocation error");
  59. return NULL;
  60. };
  61.  
  62. records->list = (Record **)calloc(DEFAULT_RECORDS_LIST_SIZE, sizeof(Record *));
  63. if (records->list == NULL) {
  64. free(records);
  65. error("Memory allocation error");
  66. return NULL;
  67. }
  68. records->_mem_size = DEFAULT_RECORDS_LIST_SIZE;
  69. records->size = 0;
  70.  
  71. return records;
  72. }
  73.  
  74. Record *add_record(Records *records, Record *record) {
  75. if (!(records->size < records->_mem_size)) {
  76. // TODO
  77. // realloc
  78. // update _mem_size
  79. }
  80. records->list[records->size] = record;
  81. records->size++;
  82.  
  83. return record;
  84. }
  85.  
  86. Record *search_record(Records *records, char *keyword) {
  87. Record *record;
  88. int i;
  89. for (i = 0; i < records->size; i++) {
  90. record = records->list[i];
  91. if (strncmp(keyword, record->code, MAX_BUF_SIZE) == 0) {
  92. return record;
  93. }
  94. }
  95. return NULL;
  96. }
  97.  
  98. void free_records(Records *records) {
  99. int i;
  100. for (i = 0; i < records->size; i++) {
  101. free(records->list[i]);
  102. }
  103. free(records->list);
  104. free(records);
  105. }
  106.  
  107. Records *load_records(FILE *i_fp) {
  108. int i;
  109. char buf[MAX_BUF_SIZE];
  110. char code[MAX_BUF_SIZE], name[MAX_BUF_SIZE];
  111. int price;
  112. Records *records;
  113. Record *record;
  114.  
  115. records = create_records();
  116. if (records == NULL) {
  117. return NULL;
  118. };
  119.  
  120. while ( fgets(buf, MAX_BUF_SIZE, i_fp) != NULL ) {
  121. i = 0;
  122. sscanf(buf, "%s%*[ ]%s%*[ ]%d", code, name, &price);
  123.  
  124. record = create_record(code, name, price);
  125. if (records == NULL) {
  126. free_records(records);
  127. return NULL;
  128. };
  129.  
  130. record = add_record(records, record);
  131. if (record == NULL) {
  132. free_records(records);
  133. return NULL;
  134. };
  135. }
  136.  
  137. return records;
  138. }
  139.  
  140.  
  141. int main (int argc, char *argv[]) {
  142. char buf[MAX_BUF_SIZE];
  143. char *i_filename;
  144. FILE *i_fp;
  145. Records *records;
  146. Record *record;
  147. errno = 0;
  148.  
  149. if (argc < 2) {
  150. error("Too few arugments");
  151. return EXIT_FAILURE;
  152. }
  153.  
  154. i_filename = argv[1];
  155. i_fp = fopen(i_filename, "r");
  156. if( i_fp == NULL) {
  157. error("File open error");
  158. return EXIT_FAILURE;
  159. }
  160.  
  161. records = load_records(i_fp);
  162. if (records == NULL) {
  163. fclose(i_fp);
  164. return EXIT_FAILURE;
  165. }
  166.  
  167. printf("keyword: ");
  168. fgets(buf, MAX_BUF_SIZE, stdin);
  169. buf[strlen(buf) - 1] = '\0';
  170.  
  171. record = search_record(records, buf);
  172. if (record) {
  173. printf("code:\t%s\n", record->code);
  174. printf("name:\t%s\n", record->name);
  175. printf("price:\t%d\n", record->price);
  176. printf("\n");
  177. }
  178. else {
  179. printf("Not Found.\n");
  180. }
  181.  
  182. free_records(records);
  183. fclose(i_fp);
  184.  
  185. return EXIT_SUCCESS;
  186. }
Add Comment
Please, Sign In to add comment