Чтение данных
This commit is contained in:
2
bitcoin-project/.gitignore
vendored
Normal file
2
bitcoin-project/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
data
|
||||||
|
build
|
||||||
26
bitcoin-project/Makefile
Normal file
26
bitcoin-project/Makefile
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
CXX = g++
|
||||||
|
CXXFLAGS = -std=c++17 -O2 -Wall -Wextra
|
||||||
|
|
||||||
|
SRC_DIR = src
|
||||||
|
BUILD_DIR = build
|
||||||
|
|
||||||
|
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
|
||||||
|
OBJS = $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))
|
||||||
|
|
||||||
|
TARGET = $(BUILD_DIR)/bitcoin_app
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(BUILD_DIR):
|
||||||
|
mkdir -p $(BUILD_DIR)
|
||||||
|
|
||||||
|
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp | $(BUILD_DIR)
|
||||||
|
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(TARGET): $(OBJS)
|
||||||
|
$(CXX) $(CXXFLAGS) $^ -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
47
bitcoin-project/src/csv_loader.cpp
Normal file
47
bitcoin-project/src/csv_loader.cpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "csv_loader.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
std::vector<Record> load_csv(const std::string& filename) {
|
||||||
|
std::vector<Record> data;
|
||||||
|
std::ifstream file(filename);
|
||||||
|
|
||||||
|
if (!file.is_open()) {
|
||||||
|
throw std::runtime_error("Cannot open file: " + filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
// читаем первую строку (заголовок)
|
||||||
|
std::getline(file, line);
|
||||||
|
|
||||||
|
while (std::getline(file, line)) {
|
||||||
|
std::stringstream ss(line);
|
||||||
|
std::string item;
|
||||||
|
|
||||||
|
Record row;
|
||||||
|
|
||||||
|
std::getline(ss, item, ',');
|
||||||
|
row.timestamp = std::stod(item);
|
||||||
|
|
||||||
|
std::getline(ss, item, ',');
|
||||||
|
row.open = std::stod(item);
|
||||||
|
|
||||||
|
std::getline(ss, item, ',');
|
||||||
|
row.high = std::stod(item);
|
||||||
|
|
||||||
|
std::getline(ss, item, ',');
|
||||||
|
row.low = std::stod(item);
|
||||||
|
|
||||||
|
std::getline(ss, item, ',');
|
||||||
|
row.close = std::stod(item);
|
||||||
|
|
||||||
|
std::getline(ss, item, ',');
|
||||||
|
row.volume = std::stod(item);
|
||||||
|
|
||||||
|
data.push_back(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
6
bitcoin-project/src/csv_loader.hpp
Normal file
6
bitcoin-project/src/csv_loader.hpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "record.hpp"
|
||||||
|
|
||||||
|
std::vector<Record> load_csv(const std::string& filename);
|
||||||
17
bitcoin-project/src/main.cpp
Normal file
17
bitcoin-project/src/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "csv_loader.hpp"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
auto records = load_csv("data/data.csv");
|
||||||
|
|
||||||
|
std::cout << "Loaded rows: " << records.size() << "\n";
|
||||||
|
|
||||||
|
for (int i = 0; i < 5 && i < records.size(); i++) {
|
||||||
|
std::cout << records[i].timestamp << " "
|
||||||
|
<< records[i].open << " "
|
||||||
|
<< records[i].high << " "
|
||||||
|
<< records[i].low << " "
|
||||||
|
<< records[i].close << " "
|
||||||
|
<< records[i].volume << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
11
bitcoin-project/src/record.hpp
Normal file
11
bitcoin-project/src/record.hpp
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
struct Record {
|
||||||
|
double timestamp;
|
||||||
|
double open;
|
||||||
|
double high;
|
||||||
|
double low;
|
||||||
|
double close;
|
||||||
|
double volume;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user