鱼C论坛

 找回密码
 立即注册
查看: 576|回复: 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
回复

使用道具 举报

发表于 2024-11-13 01:13:10 | 显示全部楼层
看来你的代码是为了从 `herb.txt` 文件中读取药材信息并存储在一个顺序表中,但代码似乎没有全部呈现出来。下面是一个关于如何完成这个任务的整体思路和实现,帮助你找到并解决代码问题。

### 整体解决方案:
1. **读取文件信息**:从 `herb.txt` 文件中读取已有的中药材信息,并存入顺序表。
2. **检查重复**:在新增之前检查草药名是否已存在。
3. **增加新信息**:如不存在,则将新药材信息添加到顺序表并写回文件。
4. **显示结果**:在成功添加后,显示加入的药材信息。

### 代码示例:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>

  6. using namespace std;

  7. typedef struct {
  8.     string name;                 // 草药名
  9.     string sname;                // 英文名
  10.     string growth_habits;        // 生长习性
  11.     string reproductive;         // 繁殖方法
  12.     string field_management;     // 田间管理
  13.     string pest_control;         // 病虫防治
  14.     string harvesting;           // 采收加工
  15.     string taste;                // 性味
  16.     string channel_tropism;      // 归经
  17.     string effect;               // 功效
  18.     string functional_indications; // 功能主治
  19. } Herb;

  20. class HerbList {
  21. public:
  22.     vector<Herb> herbs;

  23.     void readFromFile(const string& filename) {
  24.         ifstream infile(filename);
  25.         string line;
  26.         while (getline(infile, line)) {
  27.             istringstream iss(line);
  28.             Herb herb;
  29.             iss >> herb.name >> herb.sname >> herb.growth_habits >> herb.reproductive
  30.                 >> herb.field_management >> herb.pest_control >> herb.harvesting
  31.                 >> herb.taste >> herb.channel_tropism >> herb.effect >> herb.functional_indications;
  32.             herbs.push_back(herb);
  33.         }
  34.         infile.close();
  35.     }

  36.     bool isHerbExist(const string& herbName) {
  37.         for (const auto& herb : herbs) {
  38.             if (herb.name == herbName) {
  39.                 return true;
  40.             }
  41.         }
  42.         return false;
  43.     }

  44.     void addHerb(const Herb& herb, const string& filename) {
  45.         if (isHerbExist(herb.name)) {
  46.             cout << "增加失败" << endl;
  47.         } else {
  48.             herbs.push_back(herb);
  49.             ofstream outfile(filename, ios_base::app);
  50.             outfile << herb.name << " " << herb.sname << " " << herb.growth_habits << " "
  51.                     << herb.reproductive << " " << herb.field_management << " "
  52.                     << herb.pest_control << " " << herb.harvesting << " " << herb.taste << " "
  53.                     << herb.channel_tropism << " " << herb.effect << " "
  54.                     << herb.functional_indications << endl;
  55.             outfile.close();
  56.             cout << "添加成功: " << herb.name << endl;
  57.         }
  58.     }
  59. };

  60. int main() {
  61.     HerbList herbList;
  62.     string filename = "herb.txt";
  63.     herbList.readFromFile(filename);
  64.    
  65.     Herb newHerb;
  66.     // 假设以下值由用户输入获取
  67.     newHerb.name = "新草药";
  68.     newHerb.sname = "NewHerb";
  69.     newHerb.growth_habits = "生长习性";
  70.     newHerb.reproductive = "繁殖方法";
  71.     newHerb.field_management = "田间管理";
  72.     newHerb.pest_control = "病虫防治";
  73.     newHerb.harvesting = "采收加工";
  74.     newHerb.taste = "性味";
  75.     newHerb.channel_tropism = "归经";
  76.     newHerb.effect = "功效";
  77.     newHerb.functional_indications = "功能主治";

  78.     herbList.addHerb(newHerb, filename);
  79.    
  80.     return 0;
  81. }
复制代码


### 注意事项:
- 确保文件路径正确,文件存在并可读写。
- 用户输入的数据应当合法,并以适当格式存储。
- 每个字段应与 `herb.txt` 的文件结构匹配,方便正确解析。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-13 22:01:12 | 显示全部楼层
cy,蹲
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-18 14:56:19 | 显示全部楼层
c++忘光了,写了个c的给你提供一下思路

#include <stdio.h>
#include <string.h>       
#include<stdlib.h>
#include<search.h>
#define MAX_LINE_LENGTH  20000
#define NUM_STRINGS 10
// 冒泡排序
void bubbleSort(int positions[], int n) {
    int i, j;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (positions[j] > positions[j + 1]) {
                // 交换两个元素的位置
                int temp = positions[j];
                positions[j] = positions[j + 1];
                positions[j + 1] = temp;
            }
        }
    }
}
// 函数声明
int find_string(const char* haystack, const char* needle, int* position);

void split_and_save(const char* content, int positions[], int num_positions, const char* output_file);

int main() {
    FILE* input_file;
    fopen_s(&input_file, "11.txt", "r");
    if (input_file == NULL) {
        perror("无法打开输入文件");
        return 1;
    }

    // 读取文件内容
    char content[MAX_LINE_LENGTH];
   // if (fgets(content, MAX_LINE_LENGTH, input_file) == NULL) {
    size_t result = fread(content, 1, 1500, input_file);
    if (result < 2000) {
        if (feof(input_file)) {
            printf("提前到达文件末尾,只读取了 %zu 个元素\n", result);
        }
        else {
            perror("读取文件出错");
        }
    }
    fclose(input_file);
    content[result] ='\0' ;
    size_t len = strlen(content);
    printf_s("len:%d 读取文件内容: %s\n", len, content);
   // printf_s("len:%d 读取文件内容: %s\n", len,content);
    // 目标字符串
    char target_strings[NUM_STRINGS][10] = {
        "草药名", "英文名", "生长习性", "繁殖方法", "田间管理",
        "病虫防治", "采收加工", "性味",  "功能主治","功效"
    };

    // 记录匹配位置
    int positions[NUM_STRINGS] = { 0 };

    for (int i = 0; i < NUM_STRINGS; i++) {
        if (find_string(content, target_strings[i], &positions[i]) == -1) {
            printf("未找到字符串: %s\n", target_strings[i]);
        }
        else {
            printf("找到字符串: %s, 位置: %d\n", target_strings[i], positions[i]);
        }
    }

    //位置排序
    bubbleSort(positions, NUM_STRINGS);
    for (int i = 0; i < NUM_STRINGS; i++)
    {
        printf("%d排序位置: %d\n", i, positions[i]);
    }
    // 分割并保存结果
    split_and_save(content, positions, NUM_STRINGS, "2.txt");

    return 0;
}

int find_string(const char* haystack, const char* needle, int* position) {
    *position = strstr(haystack, needle) - haystack;
    return *position;
}

void split_and_save(const char* content, int positions[], int num_positions, const char* output_file) {
    // 获取文件长度
    size_t len = strlen(content);
    FILE* output;
    fopen_s(&output, output_file, "w");
    if (output == NULL) {
        perror("无法打开输出文件");
        return;
    }

   
    for (int i = 0; i < num_positions - 1; i++) {
        fwrite(content + positions[i], 1, positions[i+1]- positions[i], output);
        fputc('\n', output); // 每段之间添加换行符
    }

    ////最后一段


    fwrite(content + positions[num_positions-1], 1, len - positions[num_positions - 1], output);
   

    fclose(output);
}


屏幕截图 2024-11-18 145502.png
屏幕截图 2024-11-18 145514.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 00:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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