#include <bits/stdc++.h>
#define MAXSIZE 10000
using namespace std;
typedef struct {
char name[100]; // 草药名
char sname[100]; // 英文名
char growth_habits[5000]; // 生长习性
char reproductive[5000]; // 繁殖方法
char field_management[5000]; // 田间管理
char pest_control[5000]; // 病虫防治
char harvesting[5000]; // 采收加工
char taste[5000]; // 性味
string channel_tropism[6]; // 归经
int channel_tropism_num = 0; // 归经数量
char effect[5000]; // 功效
char functional_indications[5000]; // 功能主治
} Herb;
typedef struct {
Herb* elem; // 指向数组的指针
int length; // 数组的长度
} SqList;
void InitList(SqList& L) {
// 使用动态内存分配new进行初始化
L.elem = new Herb[MAXSIZE];
if(!L.elem) exit(1);
L.length = 0;
}
void FreeList(SqList& L) {
// 释放内存
delete[] L.elem;
L.length = 0;
}
void ReadFile(SqList& L, string filename) {
// 从文件中读取中药材信息,将其按顺序存入L.elem指向的数组中
ifstream infile(filename);
if (!infile.is_open()) {
cerr << "无法打开文件: " << filename << endl;
exit(1);
}
char s[5000];
bool flag = false;
while (infile.getline(s, sizeof s, '#')) {
Herb temp;
stringstream streams(s);
string ss;
int type = 0;
if (flag)
getline(streams, ss, '\n');
flag = true;
while (getline(streams, ss, '\n')) {
switch (type) {
case 0:
strcpy(temp.name, ss.substr(7).c_str()); // 草药名:
break;
case 1:
strcpy(temp.sname, ss.substr(7).c_str()); // 英文名:
break;
case 2:
strcpy(temp.growth_habits, ss.substr(9).c_str()); // 生长习性:
break;
case 3:
strcpy(temp.reproductive, ss.substr(9).c_str()); // 繁殖方法:
break;
case 4:
strcpy(temp.field_management, ss.substr(9).c_str()); // 田间管理:
break;
case 5:
strcpy(temp.pest_control, ss.substr(9).c_str()); // 病虫防治:
break;
case 6:
strcpy(temp.harvesting, ss.substr(9).c_str()); // 采收加工:
break;
case 7:
strcpy(temp.taste, ss.substr(5).c_str()); // 性味:
break;
case 8: {
size_t colonPos = ss.find(':');
if (colonPos != string::npos) {
string channels = ss.substr(colonPos);
stringstream channelsStream(channels);
string channel;
while (getline(channelsStream, channel, ',')) {
if (temp.channel_tropism_num < 6) { // 防止数组越界
temp.channel_tropism[temp.channel_tropism_num++] = channel;
}
}
}
break;
}
case 9:
strcpy(temp.effect, ss.substr(5).c_str()); // 功效:
break;
case 10:
strcpy(temp.functional_indications, ss.substr(10).c_str()); // 功能主治:
break;
default:
break;
}
type++;
}
L.elem[L.length++] = temp;
}
infile.close();
}
void SaveFile(SqList& L, string filename) {
// 保存中药材信息到文件
ofstream outfile(filename);
for (int i = 0; i < L.length; ++i) {
outfile << "草药名:" << L.elem[i].name << "\n"
<< "英文名:" << L.elem[i].sname << "\n"
<< "生长习性:" << L.elem[i].growth_habits << "\n"
<< "繁殖方法:" << L.elem[i].reproductive << "\n"
<< "田间管理:" << L.elem[i].field_management << "\n"
<< "病虫防治:" << L.elem[i].pest_control << "\n"
<< "采收加工:" << L.elem[i].harvesting << "\n"
<< "性味:" << L.elem[i].taste << "\n"
<< "归经:";
for (int j = 0; j < L.elem[i].channel_tropism_num; ++j) {
outfile << L.elem[i].channel_tropism[j] << ",";
}
outfile << "\n"
<< "功效:" << L.elem[i].effect << "\n"
<< "功能主治:" << L.elem[i].functional_indications << "\n"
<< "#\n";
}
outfile.close();
}
bool InsertHerb(SqList& L) {
// 插入中药材信息,输入中药材的草药名、英文名、生长习性、繁殖方法、田间管理、病虫防治、采收加工、性味、归经、功效和功能主治信息
// 如果插入成功,返回true,否则,返回false
Herb newHerb;
string name, sname, growth_habits, reproductive, field_management, pest_control, harvesting, taste, effect, functional_indications;
getline(cin, name);
strncpy(newHerb.name, name.c_str(), 99);
newHerb.name[99] = '\0';
getline(cin, sname);
strncpy(newHerb.sname, sname.c_str(), 99);
newHerb.sname[99] = '\0';
getline(cin, growth_habits);
strncpy(newHerb.growth_habits, growth_habits.c_str(), 4999);
newHerb.growth_habits[4999] = '\0';
getline(cin, reproductive);
strncpy(newHerb.reproductive, reproductive.c_str(), 4999);
newHerb.reproductive[4999] = '\0';
getline(cin, field_management);
strncpy(newHerb.field_management, field_management.c_str(), 4999);
newHerb.field_management[4999] = '\0';
getline(cin, pest_control);
strncpy(newHerb.pest_control, pest_control.c_str(), 4999);
newHerb.pest_control[4999] = '\0';
getline(cin, harvesting);
strncpy(newHerb.harvesting, harvesting.c_str(), 4999);
newHerb.harvesting[4999] = '\0';
getline(cin, taste);
strncpy(newHerb.taste, taste.c_str(), 4999);
newHerb.taste[4999] = '\0';
string channelInput;
getline(cin, channelInput);
stringstream channelStream(channelInput);
string channel;
while (getline(channelStream, channel, ',')) {
if (newHerb.channel_tropism_num < 6) {
newHerb.channel_tropism[newHerb.channel_tropism_num++] = channel;
}
}
getline(cin, effect);
strncpy(newHerb.effect, effect.c_str(), 4999);
newHerb.effect[4999] = '\0';
getline(cin, functional_indications);
strncpy(newHerb.functional_indications, functional_indications.c_str(), 4999);
newHerb.functional_indications[4999] = '\0';
for (int i = 0; i < L.length; ++i) {
if (strcmp(L.elem[i].name, newHerb.name) == 0) {
cout << "增加失败" << endl;
return false;
}
}
L.elem[L.length++] = newHerb;
return true;
}
void Print(SqList& L) {
// 输出中药材信息
if (L.length > 0) {
Herb lastHerb = L.elem[L.length - 1];
cout << "草药名:" << lastHerb.name << "\n"
<< "英文名:" << lastHerb.sname << "\n"
<< "生长习性:" << lastHerb.growth_habits << "\n"
<< "繁殖方法:" << lastHerb.reproductive << "\n"
<< "田间管理:" << lastHerb.field_management << "\n"
<< "病虫防治:" << lastHerb.pest_control << "\n"
<< "采收加工:" << lastHerb.harvesting << "\n"
<< "性味:" << lastHerb.taste << "\n"
<< "归经:";
for (int j = 0; j < lastHerb.channel_tropism_num; ++j) {
cout << lastHerb.channel_tropism[j];
if (j < lastHerb.channel_tropism_num - 1) {
cout << ", "; // 控制归经间的分隔符
}
}
cout << "\n"
<< "功效:" << lastHerb.effect << "\n"
<< "功能主治:" << lastHerb.functional_indications << "\n";
} else {
cout << "没有数据" << endl;
}
}
int main(){
SqList L;
InitList(L);
string originFilename = "/data/workspace/myshixun/1.1.1-中药材信息增加/herb.txt";
string newFilename = "/data/workspace/myshixun/1.1.1-中药材信息增加/new_herb.txt";
ReadFile(L, originFilename);
if (InsertHerb(L)){
SaveFile(L, newFilename);
ReadFile(L, newFilename);
Print(L);
}
else{
cout << "增加失败" << endl;
}
FreeList(L);
return 0;
}
草药名:��:沉香