Обновить transactions2/transactions2.cpp

This commit is contained in:
arrcprod 2025-05-16 08:20:36 +00:00
parent c7b5312b79
commit 56ca258454

View File

@ -34,7 +34,7 @@ vector<Block> blocks;
bool loadBlocksFromFile(const string& filename) { bool loadBlocksFromFile(const string& filename) {
ifstream input_file(filename); ifstream input_file(filename);
if (!input_file.is_open()) { if (!input_file.is_open()) {
cerr << "Error: Cannot open file " << filename << endl; //Ошибка: не удалось открыть файл cerr << "Error: Cannot open file " << filename << endl;
return false; return false;
} }
@ -64,12 +64,12 @@ bool loadBlocksFromFile(const string& filename) {
blocks.push_back(block); blocks.push_back(block);
} }
//Успешно загружено //блоков из файла
cout << "Successfully loaded " << blocks.size() << " blocks from " << filename << endl; cout << "Successfully loaded " << blocks.size() << " blocks from " << filename << endl;
return true; return true;
} }
catch (const exception& e) { catch (const exception& e) {
cerr << "JSON parsing error: " << e.what() << endl; //Ошибка при чтении JSON: cerr << "JSON parsing error: " << e.what() << endl;
return false; return false;
} }
} }
@ -100,12 +100,12 @@ bool saveBlocksToFile(const string& filename) {
ofstream output_file(filename); ofstream output_file(filename);
if (!output_file.is_open()) { if (!output_file.is_open()) {
cerr << "Error: Cannot create file " << filename << endl; //Ошибка: не удалось создать файл cerr << "Error: Cannot create file " << filename << endl;
return false; return false;
} }
output_file << setw(4) << j << endl; output_file << setw(4) << j << endl;
cout << "Successfully saved " << blocks.size() << " blocks to " << filename << endl; //Успешно сохранено //блоков в файл cout << "Successfully saved " << blocks.size() << " blocks to " << filename << endl;
return true; return true;
} }
@ -127,7 +127,7 @@ void printBlock(const Block& block) {
void printAllBlocks() { void printAllBlocks() {
if (blocks.empty()) { if (blocks.empty()) {
cout << "There is no data about the blocks.Download them first." << endl; //Нет данных о блоках. Загрузите их сначала. cout << "There is no data about the blocks.Download them first." << endl;
return; return;
} }
@ -138,12 +138,12 @@ void printAllBlocks() {
void filterByMiner() { void filterByMiner() {
if (blocks.empty()) { if (blocks.empty()) {
cout << "There is no data about the blocks. Download them first." << endl; //Нет данных о блоках. Загрузите их сначала. cout << "There is no data about the blocks. Download them first." << endl;
return; return;
} }
string minerName; string minerName;
cout << "Enter the miner's name: "; //Введите имя майнера: cout << "Enter the miner's name: ";
cin >> minerName; cin >> minerName;
bool found = false; bool found = false;
@ -155,17 +155,17 @@ void filterByMiner() {
} }
if (!found) { if (!found) {
cout << "Blocks with a miner " << minerName << " not found." << endl; //Блоки с майнером ... не найдены cout << "Blocks with a miner " << minerName << " not found." << endl;
} }
} }
void printTransactionsCount() { void printTransactionsCount() {
if (blocks.empty()) { if (blocks.empty()) {
cout << "There is no data about the blocks. Download them first." << endl; //Нет данных о блоках. Загрузите их сначала. cout << "There is no data about the blocks. Download them first." << endl;
return; return;
} }
cout << "Number of transactions in blocks: " << endl; //Количество транзакций в блоках: cout << "Number of transactions in blocks: " << endl;
for (const auto& block : blocks) { for (const auto& block : blocks) {
cout << "block " << block.index << ": " cout << "block " << block.index << ": "
<< block.transactions.size() << " transactions" << endl; << block.transactions.size() << " transactions" << endl;
@ -174,7 +174,7 @@ void printTransactionsCount() {
void calculateBalances() { void calculateBalances() {
if (blocks.empty()) { if (blocks.empty()) {
cout << "There is no data about the blocks. Download them first." << endl; //Нет данных о блоках. Загрузите их сначала. cout << "There is no data about the blocks. Download them first." << endl;
return; return;
} }
@ -189,7 +189,7 @@ void calculateBalances() {
} }
} }
cout << "User balances:" << endl; //Балансы пользователей: cout << "User balances:" << endl;
for (const auto& entry : balances) { for (const auto& entry : balances) {
cout << entry.first << ": " << fixed << setprecision(2) << entry.second << endl; cout << entry.first << ": " << fixed << setprecision(2) << entry.second << endl;
} }
@ -208,7 +208,7 @@ void sortBlocksByIndex() {
} }
} }
} }
cout << "The blocks are sorted by index." << endl; //Блоки отсортированы по индексу. cout << "The blocks are sorted by index." << endl;
} }
void printEvenIndexBlocks() { void printEvenIndexBlocks() {
@ -217,7 +217,7 @@ void printEvenIndexBlocks() {
return; return;
} }
cout << "Blocks with an even index:" << endl; //Блоки с четным индексом: cout << "Blocks with an even index:" << endl;
for (const auto& block : blocks) { for (const auto& block : blocks) {
if (block.index % 2 == 0) { if (block.index % 2 == 0) {
printBlock(block); printBlock(block);
@ -232,7 +232,7 @@ void printVowelMinerBlocks() {
} }
const string vowels = "AEIOUaeiou"; const string vowels = "AEIOUaeiou";
cout << "Blocks where the miner starts with a vowel:" << endl; //Блоки, где майнер начинается с гласной: cout << "Blocks where the miner starts with a vowel:" << endl;
for (const auto& block : blocks) { for (const auto& block : blocks) {
if (!block.miner.empty() && vowels.find(block.miner[0]) != string::npos) { if (!block.miner.empty() && vowels.find(block.miner[0]) != string::npos) {
@ -247,7 +247,7 @@ void printFourDigitNonceBlocks() {
return; return;
} }
cout << "Blocks with a 4-digit nonce (1000-9999):" << endl; //Блоки с 4-значным nonce (1000-9999): cout << "Blocks with a 4-digit nonce (1000-9999):" << endl;
for (const auto& block : blocks) { for (const auto& block : blocks) {
if (block.nonce >= 1000 && block.nonce <= 9999) { if (block.nonce >= 1000 && block.nonce <= 9999) {
printBlock(block); printBlock(block);
@ -256,19 +256,19 @@ void printFourDigitNonceBlocks() {
} }
void showMenu() { void showMenu() {
cout << "\n=== Menu ===" << endl //Загрузить блоки из файла cout << "\n=== Menu ===" << endl
<< "1. Load blocks from file" << endl //Загрузить блоки из файла << "1. Load blocks from file" << endl
<< "2. Save blocks to file" << endl //Сохранить блоки в файл << "2. Save blocks to file" << endl
<< "3. Print all blocks" << endl //Вывести все блоки << "3. Print all blocks" << endl
<< "4. Filter by miner" << endl //Фильтр по майнеру << "4. Filter by miner" << endl
<< "5. Transaction count" << endl //Количество транзакций << "5. Transaction count" << endl
<< "6. User balances" << endl //Балансы пользователей << "6. User balances" << endl
<< "7. Sort by index" << endl //Сортировать по индексу << "7. Sort by index" << endl
<< "8. Blocks with even index" << endl //Блоки с четным индексом << "8. Blocks with even index" << endl
<< "9. Blocks where miner starts with a vowel" << endl //Блоки, где майнер начинается с гласной << "9. Blocks where miner starts with a vowel" << endl
<< "10. Blocks with 4-digit nonce" << endl //Блоки с 4-значным nonce << "10. Blocks with 4-digit nonce" << endl
<< "0. Exit" << endl //Выход << "0. Exit" << endl
<< "Enter choice: "; //Введите номер << "Enter choice: ";
} }
int main() { int main() {
@ -280,16 +280,16 @@ int main() {
switch (choice) { switch (choice) {
case 1: { case 1: {
string filename; string filename;
cout << "Enter the file name to download: "; //Введите имя файла для загрузки: cout << "Enter the file name to download: ";
cin >> filename; cin >> filename;
if (!loadBlocksFromFile(filename)) { if (!loadBlocksFromFile(filename)) {
cout << "File upload error!" << endl; //Ошибка загрузки файла! cout << "File upload error!" << endl;
} }
break; break;
} }
case 2: { case 2: {
string filename; string filename;
cout << "Enter the file name to save: "; //Введите имя файла для сохранения: cout << "Enter the file name to save: ";
cin >> filename; cin >> filename;
if (!saveBlocksToFile(filename)) { if (!saveBlocksToFile(filename)) {
cout << "File upload error!" << endl; cout << "File upload error!" << endl;
@ -324,7 +324,7 @@ int main() {
cout << "Exit the program..." << endl; cout << "Exit the program..." << endl;
break; break;
default: default:
cout << "Wrong choice! Try again." << endl; //Неверный выбор! Попробуйте снова. cout << "Wrong choice! Try again." << endl;
} }
} while (choice != 0); } while (choice != 0);