数据结构-王卓-病毒检测-单向链表

#include <iostream>  // cout
#include <fstream>   // ifstream\ofstream
#include <istream>   // get
#include <ostream>   // opeator<< for out_file
#include <string>    // getline\find\c.str
#include <cstring>   // strcpy\strcat
#include <vector>    // vector
#include <array>     // array
#include <algorithm> // max_element

using namespace std;
static int num = 0; // 待检测的任务数
void txt_data(istream &input_data_param, vector<string> &Virus, vector<string> &Person)
{
    // char c;
    string line;
    // while (input_data_param.get(c))  // ! 一个一个数据读取
    //     if (getline(input_data_param, line))
    //     {
    //         cout << c << line << endl;

    //     }
    while (getline(input_data_param, line)) // ! 一行一行数据读取
    {
        // cout << line << endl;
        // cout << "length: " << line.length() << endl;
        //  cout << "hello" << endl;

        // for (auto i : line)  // ! 单个字符的操作
        // {
        // if (i != '\t')
        //     cout << i;
        // else
        //     cout << " this is";

        // }
        num++;

        int start_pos = 0;
        int DNAEnd_pos = line.find('\t', start_pos);
        Virus.push_back(line.substr(start_pos, DNAEnd_pos));

        // cout << DNAEnd_pos << endl;

        // int PersonEnd_pos = line.find('\n', DNAEnd_pos);
        if (!input_data_param.eof())
            Person.push_back(line.substr(DNAEnd_pos + 1, line.length() - 2 - DNAEnd_pos));
        else
            Person.push_back(line.substr(DNAEnd_pos + 1, line.length() - 1 - DNAEnd_pos));
    }

    if (input_data_param.eof()) // * eof到达了文件的末尾
        cout << "数据读取完毕" << endl;
    else
        cout << "数据读取失败" << endl;
}

static int pos = 0;
// * 单链表的存储结构
class SList_str
{
public:
    string virus_name; // 数据域-存放病毒变种
    string flag_name;  // 数据域-存在检测结果
    SList_str *next;   // 指针域
public:
    // 后插法创建单链表
    SList_str *AddList_str(vector<string> virusDNA, vector<string> virusDNA_new, vector<string> PersonDNA)
    {
        SList_str *head = new SList_str, *tail;
        head->next = NULL;
        tail = head;

        for (int j = 0; j < virusDNA[pos].length(); j++) // * 根据VirusDNA_new链接新结点
        {
            SList_str *p = new SList_str;
            p->virus_name = (virusDNA_new[pos]).substr(j, virusDNA[pos].length());
            p->next = NULL;
            tail->next = p;
            tail = p;
        }

        SList_str *imd_ptr = head->next;
        for (int k = 0; k < virusDNA[pos].length(); k++) // 由病毒长度判断需要检测的次数
        {
            int DNA_pos = 0, Person_pos = 0;
            while (Person_pos < (PersonDNA[pos]).length() && DNA_pos < (virusDNA[pos]).length())
            {
                if ((PersonDNA[pos])[Person_pos] == imd_ptr->virus_name[DNA_pos])
                {
                    Person_pos++;
                    DNA_pos++;
                }
                else
                {
                    Person_pos = Person_pos - DNA_pos + 1;
                    DNA_pos = 0;
                }
            }

            if (DNA_pos >= (virusDNA[pos]).length())
                imd_ptr->flag_name = "YES";
            else
                imd_ptr->flag_name = "NO";
            imd_ptr = imd_ptr->next;
        }
        pos++;
        return head;
    }
};

static vector<string> flag(10, "NO");
void virus_detection(vector<string> &VirusDNA, vector<string> &PersonDNA)
{
    vector<string> VirusDNA_new1;
    for (int i = 0; i < num; i++) // 采用容器扩充至2倍,简单粗暴的方法
    {
        string VirusDNA_temp = VirusDNA[i]; // 病毒DNA的临时变量
        VirusDNA_temp.append(VirusDNA_temp);
        VirusDNA_new1.push_back(VirusDNA_temp);
    }

    // ! 测试输出结果是否有无
    cout << "-------病毒和抽查人群的DNA-------" << endl;
    cout << "Index:" << '\t' << "VirusDNA:" << '\t' << "PersonDNA:" << endl;
    for (int i = 0; i < num; i++)
    {
        cout << (i + 1) << '\t' << VirusDNA[i] << '\t' << '\t' << PersonDNA[i] << endl;
    }

    for (int i = 0; i < num; i++)
    {
        SList_str result_object;
        SList_str *head_list = result_object.AddList_str(VirusDNA, VirusDNA_new1, PersonDNA);

        SList_str *imd_ptr, *head_ptr = head_list;
        imd_ptr = head_ptr->next;
        for (int j = 0; j < VirusDNA[i].length(); j++)
        {
            if (imd_ptr->flag_name == "YES")
            {
                flag[i] = "YES";
                break;
            }
            else
                imd_ptr = imd_ptr->next;
        }
    }
}

int main()
{

    vector<string> VirusDNA;  // * 存放病毒DNA数据
    vector<string> PersonDNA; // * 存放感染人群的DNA数据

    ifstream input_data("E:\\Documents\\VSCode\\DataFilesSaved\\input_data.txt");
    ofstream out_file;
    out_file.open("E:\\Documents\\VSCode\\DataFilesSaved\\output_data.txt", ios_base::out);

    if (!input_data.is_open())
        cout << "file_open_fail" << endl;

    txt_data(input_data, VirusDNA, PersonDNA); // 调用文本文件处理函数

    cout << "样本容量大小为:" << num << endl;

    virus_detection(VirusDNA, PersonDNA);

    cout << "---------------------输出结果---------------------" << endl;
    cout << "Index:" << '\t' << "VirusDNA:" << '\t' << "PersonDNA:" << '\t' << "Result:" << endl;
    for (int i = 0; i < num; i++)
    {
        cout << (i + 1) << '\t' << VirusDNA[i] << '\t' << '\t'
             << PersonDNA[i] << '\t' << flag[i] << endl;
    }

    cout << "测试结果已保存至文件:E:\\Documents\\VSCode\\DataFilesSaved\\output_data.txt" << endl;
    // for (int i = 0; i < num; i++)
    //     out_file << VirusDNA[i] << '\t' << PersonDNA[i]
    //              << '\t' << flag[i] << endl; // * 逐行写入数据到文件中

    input_data.close();
    out_file.close();
}

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yinloonga

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值