数据结构第三次上机代码


报告
# 病毒检测

反正就是和比人不一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<Windows.h>
using namespace std;
//堆串
typedef struct
{
char arr[600];
int length=0;
}HString;
//BF算法
int index_BF(HString HS/*人的DNA*/, HString T/*病毒的DNA*/, int pos)
{
int i = pos;
int j = 1;
if (!(i >= 1 && i <= HS.length))return 0;
while (i <= HS.length&&j <= T.length)
{
if (T.arr[j] == HS.arr[i])
{
i++; j++;
}
else
{
i = i - j + 2; j = 1;
}
}
return j > T.length ? i - T.length : 0;
}
//检测函数
void Virus_Detection()
{
int num/*任务数*/, flag/*状态标志*/;
int f = 0,t = 0;//个数
HString *HS = new HString;
HString *T = new HString;
ifstream inFile;//对象
ofstream outFile;
inFile.open("待检测.txt");
outFile.open("已检测.txt");
inFile >> num;
int temp_num = num;
HString* temp= new HString;
while (num--)
{
inFile >> (*T).arr + 1;
(*T).length = strlen(T->arr + 1);
inFile >> (*HS).arr + 1;
(*HS).length = strlen(HS->arr + 1);
int Len=(*temp).length = (*T).length;
for (int i = 1; i <= Len; i++)//病毒环状要比多次
{
for (int k = 1, j=i; k<= Len; k++,j++)//从环状病毒取病毒
{
(*temp).arr[k] = (*T).arr[(j-1)% Len +1];
}
(*temp).arr[Len + 1] = '\0';//不加的话,文件里会出现未初始化而显示的屯
flag = index_BF(*HS, *temp, 1);
if (flag)
{
break;
}
}
if (flag)
{
t++;
outFile << (*T).arr + 1 << " " << (*HS).arr + 1 << " " << "是" <<" "<<"携带病毒:"<< (*temp).arr+1<<" 位置:"<<flag<<endl;
}
else
{
f++;
outFile << (*T).arr + 1 << " " << (*HS).arr + 1 << " " << "否" << endl;
}
}
outFile << "共"<< temp_num <<"人,其中感染" << t << "人,未感染" <<f <<"人" << endl;
inFile.close();
outFile.close();
}
int main(void)
{
Virus_Detection();
printf("已进行检测\n请输入要调用的功能编号 可使用ctrl+c结束或其它"
"\n1 记事本打开已检测.txt\n"
"2 记事本打开待检测.txt\n"
"3 cmd输出已检测.txt\n"
"4 cmd输出待检测.txt\n"
"5 打开当前文件夹\n");
char ch = NULL;
srand(0);
while (true)
{
cin >> ch;
switch (ch)
{
case '1':system("已检测.txt"); break;
case '2':system("待检测.txt"); break;
case '3':system("type 已检测.txt"); break;
case '4':system("type 待检测.txt"); break;
case '5':rand()%2==0? system("explorer ."): system("start ."); break;
default:system("pause");return 0;break;
}
}
}

二叉树

功能不够

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define ERROR -1
typedef char DataType;
typedef struct TreeNode
{
DataType elem;
struct TreeNode* rchild;
struct TreeNode* lchild;
}TreeNode, *PTREE;
//先序创建
void CreatTree(PTREE *Root) {
char val = 0;//用于下面存放数据
val = getchar();//输入数据值
//如果输入'*',则指向为空
if (val == '*'|| val == '#' || val == '.')
(*Root) = NULL;
//如果输入非'*',则给数据域赋值
else {
(*Root) = (PTREE)malloc(sizeof(TreeNode));//申请内存空间
if ((*Root) == NULL) {
printf("创建结点失败,无法分配可用内存...");
exit(-1);
}
else {
(*Root)->elem = val;//给结点数据域赋值
CreatTree(&(*Root)->lchild);
CreatTree(&(*Root)->rchild);
}
}
}
//前序遍历
void PreOrderTree(PTREE Root) {

if (Root == NULL)
return;
else {
putchar(Root->elem);
PreOrderTree(Root->lchild);
PreOrderTree(Root->rchild);
}
}
//结点个数01*49**8**25**6** ABD***CE**FG*** 01*49**8**25**6*37***
int GetNodeNum(PTREE Tree)
{
if (NULL == Tree)
{
return 0;
}
else
{
return GetNodeNum(Tree->lchild) + GetNodeNum(Tree->rchild) + 1;
}
}
//叶子结点个数
int GetLeafNum(PTREE Tree)
{
if (Tree == NULL)
return 0;
if (Tree->lchild == NULL&&Tree->rchild == NULL)
return 1;
return GetLeafNum(Tree->lchild) + GetLeafNum(Tree->rchild);
}
//度为1结点个数
int GetNodde1Num(PTREE Tree)
{
if (Tree == NULL)

return 0;

if (Tree->lchild == NULL&&Tree->rchild != NULL || Tree->rchild == NULL&&Tree->lchild != NULL)

return 1 + GetNodde1Num(Tree->lchild) + GetNodde1Num(Tree->rchild);
return GetNodde1Num(Tree->lchild) + GetNodde1Num(Tree->rchild);
}
//度为2结点个数
int GetNodde2Num(PTREE Tree)
{
if (Tree == NULL)
return 0;
if (Tree->lchild != NULL&&Tree->rchild != NULL)
return 1+ GetNodde2Num(Tree->lchild) + GetNodde2Num(Tree->rchild);
return GetNodde2Num(Tree->lchild) + GetNodde2Num(Tree->rchild);
}
int main(void)
{
PTREE root;
printf("请输入前序 *或#或.为空\t");
CreatTree(&root);
printf("前序遍历 :\t");
PreOrderTree(root);
printf("\n");
printf("递归求所有结点 %d\n", GetNodeNum(root));
printf("等式求所有结点 %d\n", GetLeafNum(root)+ GetNodde2Num(root) + GetNodde1Num(root));
printf("递归求所有叶子结点 %d\n", GetLeafNum(root));
printf("性质求所有叶子结点 %d\n", GetNodde2Num(root)+1);
printf("递归求所有度为1的结点 %d\n", GetNodde1Num(root));
printf("等式求所有度为1的结点 %d\n", GetNodeNum(root)- GetLeafNum(root)- GetNodde2Num(root));
printf("递归求所有度为2的结点 %d\n", GetNodde2Num(root));
printf("性质求所有度为2的结点 %d\n", GetLeafNum(root)-1);
printf("总结: 根据书中的性质3和一个等式,只要有两个数据(度2和度0算一个),其他数据都可以算出\n");
system("pause");
return 0;
}