任务是这样子的:我们先完成txt文本矩阵的准备,大概做了50个矩阵;
代码如下:
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <random>
- #include <string>
- #include <windows.h> // 包含 Windows API 头文件
- // 创建文件夹(仅适用于 Windows)
- bool createDirectory(const std::string& folderName) {
- BOOL result = CreateDirectory(folderName.c_str(), NULL);
- return result != 0;
- }
- // 生成一个随机矩阵
- std::vector<std::vector<int>> generateMatrix(int rows, int cols) {
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_int_distribution<int> dis(1, 10);
- std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols, 0));
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- matrix[i][j] = dis(gen);
- }
- }
- return matrix;
- }
- // 保存矩阵到文本文件
- void saveMatrixToFile(const std::vector<std::vector<int>>& matrix, const std::string& filename) {
- std::ofstream file(filename);
- if (file.is_open()) {
- for (const auto& row : matrix) {
- for (const auto& element : row) {
- file << element << " ";
- }
- file << "\n";
- }
- file.close();
- std::cout << "Matrix saved to file: " << filename << std::endl;
- } else {
- std::cerr << "Failed to open file: " << filename << std::endl;
- }
- }
- int main() {
- // 文件夹名称
- std::string folderName = "50_matrices";
- // 创建文件夹
- bool success = createDirectory(folderName);
- if (success) {
- std::cout << "Folder created: " << folderName << std::endl;
- } else {
- std::cerr << "Failed to create folder: " << folderName << std::endl;
- return 1;
- }
- // fs::create_directory(folderName);
- //std::filesystem::create_directory(folderName);
- int numMatrices = 50;
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_int_distribution<int> dis(1, 20); // 设置矩阵的行列数范围
- std::vector<std::vector<int>> prevMatrix;
- for (int i = 1; i <= numMatrices; ++i) {
- // 确定文件名和路径
- std::string filename = folderName + "/M" + std::to_string(i) + ".txt";
- int rows, cols;
- if (prevMatrix.empty()) {
- // 第一个矩阵可以是任意维度
- rows = dis(gen);
- cols = dis(gen);
- } else {
- // 后续矩阵的行数等于前一个矩阵的列数
- rows = prevMatrix[0].size();
- cols = dis(gen);
- }
- std::vector<std::vector<int>> matrix = generateMatrix(rows, cols);
- saveMatrixToFile(matrix, filename);
- prevMatrix = matrix;
- }
- return 0;
- }
这样就会生成一个矩阵;
当然,另一个是后来看到格式:第一行为 矩阵行数row 矩阵列数col
#include <iostream>
#include <fstream>
#include <vector>
#include <random>
#include <string>
#include <windows.h> // 包含 Windows API 头文件
// 创建文件夹(仅适用于 Windows)
bool createDirectory(const std::string& folderName) {
BOOL result = CreateDirectory(folderName.c_str(), NULL);
return result != 0;
}
// 生成一个随机矩阵
std::vector<std::vector<int>> generateMatrix(int rows, int cols) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(1, 10);
std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols, 0));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
matrix[i][j] = dis(gen);
}
}
return matrix;
}
// 保存矩阵到文本文件
void saveMatrixToFile(const std::vector<std::vector<int>>& matrix, const std::string& filename) {
std::ofstream file(filename);
if (file.is_open()) {
// 写入矩阵行数和列数
file << "矩阵行数" << matrix.size() << " " << "矩阵列数" << matrix[0].size() << std::endl;
//file << "矩阵的列数col: " << cols << "\n";
//file << matrix.size() << " " << matrix[0].size() << "\n";
// 写入矩阵数据
for (const auto& row : matrix) {
for (const auto& element : row) {
file << element << " ";
}
file << "\n";
}
file.close();
std::cout << "Matrix saved to file: " << filename << std::endl;
} else {
std::cerr << "Failed to open file: " << filename << std::endl;
}
}
int main() {
// 文件夹名称
std::string folderName = "50_matrices";
// 创建文件夹
bool success = createDirectory(folderName);
if (success) {
std::cout << "Folder created: " << folderName << std::endl;
} else {
std::cerr << "Failed to create folder: " << folderName << std::endl;
return 1;
}
// fs::create_directory(folderName);
//std::filesystem::create_directory(folderName);
int numMatrices = 50;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(1, 20); // 设置矩阵的行列数范围
std::vector<std::vector<int>> prevMatrix;
for (int i = 1; i <= numMatrices; ++i) {
// 确定文件名和路径
std::string filename = folderName + "/M" + std::to_string(i) + ".txt";
int rows, cols;
if (prevMatrix.empty()) {
// 第一个矩阵可以是任意维度
rows = dis(gen);
cols = dis(gen);
} else {
// 后续矩阵的行数等于前一个矩阵的列数
rows = prevMatrix[0].size();
cols = dis(gen);
}
std::vector<std::vector<int>> matrix = generateMatrix(rows, cols);
saveMatrixToFile(matrix, filename);
prevMatrix = matrix;
}
return 0;
}
OK,剩下的矩阵连乘再接着写喽;
标签:连乘,std,matrix,int,矩阵,cols,生成,folderName From: https://www.cnblogs.com/yq15mm/p/17753476.html