-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_version.cpp
96 lines (70 loc) · 1.79 KB
/
my_version.cpp
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
#include <iostream>
#include <string>
#include <fstream>
#include <bits/stdc++.h>
#include<cstring>
#include<cstdio>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <iomanip>
using namespace std;
double PCFreq = 0.0;
__int64 CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
cout << "QueryPerformanceFrequency failed!\n";
PCFreq = double(li.QuadPart)/1000000.0;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return double(li.QuadPart-CounterStart)/PCFreq;
}
bool check(int index, string pattern, string lineToCheck){
bool answer=false;
for(int x=0;x<pattern.size();x++){
if(lineToCheck[index + x] == pattern[x]){
answer=true;
}else{
answer=false;
break;
}
}
if(answer){
cout<<"Pattern found at the index : "<< index << endl;
}
return answer;
}
void patternSearch(string lineToCheck, string pattern){
int lineLength=lineToCheck.size();
int patternLength=pattern.size();
int index=0;
while(index != lineLength-patternLength-1){
if(check(index, pattern , lineToCheck)){
index+=pattern.size();
}else{index++;}
}
}
int main(){
ifstream myFile("data.txt");
string pattern="KARL";
string line;
int index=0; // will use this var to determine areas which was checked/not checked
if(myFile.is_open()){
while(getline(myFile,line)){
if(line.size() >= pattern.size()){
patternSearch(line, pattern);
}
}
}else{
cout<<"ERROR : while opening 'data.txt' "<<endl;
}
return 0;
}