fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. struct Pegawai
  7. {
  8. char Nama[21];
  9. char id[6];
  10. char Jabatan[21];
  11. int Performa_Kerja;
  12. double Nilai_Dasprog;
  13. };
  14.  
  15. struct Validation
  16. {
  17. int id;
  18. int index;
  19. };
  20.  
  21. struct Pegawai database[100];
  22. struct Validation id[100000] =
  23. {0};
  24.  
  25. int counter = 0;
  26.  
  27. void shiftAndRemove(int index)
  28. {
  29. for (int i = index; i < counter; i++)
  30. {
  31. database[i] = database[i + 1];
  32. id[atoi(database[i].id)].index = i;
  33. }
  34. counter--;
  35. }
  36.  
  37. void addPegawai()
  38. {
  39. char Nama[20], Jabatan[20], new_id[6];
  40. int Performa_Kerja;
  41. double Nilai_Dasprog;
  42.  
  43. scanf("%s %s %s %d %lf", Nama, new_id, Jabatan, &Performa_Kerja, &Nilai_Dasprog);
  44.  
  45. if (id[atoi(new_id)].id)
  46. {
  47. printf("ERROR CANNOT CREATE.\n");
  48. } else
  49. {
  50. printf("DATA CREATED.\n");
  51. strcpy(database[counter].Nama, Nama);
  52. strcpy(database[counter].id, new_id);
  53. strcpy(database[counter].Jabatan, Jabatan);
  54. database[counter].Performa_Kerja = Performa_Kerja;
  55. database[counter].Nilai_Dasprog = Nilai_Dasprog;
  56. id[atoi(new_id)].id = 1;
  57. id[atoi(new_id)].index = counter;
  58. counter++;
  59. }
  60. }
  61.  
  62. void displayPegawai()
  63. {
  64. char read[100];
  65. scanf("%s", read);
  66.  
  67. if (strcmp(read, "ALL") == 0 && counter > 0)
  68. {
  69. for (int j = 0; j < counter; j++)
  70. {
  71. struct Pegawai *curr = database + j;
  72. printf("%s %s %s %d %.2f\n", curr->Nama, curr->id, curr->Jabatan, curr->Performa_Kerja, curr->Nilai_Dasprog);
  73. }
  74. } else
  75. {
  76. if (id[atoi(read)].id)
  77. {
  78. struct Pegawai *curr = database + id[atoi(read)].index;
  79. printf("%s %s %s %d %.2f\n", curr->Nama, curr->id, curr->Jabatan, curr->Performa_Kerja, curr->Nilai_Dasprog);
  80. } else
  81. {
  82. printf("DATA NOT FOUND.\n");
  83. }
  84. }
  85. }
  86.  
  87. void modifyPegawai(int update_id)
  88. {
  89. if (id[update_id].id)
  90. {
  91. struct Pegawai *curr = database + id[update_id].index;
  92. scanf("%s %s %d %lf", curr->Nama, curr->Jabatan, &curr->Performa_Kerja, &curr->Nilai_Dasprog);
  93. printf("DATA UPDATED.\n");
  94. } else
  95. {
  96. char tempNama[21], tempPos[21];
  97. int TempPerforma_Kerja;
  98. double Temp_Nilai_Dasprog;
  99. scanf("%s %s %d %lf", tempNama, tempPos, &TempPerforma_Kerja, &Temp_Nilai_Dasprog);
  100. printf("ERROR CANNOT UPDATE.\n");
  101. }
  102. }
  103.  
  104. void eliminatePegawai()
  105. {
  106. char delete[100];
  107. scanf("%s", delete);
  108.  
  109. if (strcmp(delete, "ALL") == 0 && counter > 0)
  110. {
  111. for (int i = 0; i < counter; i++)
  112. {
  113. id[atoi(database[i].id)].id = 0;
  114. }
  115. counter = 0;
  116. printf("DATA DELETED.\n");
  117. } else if (id[atoi(delete)].id)
  118. {
  119. shiftAndRemove(id[atoi(delete)].index);
  120. id[atoi(delete)].id = 0;
  121. printf("DATA DELETED.\n");
  122. } else
  123. {
  124. printf("ERROR CANNOT DELETE.\n");
  125. }
  126. }
  127.  
  128. int main()
  129. {
  130. int n;
  131. scanf("%d", &n);
  132.  
  133. for (int i = 0; i < n; i++)
  134. {
  135. struct Pegawai *curr = database + i;
  136. scanf("%s %s %s %d %lf", curr->Nama, curr->id, curr->Jabatan, &curr->Performa_Kerja, &curr->Nilai_Dasprog);
  137. id[atoi(curr->id)].id = 1;
  138. id[atoi(curr->id)].index = counter;
  139. counter++;
  140. }
  141.  
  142. int m;
  143. scanf("%d", &m);
  144.  
  145. for (int i = 0; i < m; i++)
  146. {
  147. char command[100];
  148. scanf("%s", command);
  149.  
  150. if (strcmp(command, "CREATE") == 0)
  151. {
  152. addPegawai();
  153. } else if (strcmp(command, "READ") == 0)
  154. {
  155. displayPegawai();
  156. } else if (strcmp(command, "UPDATE") == 0)
  157. {
  158. int update_id;
  159. char throw[3];
  160. scanf("%d %s", &update_id, throw);
  161. modifyPegawai(update_id);
  162. } else if (strcmp(command, "DELETE") == 0)
  163. {
  164. eliminatePegawai();
  165. } else
  166. {
  167. printf("INVALID COMMAND.\n");
  168. }
  169. }
  170.  
  171. return 0;
  172. }
  173.  
Success #stdin #stdout 0s 5308KB
stdin
3
Yanto 00181 Penjaga 99 97.54
Sucipto 00154 Penjaga 30 85.99
Surti 01905 Database 75 96.00
10
CREATE Raz 00181 Database 99 99.99
CREATE Raz 00182 Database 98 98.89
READ ALL
UPDATE 00180 TO Zan Hengker 94 95.67
UPDATE 00181 TO Zan Hengker 94 95.67
READ 00181
DELETE 00154
DELETE 00154
READ 00154
END
stdout
ERROR CANNOT CREATE.
DATA CREATED.
Yanto 00181 Penjaga 99 97.54
Sucipto 00154 Penjaga 30 85.99
Surti 01905 Database 75 96.00
Raz 00182 Database 98 98.89
ERROR CANNOT UPDATE.
DATA UPDATED.
Zan 00181 Hengker 94 95.67
DATA DELETED.
ERROR CANNOT DELETE.
DATA NOT FOUND.
INVALID COMMAND.