Advertisement
cd62131

r1a.c

Aug 10th, 2014
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "r1.h"
  5. static void s_view(T_TLIST *p_tlist) {
  6.   int i;
  7.   T_TINFO *p;
  8.   for (i = 0; i < p_tlist->num_tinfo; i++) {
  9.     p = &p_tlist->tinfo[i];
  10.     printf("[%5d]%15s(%15s),%2d\n", p->no, p->name, p->affiliation, p->student);
  11.   }
  12. }
  13. static void s_update(T_TLIST *p_tlist) {
  14.   char update[SIZE_STR];
  15.   int no;
  16.   T_TINFO *p;
  17.   printf("Input No. : ");
  18.   key_in_s(update, SIZE_STR);
  19.   no = atoi(update);
  20.   if (no < 0 || p_tlist->num_tinfo <= no) {
  21.     printf("%d is not found\n", no);
  22.     return;
  23.   }
  24.   p = &p_tlist->tinfo[no];
  25.   printf("Input Name [%s]: ", p->name);
  26.   key_in_s(p->name, SIZE_STR);
  27.   printf("Input Affiliation [%s]: ", p->affiliation);
  28.   key_in_s(p->affiliation, SIZE_STR);
  29.   printf("Input Student(1)/other[%d]: ", p->student);
  30.   p->student = key_in_c() - '0';
  31. }
  32. static void s_append(T_TLIST *p_tlist) {
  33.   T_TINFO *p;
  34.   p = &p_tlist->tinfo[p_tlist->num_tinfo];
  35.   p->no = p_tlist->num_tinfo++;
  36.   printf("Input Name: ");
  37.   key_in_s(p->name, SIZE_STR);
  38.   printf("Input Affiliation: ");
  39.   key_in_s(p->affiliation, SIZE_STR);
  40.   printf("Input Student(1)/other: ");
  41.   p->student = key_in_c() - '0';
  42. }
  43. static void s_delete(T_TLIST *p_tlist) {
  44.   char delete[SIZE_STR];
  45.   int i, n;
  46.   T_TINFO *p, *q;
  47.   printf("Input No.: ");
  48.   key_in_s(delete, SIZE_STR);
  49.   n = atoi(delete);
  50.   if (n < 0 || p_tlist->num_tinfo <= n) return;
  51.   p_tlist->num_tinfo--;
  52.   for (i = n; i < NUM_TINFO - 1; i++) {
  53.     p = &p_tlist->tinfo[i];
  54.     q = &p_tlist->tinfo[i + 1];
  55.     memcpy(p, q, sizeof(T_TINFO));
  56.     p->no--;
  57.   }
  58. }
  59. int main(void) {
  60.   char c;
  61.   T_TLIST tlist;
  62.   memset(&tlist, '\0', sizeof(tlist));
  63.   while (1) {
  64.     printf("\n(L)oad, (V)iew, (A)ppend, (D)elete, (U)pdate, (S)ave, (Q)uit: ");
  65.     c = key_in_c();
  66.     if (c == 'L' || c == 'l') {
  67.       s_load(&tlist);
  68.     } else if (c == 'V' || c == 'v') {
  69.       s_view(&tlist);
  70.     } else if (c == 'A' || c == 'a') {
  71.       s_append(&tlist);
  72.     } else if (c == 'D' || c == 'd') {
  73.       s_delete(&tlist);
  74.     } else if (c == 'U' || c == 'u') {
  75.       s_update(&tlist);
  76.     } else if (c == 'S' || c == 's') {
  77.       s_save(&tlist);
  78.     } else if (c == 'Q' || c == 'q') {
  79.       break;
  80.     } else {
  81.       fprintf(stderr, "Unsupported Character: %c\n", c);
  82.     }
  83.   }
  84.   return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement