鱼C论坛

 找回密码
 立即注册
查看: 960|回复: 3

实在改不出来了,运行情况如下,求大神帮帮

[复制链接]
发表于 2024-11-13 01:06:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
任务描述
从 herb.txt 中读取食材的基本信息,创建一个中药材信息的顺序表,基于该顺序表,实现食材基本信息的增加功能。

编程要求
输入中药材草药名、英文名、生长习性、繁殖方法、田间管理、病虫防治、采收加工、性味、归经、功效和功能主治,将该中药材的基本信息添加到herb.txt中的最后。新增成功后,输出 herb.txt 的最后一行即新增中药材信息。(注:由于中药材草药名是唯一的,因此在增加时需要判断该草药名是否存在于 herb.txt 中,若存在,则输出“增加失败”。)

  1. #include <bits/stdc++.h>
  2. #define MAXSIZE 10000
  3. using namespace std;

  4. typedef struct {
  5.     char name[100];                                     // 草药名
  6.     char sname[100];                            // 英文名
  7.     char growth_habits[5000];                  // 生长习性
  8.     char reproductive[5000];                   // 繁殖方法
  9.     char field_management[5000];                  // 田间管理
  10.     char pest_control[5000];           // 病虫防治
  11.     char harvesting[5000];                  // 采收加工
  12.     char taste[5000];                                // 性味
  13.     string channel_tropism[6];                // 归经
  14.     int channel_tropism_num = 0;                // 归经数量
  15.     char effect[5000];                               // 功效
  16.       char functional_indications[5000]; // 功能主治
  17. } Herb;

  18. typedef struct {
  19.     Herb* elem;                 // 指向数组的指针
  20.     int length;                 // 数组的长度
  21. } SqList;

  22. void InitList(SqList& L) {
  23.     // 使用动态内存分配new进行初始化
  24.     L.elem = new Herb[MAXSIZE];
  25.     if(!L.elem) exit(1);
  26.     L.length = 0;
  27. }

  28. void FreeList(SqList& L) {
  29.     // 释放内存
  30.     delete[] L.elem;
  31.     L.length = 0;
  32. }

  33. void ReadFile(SqList& L, string filename) {
  34.    // 从文件中读取中药材信息,将其按顺序存入L.elem指向的数组中
  35. ifstream infile(filename);
  36.     if (!infile.is_open()) {
  37.         cerr << "无法打开文件: " << filename << endl;
  38.         exit(1);
  39.     }

  40.     char s[5000];
  41.     bool flag = false;
  42.     while (infile.getline(s, sizeof s, '#')) {
  43.         Herb temp;
  44.         stringstream streams(s);
  45.         string ss;
  46.         int type = 0;
  47.         if (flag)
  48.             getline(streams, ss, '\n');
  49.         flag = true;
  50.         while (getline(streams, ss, '\n')) {
  51.             switch (type) {
  52.                 case 0:
  53.                     strcpy(temp.name, ss.substr(7).c_str()); // 草药名:
  54.                     break;
  55.                 case 1:
  56.                     strcpy(temp.sname, ss.substr(7).c_str()); // 英文名:
  57.                     break;
  58.                 case 2:
  59.                     strcpy(temp.growth_habits, ss.substr(9).c_str()); // 生长习性:
  60.                     break;
  61.                 case 3:
  62.                     strcpy(temp.reproductive, ss.substr(9).c_str()); // 繁殖方法:
  63.                     break;
  64.                 case 4:
  65.                     strcpy(temp.field_management, ss.substr(9).c_str()); // 田间管理:
  66.                     break;
  67.                 case 5:
  68.                     strcpy(temp.pest_control, ss.substr(9).c_str()); // 病虫防治:
  69.                     break;
  70.                 case 6:
  71.                     strcpy(temp.harvesting, ss.substr(9).c_str()); // 采收加工:
  72.                     break;
  73.                 case 7:
  74.                     strcpy(temp.taste, ss.substr(5).c_str()); // 性味:
  75.                     break;
  76.                 case 8: {
  77.                     size_t colonPos = ss.find(':');
  78.                     if (colonPos != string::npos) {
  79.                         string channels = ss.substr(colonPos);
  80.                         stringstream channelsStream(channels);
  81.                         string channel;
  82.                         while (getline(channelsStream, channel, ',')) {
  83.                             if (temp.channel_tropism_num < 6) { // 防止数组越界
  84.                                 temp.channel_tropism[temp.channel_tropism_num++] = channel;
  85.                             }
  86.                         }
  87.                     }
  88.                     break;
  89.                 }
  90.                 case 9:
  91.                     strcpy(temp.effect, ss.substr(5).c_str()); // 功效:
  92.                     break;
  93.                 case 10:
  94.                     strcpy(temp.functional_indications, ss.substr(10).c_str()); // 功能主治:
  95.                     break;
  96.                 default:
  97.                     break;
  98.             }
  99.             type++;
  100.         }
  101.         L.elem[L.length++] = temp;
  102.     }
  103.     infile.close();
  104. }

  105. void SaveFile(SqList& L, string filename) {
  106.     // 保存中药材信息到文件
  107.     ofstream outfile(filename);
  108.     for (int i = 0; i < L.length; ++i) {
  109.         outfile << "草药名:" << L.elem[i].name << "\n"
  110.                 << "英文名:" << L.elem[i].sname << "\n"
  111.                 << "生长习性:" << L.elem[i].growth_habits << "\n"
  112.                 << "繁殖方法:" << L.elem[i].reproductive << "\n"
  113.                 << "田间管理:" << L.elem[i].field_management << "\n"
  114.                 << "病虫防治:" << L.elem[i].pest_control << "\n"
  115.                 << "采收加工:" << L.elem[i].harvesting << "\n"
  116.                 << "性味:" << L.elem[i].taste << "\n"
  117.                 << "归经:";
  118.         for (int j = 0; j < L.elem[i].channel_tropism_num; ++j) {
  119.             outfile << L.elem[i].channel_tropism[j] << ",";
  120.         }
  121.         outfile << "\n"
  122.                 << "功效:" << L.elem[i].effect << "\n"
  123.                 << "功能主治:" << L.elem[i].functional_indications << "\n"
  124.                 << "#\n";
  125.     }
  126.     outfile.close();
  127. }

  128. bool InsertHerb(SqList& L) {
  129.     // 插入中药材信息,输入中药材的草药名、英文名、生长习性、繁殖方法、田间管理、病虫防治、采收加工、性味、归经、功效和功能主治信息
  130.     // 如果插入成功,返回true,否则,返回false
  131.     Herb newHerb;

  132.     string name, sname, growth_habits, reproductive, field_management, pest_control, harvesting, taste, effect, functional_indications;

  133.     getline(cin, name);
  134.     strncpy(newHerb.name, name.c_str(), 99);
  135.     newHerb.name[99] = '\0';

  136.     getline(cin, sname);
  137.     strncpy(newHerb.sname, sname.c_str(), 99);
  138.     newHerb.sname[99] = '\0';

  139.     getline(cin, growth_habits);
  140.     strncpy(newHerb.growth_habits, growth_habits.c_str(), 4999);
  141.     newHerb.growth_habits[4999] = '\0';

  142.     getline(cin, reproductive);
  143.     strncpy(newHerb.reproductive, reproductive.c_str(), 4999);
  144.     newHerb.reproductive[4999] = '\0';

  145.     getline(cin, field_management);
  146.     strncpy(newHerb.field_management, field_management.c_str(), 4999);
  147.     newHerb.field_management[4999] = '\0';

  148.     getline(cin, pest_control);
  149.     strncpy(newHerb.pest_control, pest_control.c_str(), 4999);
  150.     newHerb.pest_control[4999] = '\0';

  151.     getline(cin, harvesting);
  152.     strncpy(newHerb.harvesting, harvesting.c_str(), 4999);
  153.     newHerb.harvesting[4999] = '\0';

  154.     getline(cin, taste);
  155.     strncpy(newHerb.taste, taste.c_str(), 4999);
  156.     newHerb.taste[4999] = '\0';

  157.     string channelInput;
  158.     getline(cin, channelInput);
  159.     stringstream channelStream(channelInput);
  160.     string channel;
  161.     while (getline(channelStream, channel, ',')) {
  162.         if (newHerb.channel_tropism_num < 6) {
  163.             newHerb.channel_tropism[newHerb.channel_tropism_num++] = channel;
  164.         }
  165.     }

  166.     getline(cin, effect);
  167.     strncpy(newHerb.effect, effect.c_str(), 4999);
  168.     newHerb.effect[4999] = '\0';

  169.     getline(cin, functional_indications);
  170.     strncpy(newHerb.functional_indications, functional_indications.c_str(), 4999);
  171.     newHerb.functional_indications[4999] = '\0';

  172.     for (int i = 0; i < L.length; ++i) {
  173.         if (strcmp(L.elem[i].name, newHerb.name) == 0) {
  174.             cout << "增加失败" << endl;
  175.             return false;
  176.         }
  177.     }
  178.     L.elem[L.length++] = newHerb;
  179.     return true;
  180. }

  181. void Print(SqList& L) {
  182.     // 输出中药材信息
  183.     if (L.length > 0) {
  184.         Herb lastHerb = L.elem[L.length - 1];
  185.         cout << "草药名:" << lastHerb.name << "\n"
  186.              << "英文名:" << lastHerb.sname << "\n"
  187.              << "生长习性:" << lastHerb.growth_habits << "\n"
  188.              << "繁殖方法:" << lastHerb.reproductive << "\n"
  189.              << "田间管理:" << lastHerb.field_management << "\n"
  190.              << "病虫防治:" << lastHerb.pest_control << "\n"
  191.              << "采收加工:" << lastHerb.harvesting << "\n"
  192.              << "性味:" << lastHerb.taste << "\n"
  193.              << "归经:";
  194.         for (int j = 0; j < lastHerb.channel_tropism_num; ++j) {
  195.             cout << lastHerb.channel_tropism[j];
  196.             if (j < lastHerb.channel_tropism_num - 1) {
  197.                 cout << ", "; // 控制归经间的分隔符
  198.             }
  199.         }
  200.         cout << "\n"
  201.              << "功效:" << lastHerb.effect << "\n"
  202.              << "功能主治:" << lastHerb.functional_indications << "\n";
  203.     } else {
  204.         cout << "没有数据" << endl;
  205.     }
  206. }
  207. int main(){       
  208.         SqList L;
  209.         InitList(L);
  210.         string originFilename = "/data/workspace/myshixun/1.1.1-中药材信息增加/herb.txt";
  211.         string newFilename = "/data/workspace/myshixun/1.1.1-中药材信息增加/new_herb.txt";
  212.         ReadFile(L, originFilename);

  213.         if (InsertHerb(L)){
  214.                 SaveFile(L, newFilename);
  215.                 ReadFile(L, newFilename);
  216.                 Print(L);
  217.         }
  218.         else{
  219.                 cout << "增加失败" << endl;
  220.         }
  221.         FreeList(L);
  222.         return 0;
  223. }
复制代码

草药名:&#65533;&#65533;:沉香
英文名:&#65533;&#65533;: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,用特制的菌种塞满小洞后,用塑料薄膜包扎封口。当上下伤口的结香相连时,便整株砍下采香。用刀剔除木质部及腐烂部分.剩下褐黑的树脂即是中药沉香。
性味:&#65533;:辛、苦,微温。
归经:
功效:&#65533;:心
功能主治:&#65533;&#65533;:脾
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-29 12:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表