간단하게 이름과 번호를 Console 창에 입력하여 저장하고 조회하고, 삭제하는 로직을 만들어 볼까 합니다.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define CAPACITY 100
#define BUFFER_SIZE 100
char *names[CAPACITY]; /* names */
char *numbers[CAPACITY]; /* phone numbers */
int n = 0; /* number of people in phone directory */
void add();
void find();
void delete();
void status();
int main() {
char command[BUFFER_SIZE];
while (1) {
printf("$ ");
scanf("%s", command);
if (strcmp(command, "add") == 0) {
add();
} else if (strcmp(command, "find") == 0) {
find();
} else if (strcmp(command, "status") == 0) {
status();
} else if (strcmp(command, "delete") == 0) {
delete();
} else if (strcmp(command, "exit") == 0)
break;
}
return 0;
};
void add(){
char nameTmp[BUFFER_SIZE], numberTmp[BUFFER_SIZE];
scanf("%s", nameTmp);
scanf("%s", numberTmp);
names[n] = strdup(nameTmp);
numbers[n] = strdup(numberTmp);
n++;
printf("%s was added successfully.\n", nameTmp);
}
void find(){
char findTmp[BUFFER_SIZE];
scanf("%s", findTmp);
int i ;
for (int i = 0; i < n; ++i) {
if (strcmp(findTmp, names[i]) == 0) {
printf("%s\n", numbers[i]);
return;
}
}
printf("No person name '%s' exists.\n", findTmp);
}
void status(){
int i ;
for (int i = 0; i < n; ++i) {
printf("%s %s\n", names[i], numbers[i]);
}
printf("Total %d persons.\n", n);
}
void delete(){
char deleteTmp[BUFFER_SIZE];
scanf("%s", deleteTmp);
int i ;
for (int i = 0; i < n; ++i) {
if (strcmp(deleteTmp, names[i]) == 0) {
names[i] = names[n - 1];
numbers[i] = numbers[n - 1];
n--;
printf("'%s' was deleted successfully. \n", deleteTmp);
return;
}
}
printf("No person name '%s' exists.\n", deleteTmp);
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define CAPACITY 100
#define BUFFER_SIZE 100
char *names[CAPACITY]; /* names */
char *numbers[CAPACITY]; /* phone numbers */
int n = 0; /* number of people in phone directory */
void add();
void find();
void delete();
void status();
int main() {
char command[BUFFER_SIZE];
while (1) {
printf("$ ");
scanf("%s", command);
if (strcmp(command, "add") == 0) {
add();
} else if (strcmp(command, "find") == 0) {
find();
} else if (strcmp(command, "status") == 0) {
status();
} else if (strcmp(command, "delete") == 0) {
delete();
} else if (strcmp(command, "exit") == 0)
break;
}
return 0;
};
void add(){
char nameTmp[BUFFER_SIZE], numberTmp[BUFFER_SIZE];
scanf("%s", nameTmp);
scanf("%s", numberTmp);
names[n] = strdup(nameTmp);
numbers[n] = strdup(numberTmp);
n++;
printf("%s was added successfully.\n", nameTmp);
}
void find(){
char findTmp[BUFFER_SIZE];
scanf("%s", findTmp);
int i ;
for (int i = 0; i < n; ++i) {
if (strcmp(findTmp, names[i]) == 0) {
printf("%s\n", numbers[i]);
return;
}
}
printf("No person name '%s' exists.\n", findTmp);
}
void status(){
int i ;
for (int i = 0; i < n; ++i) {
printf("%s %s\n", names[i], numbers[i]);
}
printf("Total %d persons.\n", n);
}
void delete(){
char deleteTmp[BUFFER_SIZE];
scanf("%s", deleteTmp);
int i ;
for (int i = 0; i < n; ++i) {
if (strcmp(deleteTmp, names[i]) == 0) {
names[i] = names[n - 1];
numbers[i] = numbers[n - 1];
n--;
printf("'%s' was deleted successfully. \n", deleteTmp);
return;
}
}
printf("No person name '%s' exists.\n", deleteTmp);
}