-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path第5关_基于顺序表的顺序查找.cpp
144 lines (134 loc) · 3.46 KB
/
第5关_基于顺序表的顺序查找.cpp
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
#include <bits/stdc++.h>
#define MAXSIZE 10000
using namespace std;
string allfilename;
typedef struct Food {
char name[100]; // 中文名称
char sname[100]; // 英文名称
char health[10000]; // 养生功效
char nutrition[10000]; // 营养与功效
char expert[10000]; // 专家提醒
char link[10000]; // 相关链接
string recipe[30]; // 养生保健食谱
int recipe_size = 0; // 食谱数量
string therapy[30]; // 食疗验方
int therapy_size = 0; // 验方数量
} Food;
typedef struct {
Food* elem; // 指向数组的指针
int length; // 数组的长度
} SqList;
void InitList(SqList& L) {
L.elem = new Food[MAXSIZE];
L.length = 0;
}
void FreeList(SqList& L) {
delete[] L.elem;
}
void ReadFood(Food& f, ifstream& infile) {
infile.getline(f.name, 100);
infile.getline(f.sname, 100);
infile.getline(f.health, 10000);
infile.getline(f.nutrition, 10000);
infile.getline(f.expert, 10000);
infile.getline(f.link, 10000);
string line;
int i = 0, j = 0;
b:while (getline(infile, line)) {
if (line == "#") {
break;
}
else if (line == "养生保健食谱:") {
streampos previousPosition = infile.tellg();
while (getline(infile, line)) {
if (line == "食疗验方:")
goto a;
else if (line == "#") return;
f.recipe[i++] = line;
f.recipe_size++;
}
}
else if (line == "食疗验方: ") {
a:streampos previousPosition = infile.tellg();
while (getline(infile, line)) {
if (line == "#")
{
infile.seekg(previousPosition);
goto b;
}
f.therapy_size++;
f.therapy[j++] = line;
}
}
}
}
void ReadFile(SqList& L, string filename) {
string line;
allfilename = filename;
int i = 0;
L.length = 0;
ifstream infile(filename);
if (!infile) {
cout << "文件打开失败" << endl;
exit(EXIT_FAILURE);
}
while (!infile.eof()) {
Food f;
ReadFood(f, infile);
L.elem[L.length++] = f;
}
infile.close();
}
int SeqSearch(SqList& L, char* sname) {
// 在顺序表L中顺序查找食材英文名称等于sname的数据元素
// 若找到,则返回该元素在表中的下标,否则返回-1
string strname = sname;
string strname2 = "英文名称:";
strname = strname2 + strname;
cout << strname << endl;
for (int i = 0; i < L.length; i++) {
if (strname == L.elem[i].sname)
return i;
}
return -1;
}
double GetASL(SqList& L) {
// 返回基于顺序表的顺序查找的ASL
return (double)(L.length + 1) / 2.0;
}
void Print(SqList& L, int pos) {
// 输出食材信息
cout << "中文名称:" << L.elem[pos].name << endl;
cout << "英文名称:" << L.elem[pos].sname << endl;
cout << "养生功效:" << L.elem[pos].health << endl;
cout << "营养与功效:" << L.elem[pos].nutrition << endl;
cout << "专家提醒:" << L.elem[pos].expert << endl;
cout << "相关链接:" << L.elem[pos].link << endl;
cout << "养生保健食谱:" << endl;
for (int i = 0; i < L.elem[pos].recipe_size; i++) {
cout << L.elem[pos].recipe[i] << endl;
}
cout << "食疗验方:" << endl;
for (int i = 0; i < L.elem[pos].therapy_size; i++) {
cout << L.elem[pos].therapy[i] << endl;
}
}
int main() {
SqList L;
InitList(L);
string originFilename = "food.txt";
ReadFile(L, originFilename);
char sname[100];
cin.getline(sname, 100);
int pos = SeqSearch(L, sname);
if (pos != -1) {
Print(L, pos);
double ASL = GetASL(L);
cout << "ASL为:" << fixed << setprecision(2) << ASL << endl;
}
else {
cout << "查找失败" << endl;
}
FreeList(L);
return 0;
}