언어/c언어

c언어 조건에 맞는 사람 찾기

파아랑새 2016. 6. 12. 21:58
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _OR_ ||
#define _TRUE_ 1
typedef struct _INFO {
    int age; // 나이
    char name[100]; // 이름
    char area[100]; // 사는곳
    char ch[100]; // 취미
    char phone[100]; // 전화번호
    struct _INFO* link;
}INFO;
typedef struct _LINKEDLIST {
    INFO* head;
    INFO* tail;
    int cnt;
}LL;
// FUNCTION PROTOTYPE
void __error__(char* errorMessage);
void __init__(LL** L);
void __dataInput__(LL** L);
void __find__(LL** L);
 
void __manu__() {
    printf("회원 목록 작성 프로그램입니다. \n\n");
    printf("==========================\n");
    printf(" [1] ---- 작성 \n");
    printf(" [2] ---- 검색 \n");
    printf(" [3] ---- 작성종료 \n");
    printf("==========================\n");
// end of __manu__
 
int main(void) {
    LL* stu = NULL;
    __init__(&stu);
    int choice = 0;
    while (_TRUE_) {
        __manu__();
        printf("선택 : ");
        scanf_s("%d"&choice);
        fflush(stdin);
        system("cls");
        switch (choice){
            case 1: __dataInput__(&stu); break;
            case 2: __find__(&stu); break;
            case 3printf("종료\n"); 
                    goto end;
            default:printf("잘못 입력하셨습니다.\n");
                    break;
                 
        }
    
    }
    end:
    return 0;
// end of main
 
void __error__(char* errorMessage) {
    fprintf(stderr, "%s", errorMessage);
    exit(0);
// end of __error__
 
void __init__(LL** L){
    (*L) = (LL*)malloc(sizeof(LL));
    (*L)->head = NULL;
    (*L)->tail = NULL;
    (*L)->cnt = 0;
 
    (**L).head = (INFO*)malloc(sizeof(INFO));
    (**L).tail = (INFO*)malloc(sizeof(INFO));
    if (((**L).head == NULL) _OR_((**L).tail == NULL)){
        __error__("malloc fail!!");
    }
    else {// head != NULL _AND_ tail !=NULL
        memset((**L).head, 0sizeof(INFO));
        memset((**L).tail, 0sizeof(INFO));
    }
// end of __init__
 
void __dataInput__(LL** L) {
    INFO* newData = NULL;
    newData = (INFO*)malloc(sizeof(INFO));
    if (newData == NULL) {
        __error__("malloc fail!!");
    }
    else { // newData != NULL
        
        memset(newData, 0sizeof(INFO));
        fputs("이름 : ", stdout);
        gets_s(newData->name, 100);
        fflush(stdin);
        fputs("나이 : ", stdout);
        scanf_s("%d"&(newData->age));
        fflush(stdin);
        fputs("사는곳 : ", stdout);
        gets_s(newData->area, 100);
        fflush(stdin);
        fputs("취미 : ", stdout);
        gets_s(newData->ch, 100);
        fflush(stdin);
        fputs("전화번호 : ", stdout);
        gets_s(newData->phone, 100);
        if ((**L).cnt == 0) {
            (*L)->head->link = newData;
            (*L)->tail->link = newData;
            (**L).cnt += 1;
        }
        else { // (**L).cnt != 0
            (*L)->tail->link->link = newData;
            (*L)->tail->link = newData;
            (**L).cnt += 1;
        }
        system("cls");
    }
// end of __dataInput__
 
void __find__(LL** L) {
    FILE *f;
    fopen_s(&f,"find.txt""w");
    char findName[100];
    int i;
    INFO* Index = NULL;
    if ((**L).cnt == 0) {
        printf("리스트에 데이터가 없습니다. \n");
        __error__("empty");
    }
    else {
        strcpy_s(findName, 100" ");
        fputs(" 찾는 사람 이름: ", stdout);
        gets_s(findName, 100);
        fflush(stdin);
        Index = (*L)->head->link;
        for (i = 0; i < (**L).cnt; i++) {
            if (strcmp(findName, Index->name) == 0) {
                fputs("======================================\n", stdout);
                fputs("검색결과맞는 결과를 찾았습니다.\n", stdout);
                fprintf(stdout, " 나이     -> %d \n", Index->age);
                fprintf(stdout, " 사는곳   -> %s \n", Index->area);
                fprintf(stdout, " 취미     -> %s \n", Index->ch);
                fprintf(stdout, " 전화번호 -> %s \n", Index->phone);
                fputs("======================================\n", stdout);
                //fprintf(f, Index->age);
                //fprintf(f, "\n");
                fprintf(f, Index->area);
                fprintf(f, "\n");
                fprintf(f, Index->ch);
                fprintf(f, "\n");
                fprintf(f, Index->phone);
                fprintf(f, "\n");
                fclose(f);
                return;// END
            }
            else {
                Index = Index->link;
            }
        }
        fputs("======================================\n", stdout);
        fputs("검색 조건에 맞는 결과가 없습니다.\n", stdout);
        fputs("======================================\n", stdout);
    }
}// end of __find__
 
cs