-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.c
179 lines (152 loc) · 3.73 KB
/
product.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <string.h>
typedef struct{
char name[20];
int weight;
int price;
int rate[1000];
int num_rate;
} Product;
int createProduct(Product *p){
printf("과일이름은? ");
scanf("%[^\n]" , p->name);
printf("과일 무게는?(g) ");
scanf("%d", &p->weight);
printf("가격은? ");
scanf("%d", &p->price);
printf("별점은?(1~5) ");
scanf("%d", &p->rate[0]);
p->num_rate++;
// 별점기능 추가
return 1;
}
int readProduct(Product p){
printf("%s\t%d\t%d\t%d\t%d\n", p.name, p.weight, p.price, p.rate[0], p.num_rate);
return 1;
}
int updateProduct(Product *p){
printf("과일이름은? ");
scanf("%[^\n]" , p->name);
printf("과일 무게는?(g) ");
scanf("%d", &p->weight);
printf("가격은? ");
scanf("%d", &p->price);
printf("별점은?(1~5) ");
scanf("%d", &p->rate[0]);
p->num_rate = 0;
p->num_rate++;
return 1;
}
int deleteProduct(Product *p){
p->price = -1;
return 1;
}
int selectMenu(){
int menu;
printf("\n***Fruits***\n");
printf("1. 제품조회\n");
printf("2. 제품추가\n");
printf("3. 제품수정\n");
printf("4. 제품삭제\n");
printf("5. 제품검색\n");
printf("6. 제품저장\n");
printf("0. 종료\n");
printf("\n=>원하는 메뉴는? ");
scanf("%d", &menu);
while (getchar() != '\n')
continue;
return menu;
}
void listName(){
printf("**************************************************\n");
printf("No.\t이름\t무게(g)\t가격\t별점\t별점갯수\n");
}
int searchByName(Product *p, int count){
int scount = 0;
char search[20];
printf("검색할 이름은? ");
scanf("%[^\n]", search);
listName();
for(int i = 0; i < count; i++){
if(p[i].price == -1)
continue;
if(strstr(p[i].name, search)){
printf("%d\t", i+1);
readProduct(p[i]);
scount++;
}
}
printf("\n");
return scount;
}
int searchByPrice(Product *p, int count){
int scount = 0;
int smin, smax;
printf("검색할 가격대를 입력하세요?\n");
printf("최소 가격은? ");
scanf("%d", &smin);
printf("최대 가격은? ");
scanf("%d", &smax);
listName();
for(int i = 0; i < count; i++){
if(p[i].price == -1)
continue;
if(p[i].price >= smin && p[i].price <= smax){
printf("%d\t", i+1);
readProduct(p[i]);
scount++;
}
}
printf("\n");
return scount;
}
int searchByRate(Product *p, int count){
int scount = 0;
int srate;
printf("검색할 별점은?(1~5) ");
scanf("%d", &srate);
listName();
for(int i = 0; i < count; i++){
if(p[i].price == -1)
continue;
if(p[i].rate[0] == srate){
printf("%d\t", i+1);
readProduct(p[i]);
scount++;
}
}
printf("\n");
return scount;
}
void saveProduct(Product *p, int count){
FILE *fp;
fp = fopen("product.txt", "wt");
for(int i = 0; i < count; i++){
if(p[i].price == -1)
continue;
fprintf(fp,"%s\t%d\t%d\t%d\t%d\n", p[i].name, p[i].weight, p[i].price, p[i].rate[0], p[i].num_rate);
}
fclose(fp);
printf("=>제품 리스트가 저장되었습니다.\n");
}
int loadProduct(Product *p){
int i = 0;
FILE *fp;
fp = fopen("product.txt", "rt");
if(fp == NULL)
printf("=>파일이 존재하지 않습니다.\n");
else{
for(; i < 100; i++){
fscanf(fp,"%[^\t]", p[i].name);
if(feof(fp))
break;
fscanf(fp, "%d ", &p[i].weight);
fscanf(fp, "%d ", &p[i].price);
fscanf(fp, "%d ", &p[i].rate[0]);
fscanf(fp, "%d ", &p[i].num_rate);
}
printf("=>로딩에 성공하였습니다.\n");
}
fclose(fp);
return i;
}