-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC2019.c
More file actions
141 lines (137 loc) · 2.52 KB
/
C2019.c
File metadata and controls
141 lines (137 loc) · 2.52 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct Node
{
char tu[101];
char nghia[101];
struct Node *left;
struct Node *right;
} Node;
char tu[101];
char nghia[101];
Node *r = NULL;
Node *makeNode(char tu[], char nghia[])
{
Node *newNode = (Node *)malloc(sizeof(Node));
strcpy(newNode->tu, tu);
strcpy(newNode->nghia, nghia);
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node *insert(Node *r, char tu[], char nghia[])
{
if (r == NULL)
{
r = makeNode(tu, nghia);
}
else if (strcmp(tu, r->tu) < 0)
{
r->left = insert(r->left, tu, nghia);
}
else if (strcmp(tu, r->tu) > 0)
{
r->right = insert(r->right, tu, nghia);
}
return r;
}
void inorder(Node *r)
{
if (r != NULL)
{
inorder(r->left);
printf("%s\t%s\n", r->tu, r->nghia);
inorder(r->right);
}
}
void nhap()
{
FILE *fp;
fp = fopen("A.txt", "r");
while (!feof(fp))
{
fscanf(fp, "%s %s", tu, nghia);
r = insert(r, tu, nghia);
}
}
void menu()
{
printf("========================\n");
printf("1. Duyet cay\n");
printf("2. Them tu\n");
printf("3. Tim tu\n");
printf("4. Thoat\n");
}
void process1()
{
inorder(r);
}
void process2()
{
printf("Nhap tu: ");
scanf("%s", tu);
printf("Nhap nghia: ");
scanf("%s", nghia);
r = insert(r, tu, nghia);
}
Node *find(Node *r, char tu[])
{
if (r == NULL)
{
return NULL;
}
else
{
if (strcmp(tu, r->tu) < 0)
{
printf("%s\n", r->tu);
return find(r->left, tu);
}
else if (strcmp(tu, r->tu) > 0)
{
printf("%s\n", r->tu);
return find(r->right, tu);
}
else
{
printf("%s\t", r->tu);
printf("%s\n", r->nghia);
return r;
}
}
}
void process3()
{
printf("Nhap tu: ");
scanf("%s", tu);
Node* h=find(r, tu);
if(h==NULL)
{
printf("Khong tim thay tu\n");
}
}
int main()
{
nhap();
int x;
do
{
menu();
scanf("%d", &x);
if (x < 1 || x > 4)
{
printf("Nhap sai!. Hay nhap lai chuc nang: \n");
scanf("%d", &x);
}
if (x == 1)
process1();
if (x == 2)
process2();
if (x == 3)
process3();
if (x == 4)
break;
} while (1 <= x && x <= 4);
}