-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.cpp
More file actions
119 lines (113 loc) · 2.1 KB
/
Copy pathSequence.cpp
File metadata and controls
119 lines (113 loc) · 2.1 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "Sequence.h"
#include <vector>
#include <cstdio>
#include <cstdlib>
#include "string.h"
#define MAXN 1200000
int pstrcmp(const void *a,const void *b)
{
return strcmp((char*)*(int*)a,(char*)*(int*)b);
}
int comlen(char *p,char *q)
{
int i = 0;
while(*p && (*p++ == *q++))
i++;
return i;
}
int Sequence::length()
{
f.open(file.data());
int length=0;
char dna;
while( f >> dna )
{
length++;
}
cout <<"The length of dna is : " << length <<endl;
f.close();
return length;
}
int Sequence::numberOf(char base)
{
f.open(file.data());
if((base!='A')&&(base!='C')&&(base!='T')&&(base!='G'))
cout << "The input is error." << endl;
int number=0;
char dna;
char b = base;
while( f >> dna )
{
if(b == dna)
number++;
}
cout << "The dna has " << number << " " << base <<endl;
f.close();
return 0;
}
string Sequence::longestConsecutive()
{
f.open(file.data());
string tmp;
getline(f,sequence);
while(getline(f,tmp))
{
sequence+=tmp;
}
char lon = sequence[0];
char longest = sequence[0];
int lon1 = 1;
int lon2 = 1;
for(int i=1;i<sequence.length();i++)
{
if(lon == sequence[i])
lon1++;
else
{
if(lon1>lon2)
{
lon2 = lon1;
longest = lon;
}
lon = sequence[i];
lon1 = 1;
}
}
cout << "The longest sequence is " << lon2 << longest << endl;
for( ;lon2>0;lon2--)
{
cout << longest;
}
cout << endl;
f.close();
return sequence;
}
string Sequence::longestRepeated()
{
f.open(file.data());
char *a[MAXN];
char ch;
char c[MAXN];
int i,temp;
int n=0;
int maxlen = 0,maxi =0;
while(f >> ch)
{
a[n]=&c[n];
c[n++]=ch;
}//建立后置数组
c[n]='\0';
qsort( a, n, sizeof(char*),pstrcmp);
for(i = 0; i<n-1; ++i)
{
temp = comlen( a[i],a[i+1]);
if( temp>maxlen )
{
maxlen = temp;
maxi = i;
}
}
cout << maxlen << a[maxi] << endl;
f.close();
return 0;
}