预计输出排序后所有食材英文名称
#include <bits/stdc++.h>
#define MAXSIZE 10000
using namespace std;
typedef struct{
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){
// 使用动态内存分配new进行初始化
L.elem = new Food[MAXSIZE];
L.length = 0;
}
void FreeList(SqList &L){
// 释放内存
if(L.elem!=NULL){
delete[] L.elem;
}
L.elem = NULL;
L.length = 0;
}
void ReadFile(SqList &L, string filename){
// 从文件中读取食材信息,将其按顺序存入L.elem指向的数组中
ifstream inFile(filename);
if (!inFile){
cout << "文件打开失败" << endl;
return;
}
char a[MAXSIZE];
L.length = 0;
bool flag = false;
while(inFile.getline(a, sizeof a, '#'))
{
Food food;
stringstream s(a);
string b;
int type = 0;
if (flag) getline(s, b , '\n');
flag = true;
while (getline(s, b , '\n'))
{
switch (type)
{
case 0: strcpy(food.name, b.substr(15).c_str()); break;
case 1: strcpy(food.sname, b.substr(15).c_str()); break;
case 2: strcpy(food.health, b.substr(15).c_str()); break;
case 3: strcpy(food.nutrition, b.substr(18).c_str()); break;
case 4: strcpy(food.expert, b.substr(15).c_str()); break;
case 5: strcpy(food.link, b.substr(15).c_str()); break;
case 6:
{
getline(s, b , '\n');
string::size_type c = b.find("——");
while (c != string::npos && !b.empty())
{
food.recipe[food.recipe_size++] = b;
getline(s, b , '\n');
c = b.find("——");
}
}
case 7:
{
getline(s, b , '\n');
while(!b.empty())
{
food.therapy[food.therapy_size++] = b;
getline(s, b , '\n');
}
}
default: break;
}
type++;
}
L.elem[++L.length] = food;
}
inFile.close();
}
void MSDSort(SqList &L, SqList &L2, int l, int r, int d){
//L为原始的顺序表,L2为辅助排序开辟的顺序表,l是排序的下界,r是排序的上界
//d为排序的字母的下标位置,从sname中下标为0的位置开始,往后递归处理
}
int main(){
SqList L, L2;
InitList(L);
ReadFile(L, "/data/workspace/myshixun/1.4.6-高位优先字符串排序/food.txt");
InitList(L2);
MSDSort(L, L2, 0, L.length-1, 0);
for (int i = 0; i < L.length; i++)
cout << L.elem[i].sname << endl;
FreeList(L);
FreeList(L2);
return 0;
}