Added day 6 (two programs. had to rewrite for the second problem). Added starter files.
This commit is contained in:
52
2022/6/main2.cpp
Normal file
52
2022/6/main2.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream ifs("data.txt");
|
||||
if(!ifs.is_open())
|
||||
{
|
||||
std::cerr << "Missing data.txt." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(std::string line; std::getline(ifs, line); )
|
||||
{
|
||||
if(line == "")
|
||||
continue;
|
||||
|
||||
std::vector<char> buf;
|
||||
|
||||
int idx = 0;
|
||||
for(const char &ch : line)
|
||||
{
|
||||
++idx;
|
||||
|
||||
// clear out from beginning up until this character is found:
|
||||
auto i = std::find(buf.begin(), buf.end(), ch);
|
||||
if(i != buf.end())
|
||||
buf.erase(buf.begin(), i+1);
|
||||
|
||||
buf.push_back(ch);
|
||||
|
||||
if(buf.size() == 4)
|
||||
{
|
||||
std::cout << "Start-of-Packet at index " << idx << ": ";
|
||||
for(const char &ch : buf)
|
||||
std::cout << ch;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
else if(buf.size() == 14)
|
||||
{
|
||||
std::cout << "Start-of-Message at index " << idx << ": ";
|
||||
for(const char &ch : buf)
|
||||
std::cout << ch;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user