-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdelete.c
144 lines (141 loc) · 6.35 KB
/
delete.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "delete.h"
char deleteTarget[100];
COORD pos;
void hapus_data(){
if (totalBook == 0){
printf("| |\n");
printf("| Belum ada buku! |\n");
printf("| |\n");
printf("|-------------------------------------------------------------------|\n");
}
else if (totalBook == 1){
clearScreen();
printf("|-------------------------------------------------------------------|\n");
printf("| |\n");
printf("| HAPUS BUKU |\n");
printf("| |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| |\n");
printf("| Nama Buku Yang Akan Dihapus: |\n");
printf("| |\n");
printf("---------------------------------------------------------------------\n");
pos.X = 32;
pos.Y = 6;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
gets(deleteTarget);
pos.X = 0;
pos.Y = 9;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
fflush(stdin);
book *curr;
bool isFound = false;
curr = head;
while(curr != NULL){
if (strcmp(curr->name, deleteTarget) == 0){
isFound = true;
break;
}
curr = curr->next;
}
if (isFound){
// LL OPERATION
head = NULL;
free(curr);
curr = NULL;
totalBook--;
// FILE OPERATION
FILE *f;
f = fopen("data/book.txt", "w");
fclose(f);
printf("| |\n");
printf("| Buku Berhasil Dihapus! |\n");
printf("| |\n");
printf("---------------------------------------------------------------------\n");
}
else{
printf("| |\n");
printf("| Buku Tidak Ditemukan! |\n");
printf("| |\n");
printf("---------------------------------------------------------------------\n");
}
}
else{
clearScreen();
// LL OPERATION
printf("|-------------------------------------------------------------------|\n");
printf("| |\n");
printf("| HAPUS BUKU |\n");
printf("| |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| |\n");
printf("| Nama Buku Yang Akan Dihapus: |\n");
printf("| |\n");
printf("---------------------------------------------------------------------\n");
pos.X = 32;
pos.Y = 6;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
gets(deleteTarget);
pos.X = 0;
pos.Y = 9;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
fflush(stdin);
book *curr;
bool isFound = false;
curr = head;
while(curr != NULL){
if (strcmp(curr->name, deleteTarget) == 0){
isFound = true;
break;
}
curr = curr->next;
}
if (isFound){
if (curr == head){ // head
head = curr->next;
free(curr);
curr = NULL;
}
else if (curr->next == NULL){ // tail
book *prev;
prev = head;
while(prev->next != curr){
prev = prev->next;
}
prev->next = NULL;
free(curr);
curr = NULL;
}
else{ // middle
book *prev;
prev = head;
while(prev->next != curr){
prev = prev->next;
}
prev->next = curr->next;
free(curr);
curr = NULL;
}
totalBook--;
// FILE OPERATION
FILE *f;
f = fopen("data/book.txt", "w");
book *temp;
temp = head;
while(temp != NULL){
fprintf(f, "%s|%s|%d|%s|%s|%s\n", temp->name, temp->author, temp->year, temp->category, temp->created, temp->modified);
temp = temp->next;
}
fclose(f);
printf("| |\n");
printf("| Buku Berhasil Dihapus! |\n");
printf("| |\n");
printf("---------------------------------------------------------------------\n");
}
else{
printf("| |\n");
printf("| Buku Tidak Ditemukan! |\n");
printf("| |\n");
printf("---------------------------------------------------------------------\n");
}
}
}