个人学习使用,博主实力低下希望大家能轻点喷,时间有限文章简陋,欢迎在评论区纠正错误。
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<cstring>
#include<cmath>
#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) {
// 释放内存
delete[] L.elem;
}
// 清除当前food当中的内容
void ClearFood(Food& food) {
memset(food.name, 0, sizeof(food.name));
memset(food.sname, 0, sizeof(food.sname));
memset(food.health, 0, sizeof(food.health));
memset(food.nutrition, 0, sizeof(food.nutrition));
memset(food.expert, 0, sizeof(food.expert));
memset(food.link, 0, sizeof(food.link));
food.recipe_size = 0;
food.therapy_size = 0;
}
// 读文件
// 读取文件内容到顺序表
void ReadFile(SqList& L, string filename) {
ifstream infile(filename);
if (!infile) {
cerr << "错误:无法打开文件" << filename << endl;
return;
}
const char prefixname[] = "中文名称:";
const char prefixsname[] = "英文名称:";
const char prefixhealth[] = "养生功效:";
const char prefixnutrition[] = "营养与功效:";
const char prefixexpert[] = "专家提醒:";
const char prefixlink[] = "相关链接:";
const char prefixrecipe[] = "养生保健食谱:";
const char prefixtherapy[] = "食疗验方:";
string line;
Food food;
InitList(L);
while (getline(infile, line) && !infile.eof()) {
ClearFood(food);
if (line.substr(0, strlen(prefixname)) == prefixname) {
int pos =