|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
任务描述
从 herb.txt 中读取食材的基本信息,创建一个中药材信息的顺序表,基于该顺序表,实现食材基本信息的增加功能。
编程要求
输入中药材草药名、英文名、生长习性、繁殖方法、田间管理、病虫防治、采收加工、性味、归经、功效和功能主治,将该中药材的基本信息添加到herb.txt中的最后。新增成功后,输出 herb.txt 的最后一行即新增中药材信息。(注:由于中药材草药名是唯一的,因此在增加时需要判断该草药名是否存在于 herb.txt 中,若存在,则输出“增加失败”。)
- #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;
- }
复制代码
草药名:��:沉香
英文名:��:Aquilaria agallocha
生长习性:性:自木香生长在热带亚热带地区。对温度适应性强,“温年变幅在0~37C的地区都能生长,能耐短期箱冻。幼龄树耐阴,成龄树喜光,阳光充足有利子结香、对上壤适应性广,红壤或黄壤均可生长,在士层深厚肥沃的上壤上生长快,但结香少;在瘠薄的十壤上生长慢,但利于结香。
繁殖方法:法:选地与整地:选山地或丘陵肥力中等的缓坡,于冬季翻地挖穴,待来年春种植。行株距2米×1.5米,开穴深、宽各40厘米。每穴施腐熟厩肥或堆肥50~80千克。种子繁殖:于6~7月份采果实开裂的种子,随采随播于苗床。如不能及时播种,可用干沙混合后贮放于瓦盆中。播种行株距10厘米×5厘米。苗床温度27~29°C时,播后约半个月出苗。苗高50~80厘米出圃定植。
田间管理:理:幼龄树生长慢,可在行间种玉米、黄豆、砂姜等作物。成年树下可种砂仁、益智、绞股蓝等耐阴药物。结合中耕除草,春季施1次粪水或尿素,尿素每株25~50克;秋李施农家肥或钙镁磷肥,培土越冬。
病虫防治:治:每年6~7月份卷叶蛾为害叶片。在幼蛾卷叶前用90%敌百虫800倍液或80%敌敌畏1000倍液喷杀。
采收加工:工:一般种植10年以上、胸径15厘米以上的植株,取香质量较好。植株树脂形成(结香)主要受机械刺激或微生物影响所致。如在树干上凿数个阔长深(厘米)为2X5~10X5~10的长形洞,用泥土封闭,让其结香;或用利刀横切树干的一侧,每隔40~50厘米开一宽1厘米的洞,深为树干径的1/2,用特制的菌种塞满小洞后,用塑料薄膜包扎封口。当上下伤口的结香相连时,便整株砍下采香。用刀剔除木质部及腐烂部分.剩下褐黑的树脂即是中药沉香。
性味:�:辛、苦,微温。
归经:
功效:�:心
功能主治:��:脾 |
|