2301_76648344 2023-11-18 09:20 采纳率: 20%
浏览 11
已结题

c++实现文件删除,并输出被删除的内容,main函数不能修改

部分文档内容如下所示
中文名称:玉米
英文名称:corn
养生功效:补中开胃,益肺宁心,延缓衰老
营养与功效:玉米含有丰富的营养成分。其中,纤维素
专家提醒:玉米发霉后会产生致癌物,所以发霉
养生保健。
食疗验方:玉米须——玉米须30克,车前子15克,甘草
#
中文名称:小米
英文名称:millet
养生功效:健胃除
营养与功效:小米中因富含丰富的维生素B1、维生素B12等营养成分
专家提醒:小米中蛋白质的营养价值并不比大米好
相关链接:色素小米的危害∶在农贸市场上曾出现过染
养生保健食谱:
避免题目过长,省略一些内容,#号是真实存在在文档中

#include <bits/stdc++.h>
#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){
// 释放内存
if(L.elem!=NULL){
    free(L.elem);
}
L.elem = NULL;
L.length = 0;
}
void ReadFile(SqList &L, string filename){
// 从文件中读取食材信息,将其按顺序存入L.elem指向的数组中

}
void SaveFile(SqList &L, string filename){
// 保存食材信息到文件

}
Food *DeleteFood(SqList &L, char *name){
// 根据中文名称删除指定食材信息
// 如果删除成功,返回该食材的信息,否则,返回NULL

}
void Print(Food *food){
// 输出被删除食材的信息

}
bool check(SqList &L, char *name){
    for (int i = 0; i < L.length; i++){
        if (strcmp(name, L.elem[i].name) == 0){
            return false;
        }
    }
    return true;
}

int main(){
    SqList L;
    InitList(L);
    string originFilename = "/data/workspace/myshixun/1.1.2-食材信息删除/food.txt";
    string newFilename = "/data/workspace/myshixun/1.1.2-食材信息删除/new_food.txt";
    char name[100];
    cin >> name;
    ReadFile(L, originFilename);
    Food *food = DeleteFood(L, name);
    if (food){
        SaveFile(L, newFilename);
        ReadFile(L, newFilename);
        if (check(L, name))
            Print(food);
        else
            cout << "删除失败" << endl;
    }
    else{
        cout << "删除失败" << endl;
    }
    FreeList(L);
    return 0;
}

//输入要删除的食材中文名称,将该食材的全部信息从food.txt中删除,同时要求输出已删除食材对应的全部信息。(注:需要判断该中文名称是否存在于food.txt中,若存在,则执行删除操作,否则,输出“删除失败”。)
  • 写回答

8条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-11-18 09:20
    关注

    引自强大而免费的微信小程序:皆我百晓生

    以下是四个函数的代码:

    void ReadFile(SqList &L, string filename){
        ifstream inFile(filename);
        if (!inFile){
            cout << "文件打开失败" << endl;
            return;
        }
        string line;
        char temp[100];
        Food food;
        while (getline(inFile, line)){
            if (line == "#" && L.length > 0){
                L.elem[L.length++] = food;
                food = {};
            }
            else if (line.find("中文名称:") == 0){
                strncpy(food.name, line.substr(5).c_str(), sizeof(food.name)-1);
            }
            else if (line.find("英文名称:") == 0){
                strncpy(food.sname, line.substr(5).c_str(), sizeof(food.sname)-1);
            }
            else if (line.find("养生功效:") == 0){
                strncpy(food.health, line.substr(5).c_str(), sizeof(food.health)-1);
            }
            else if (line.find("营养与功效:") == 0){
                strncpy(food.nutrition, line.substr(6).c_str(), sizeof(food.nutrition)-1);
            }
            else if (line.find("专家提醒:") == 0){
                strncpy(food.expert, line.substr(5).c_str(), sizeof(food.expert)-1);
            }
            else if (line.find("相关链接:") == 0){
                strncpy(food.link, line.substr(6).c_str(), sizeof(food.link)-1);
            }
            else if (line.find("食疗验方:") == 0){
                strcpy(temp, line.substr(6).c_str());
                food.therapy[food.therapy_size++] = temp;
            }
            else if (line == ""){
                continue;
            }
            else{
                strcpy(temp, line.c_str());
                food.recipe[food.recipe_size++] = temp;
            }
        }
        inFile.close();
    }
    
    void SaveFile(SqList &L, string filename){
        ofstream outFile(filename);
        if (!outFile){
            cout << "文件打开失败" << endl;
            return;
        }
        for (int i = 0; i < L.length; i++){
            outFile << "中文名称:" << L.elem[i].name << endl;
            outFile << "英文名称:" << L.elem[i].sname << endl;
            outFile << "养生功效:" << L.elem[i].health << endl;
            outFile << "营养与功效:" << L.elem[i].nutrition << endl;
            outFile << "专家提醒:" << L.elem[i].expert << endl;
            outFile << "相关链接:" << L.elem[i].link << endl;
            for (int j = 0; j < L.elem[i].recipe_size; j++){
                outFile << L.elem[i].recipe[j] << endl;
            }
            if (L.elem[i].therapy_size > 0){
                outFile << "食疗验方:" << endl;
                for (int j = 0; j < L.elem[i].therapy_size; j++){
                    outFile << L.elem[i].therapy[j] << endl;
                }
            }
            outFile << "#" << endl;
        }
        outFile.close();
    }
    
    Food *DeleteFood(SqList &L, char *name){
        for (int i = 0; i < L.length; i++){
            if (strcmp(name, L.elem[i].name) == 0){
                Food *deletedFood = &L.elem[i];
                for (int j = i; j < L.length - 1; j++){
                    L.elem[j] = L.elem[j + 1];
                }
                L.length--;
                return deletedFood;
            }
        }
        return NULL;
    }
    
    void Print(Food *food){
        cout << "中文名称:" << food->name << endl;
        cout << "英文名称:" << food->sname << endl;
        cout << "养生功效:" << food->health << endl;
        cout << "营养与功效:" << food->nutrition << endl;
        cout << "专家提醒:" << food->expert << endl;
        cout << "相关链接:" << food->link << endl;
        for (int i = 0; i < food->recipe_size; i++){
            cout << food->recipe[i] << endl;
        }
        if (food->therapy_size > 0){
            cout << "食疗验方:" << endl;
            for (int i = 0; i < food->therapy_size; i++){
                cout << food->therapy[i] << endl;
            }
        }
    }
    
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月18日
  • 创建了问题 11月18日

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络