zoukankan      html  css  js  c++  java
  • 数据结构第二版之(课后题)BF算法病毒感染检测

    //vs2013下编译通过.换别的编译器自行补充头文件和修改源代码
    #include<iostream> #include<fstream> #include <string> using namespace std; char temp[256];//存放的临时数组 char virus[20];//病毒数组 char DNA[20];//DNA的数组 int j = 0;//病毒长度 int DNAlength = 0;//dna长度 int flag = 0;//判断是不是感染开关 void toCharArry(string str)//将读入的字符串转化为字符数组 { j = 0; int tem=0; strcpy_s(temp, str.c_str()); //cout << str.length(); for (int i = 0; i < str.length(); i++)//找到空格的位置 { if (temp[i] == ' '){ j=(i+1); break; } } tem = j; for (int i = 0; i < str.length() - j + 1;i++)//写入DNA数组; { DNA[i] = temp[tem]; if (i == str.length() - j){ DNA[i] = '';//到底用不用加我也不知道 } tem++; } for (int i = 0; i < j; i++)//得到病毒序列 { virus[i] = temp[i]; } /*for (int i = 0; i <9; i++)//输出Dna序列 { cout <<DNA[i]; } cout << endl; for (int i = 0; i < j; i++)//输出病毒序列 { cout << virus[i]; } cout << endl; cout << j;*/ } void addvVirusToDouble(char virus[])//将病毒序列扩大为原来二倍 { int k = j-1; for (int i = 0; i < j-1 ; i++) { virus[k] = virus[i]; k++; } /*for (int i = 0; i < j*2; i++)//输出病毒序列 { cout << virus[i]; } cout << endl;*/ } int BF(int zhu, int fu, char virus[], char DNA[])//BF算法判断 { int i = 0, j = 0; while (i <= zhu&&j <= fu) { if (DNA[i]==virus[j]){ i++; j++; } else { i = i - j + 1; j = 0; } } if (j > fu){ return 1; } else return 0; } void virusPossibility()//列出将长度扩大二倍的病毒序列可能性 { char temp[30]; for (int i = 0; i < j; i++) { for (int k = 0; k < j; k++)temp[k] = virus[i + k]; //temp[j] = ''; if (BF(DNAlength - j - 1, j - 2, temp, DNA) == 1)flag = 1; } /*if (flag == 1) { flag = 0; cout << "O" << endl; } else cout << "X" << endl; */ } void detection()//调用函数判断是否感染 { int num = 0; string str; ifstream infile("病毒检测输入数据.txt", ios::in); ofstream outfile("病毒检测结果.txt", ios::out); if (!infile){ cout << "open document ERROE!!" << endl; exit(-1); } else cout << "The document reads into success!!" << endl; if (!outfile){ cout << "Establish document failure ERROE!!" << endl; exit(-1); } getline(infile, str); num = stof(str);//赋值给num之后判断; //cout << num; while (num--) { getline(infile, str); DNAlength = str.length(); toCharArry(str); addvVirusToDouble(virus); virusPossibility(); if (flag == 1){ flag = 0; outfile << str + " " + "YES" << endl; }//妈的这里必须把flag弄成0,要不只要有一个是下面的都是了... else { outfile << str + " " + "NO" << endl; } } infile.close(); outfile.close(); } int main() { string str; detection(); cout << "The TXTdocument outputs success!!!" << endl; }

    程序截图:

     

    老师让思考的作业,网上找了找也没有找到,作为小白,自己动手写了一下分享给大家,造福后来人,代码写的不够好......不要乱喷谢谢!!

  • 相关阅读:
    一种可以实时检测IP地址合法性的EditText输入框
    LVDS 屏幕 M215HGE-L21 在 rk3288 上的适配过程
    轻读一下 Android 应用开发中的 assets 目录
    XML与其在Android下的解析
    Linux Shell脚本实现根据进程名杀死进程
    RSA host key has changed 错误
    Linux下安装jdk8步骤详述
    Windows/Linux javac/java编译运行引入所需的jar包
    No cached version of ..... available for offline mode.
    Java学习之InputStream中read()与read(byte[] b)
  • 原文地址:https://www.cnblogs.com/xuexidememeda/p/7801391.html
Copyright © 2011-2022 走看看