数据结构:新冠病毒检测

检测是采用字符串的查找即KMP算法
KMP算法的核心是求next[]数组
例如

数据结构:新冠病毒检测_第1张图片
所以代码如下

#include 
#include 
#include 
#define NAME "病毒"
#define PASS "12345"
typedef unsigned char String[256];
int GetLength(String s)
{
     
	int i;
	for(i = 1;s[i] != '\0';i ++);
	return i - 1;
}
void GetNext(String b,int next[])//和自己比 
{
     
	int k,j;
	k = 0;
	j = 1;
	next[j] = k;
	//printf("%d\n",next[j]);
	while(j < b[0])
	{
     
		if(k == 0 || b[k] == b[j])
		{
     
			k ++;
			j ++;
			next[j] = k;
			//printf("%d\n",next[j]);
			
		}
		else
		{
     
			k = next[k];
		}
	}
	//return 1;
}
int Index(String s,String b,int next[])
{
     
	int i,j;
	i = 1;
	j = 1;
	while(i <= s[0] && j <= b[0])
	{
     
		if(j == 0 || s[i] == b[j])
		{
     
			i ++;
			j ++;
		}
		else
		{
     
			j = next[j];
		}
	}
	if(j > b[0])
	{
     
		return 1;
	}
	else
	{
     
		return 0;
	}
}
//验证
int YanZhen(char *Name,char *Pass)
{
     
	int count = 0;
	if(strcmp(NAME,Name) == 0 && strcmp(PASS,Pass) == 0)
	{
     
		count = 1;
	}
	return  count;
 } 
int main()
{
     
	printf("***************<<<<<<上帝之泪>>>>>***************************\n");
	String s,b;
	int choice;//选择 
	char Name[30];
	char Pass[30];
	printf("请输入用户名:");
	scanf("%s",Name);
	printf("请输入密码:");
	scanf("%s",Pass);
	
	if(YanZhen(Name,Pass) == 1)
	{
     
		printf("#####################<<<<病毒检测中心>>>>######################\n");
		while(1)
		{
     
			
		
	   printf("1,检测中心\n");
	   printf("2,退出系统\n");
	   printf("请输入你的选择:");
	   scanf("%d",&choice);
	if(choice == 1)
	{
     
	
	int next[100];
	printf("请输入患者的DNA的序列:::");
	scanf("%s",&s[1]);
	s[0] = GetLength(s);
	printf("请输入病毒的基因序列:::");
	scanf("%s",&b[1]);
	b[0] = GetLength(b);
	GetNext(b,next);
	if(Index(s,b,next))
	{
     
			printf("新冠病毒:阳性\n");
		    printf("请立即去往医院就诊\n");
	}
	else
	{
     
	
		printf("新冠病毒:阴性\n");
		printf("没有感染新冠病毒\n");
	}
  } 
  else
  {
     
  	printf("欢迎下次使用\n");
  	exit(0);
  	
   } 
  }
}
  else
  {
     
  	printf("请核对你的密码和账号\n");
  }

}

你可能感兴趣的:(数据结构)