[C언어] C언어로 전화번호부 만들기
//file io
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR 20
FILE *fp;
typedef struct struct_date {
int year, month, day;
}Sdate;
typedef struct struct_person {
char name[MAX_STR];
int telephone;
Sdate Birthday;
}Sperson;
int input;
int i = 0;
Sperson *p[100];
int pnumber=0;
void list() {
printf("\n");
printf("1.Add a person\n");
printf("2.Print the name\n");
printf("3.Name Search\n");
printf("4.Birthday this month!\n");
printf("5.Sort the phone book\n");
printf("6.Save the phone book\n");
printf("7.Load the phone book\n");
printf("0.Quit the program\n");
}
void addperson() {
p[i] = malloc(sizeof(Sperson));
char str[100];
int num;
int year, month, day;
printf("**Enter a person**\n");
printf("Enter the name:\n");
scanf("%s", str);
strcpy(p[i]->name, str);
printf("Enter the phone number:\n");
scanf("%d", &num);
p[i]->telephone = num;
printf("Enter the birthday(yyyy mm dd):\n");
scanf("%d %d %d", &year, &month, &day);
p[i]->Birthday.day = day;
p[i]->Birthday.month = month;
p[i]->Birthday.year = year;
printf("\n\n");
printf("Press any key to return...\n");
getc(stdin);
i++;
pnumber++;
}
void printthelist() {
printf("**Phone book**\n");
for (int k = 0; k <= (i-1); k++) {
printf("no.%d: %s\n", k + 1, p[k]->name);
printf("telephone: %d\n", p[k]->telephone);
printf("Birthday: %d.%d.%d\n", (p[k]->Birthday.year),(p[k]->Birthday.month), (p[k]->Birthday.day));
}
printf("Press any key to return....\n");
getc(stdin);
}
void namesearch() {
printf("write the name you are searching for\n");
char getname[MAX_STR];
scanf("%s",getname);
int got=0;
for (int a = 0; a <= (i-1); a++) {
got = strcmp(p[a]->name, getname);
if (got == 0) {
printf("name : %s\n", getname);
printf("telephone : %d\n",p[a]->telephone);
printf("birthday : %d. %d. %d\n", p[a]->Birthday.year, p[a]->Birthday.month, p[a]->Birthday.day);
}
else{
printf("Sorry.Not on List\n");
}
}
}
void birthdaythismonth() {
int month=0;
int noone=0;
printf("Enter the month: ");
scanf("%d",&month);
printf("Happy Birthdays to...\n");
for (int m = 0; m <=i-1; m++) {
if ((p[m]->Birthday.month) == month) {
printf("%s\n", p[m]->name);
}
else{
noone++;
}
}
if (noone==i){
printf("No birthday this month\n");
}
printf("\nPress any key to return...\n");
}
void save(){
int b=0;
for (b=0; b<i; b++){
fprintf(fp, "%s\n%d\n%04d%02d%02d\n",p[b]->name,p[b]->telephone, p[b]->Birthday.year,p[b]->Birthday.month, p[b]->Birthday.day);
}
printf("File Saved\n");
fclose(fp);
}
//compare은 sort하기 위해서 필요한 function이다.
int compare(const void *a, const void *b) // 오름차순 비교 함수 구현
{
int c=0;
c=strcmp((char*)a,(char*)b);
if(c>0){
//a가 b보다 크다
return 1;
}
else if (c<0){
return -1;
}
else{
return 0;
}
}
void sort(){
//stdlib에 있는 qsort()함수를 이용한다
qsort(p, pnumber, sizeof(p[0]), compare);
printf("SORTED!\n");
}
void load(){
//파일에서 정보를 읽어오는 함수
int f=0;
fp = fopen("1.txt", "w+");
if (fp==0){
printf("error at file loading!");
}
for(f=0; f<pnumber; f++){
fscanf(fp, "%s\n%d\n%04d%02d%02d", p[f]->name, &p[f]->telephone, &p[f]->Birthday.year, &p[f]->Birthday.month, &p[f]->Birthday.day);
}
printf("Loaded! go to menu2 to print the name");
}
void quit(){
printf("PROGRAM ENDED;");
exit(1);
}
int main() {
printf("Welcome to the PhoneBook! Press number to order :D\n");
fp = fopen("1.txt", "w+");
if (fp == NULL) {
printf("file opening error!");
}
int index=0;
while (1) {
list();
scanf("%d", &index);
if (index == 1) {
addperson();
}
else if (index == 2) {
printthelist();
}
else if (index == 3) {
namesearch();
}
else if (index == 4) {
birthdaythismonth();
}
else if (index == 5)
{
sort();
}
else if (index == 6) {
save();
}
else if (index==7){
load();
}
else if(index == 0) {
quit();
}
index = 0;
}
}