gpt4 book ai didi

c++ - 如何读取文件并将其存储为矩阵(二维数组)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:06:12 27 4
gpt4 key购买 nike

我有一个图形着色程序,它接受一个二维数组作为邻接矩阵,并根据该邻接矩阵为图形着色。此文件是 Color.cpp。我有另一个程序可以创建这个邻接矩阵并将其放入一个文本文件中,这个程序 AdjacencyMatrix.cpp

当我最初创建着色程序时,我将邻接矩阵硬编码到二维数组 graph[V][V] 中,其中 V 是顶点数。

这是两个独立的程序,我只是将 AdjacencyMatrix.cpp 创建的邻接矩阵复制并粘贴到 Color.cpp 中。

因为我的程序已经创建了一个包含邻接矩阵的文件,所以我想让我的着色程序读取该文件。我知道最好的做法是将它们组合起来,并将存储在数组中的矩阵传递到一个内聚程序中的着色程序中。我认为目前读取文件是我最简单的解决方案,但如果更有经验的用户能告诉我如何将矩阵存储在数据结构中并将其传递到着色程序中,我欢迎它。

邻接矩阵.cpp

该程序接收一个文件,该文件是作为相邻对列出的整数字符串,并从中创建一个邻接矩阵,然后将其存储在一个文件中。文件“24cell_skeleton.txt”是它读入的文件,“out.txt”是它打印矩阵的文件。

#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;
const int cellSize = 24;
bool graph[cellSize][cellSize];
int prevNum = -1;
int currNum = -1;


bool markMatrix(int x, int y) {
graph[x][y] = true;
graph[y][x] = true;
/*std:cout << "adding pair: " << y << "," << x << "\n";*/
return true;

}

int main()
{

ifstream in("C:\\Users\\Austin\\Documents\\adjMatrix\\24cell_skeleton.txt");
ofstream out;
out.open("C:\\Users\\Austin\\Documents\\adjMatrix\\output.txt", ios::out);
ios::out;

string line, field;

vector< vector<string> > array; // the 2D array
vector<string> v; // array of values for one line only

while (getline(in, line)) // get next line in file
{
v.clear();
stringstream ss(line);

while (getline(ss, field, ',')) // break line into comma delimited fields
{
v.push_back(field); // add each field to the 1D array
}

array.push_back(v); // add the 1D array to the 2D array
}

// print out what was read in
bool firstIter = true;

for (size_t i = 0; i<array.size(); ++i)
{
for (size_t j = 0; j<array[i].size(); ++j)
{
string curr = array[i][j]; // (separate fields by |)
prevNum = currNum;
currNum = stoi(curr);

if (j != 0 && firstIter) { //if its not the first iteration . . . or the last?
/*out << "adding pair: " << prevNum << "," << currNum << "\n";*/
markMatrix(prevNum, currNum);
}
}
firstIter = false;

}

in.close();


//std::cout << "{";
for (int i = 1; i <= cellSize; ++i)
{
std::cout << "{";
out << "{";
for (int j = 1; j <= cellSize; ++j)
{
//std::cout << "{";
std::cout << graph[i][j] << ' ';
std::cout << ", ";
out << graph[i][j] << ' ';
out << ", ";
//std::cout << "}";
}
out << "},";
std::cout << "},";
std::cout << std::endl;
}

//std::cout << "}";

out.close();
//cout << graph[534][42];

system("pause");
return 0;

}

颜色.cpp

此文件为给定邻接矩阵的图形着色。正如我之前提到的,矩阵被硬编码到其中(从前一个程序创建的文件中复制并粘贴)。这是我想阅读文件而不是自己复制和粘贴的地方。

#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>
#include<stdio.h>
#include<cstdio>


// Number of vertices in the graph
#define V 24

using namespace std;

//this function was added in the update to attempt reading file and storing into a 2d matrix

int createMatrix(int myArray[V][V])
{
ifstream in("C:\\Users\\Austin\\Documents\\adjMatrix\\output.txt");

for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
in >> myArray[i][j];
}
}
return myArray[V][V];
}
//end of updated code added

void printSolution(int color[]);

/* A utility function to check if the current color assignment
is safe for vertex v */
bool isSafe(int v, bool graph[V][V], int color[], int c)
{
for (int i = 0; i < V; i++)
if (graph[v][i] && c == color[i])
return false;
return true;
}

/* A recursive utility function to solve m coloring problem */
bool graphColoringUtil(bool graph[V][V], int m, int color[], int v)
{
/* base case: If all vertices are assigned a color then
return true */
if (v == V)
return true;

/* Consider this vertex v and try different colors */
for (int c = 1; c <= m; c++)
{
/* Check if assignment of color c to v is fine*/
if (isSafe(v, graph, color, c))
{
color[v] = c;

/* recur to assign colors to rest of the vertices */
if (graphColoringUtil(graph, m, color, v + 1) == true)
return true;

/* If assigning color c doesn't lead to a solution
then remove it */
color[v] = 0;
}
}

/* If no color can be assigned to this vertex then return false */
return false;
}

bool graphColoring(bool graph[V][V], int m)
{
// Initialize all color values as 0. This initialization is needed
// correct functioning of isSafe()
int *color = new int[V];
for (int i = 0; i < V; i++)
color[i] = 0;

// Call graphColoringUtil() for vertex 0
if (graphColoringUtil(graph, m, color, 0) == false)
{
std::cout << "Solution does not exist";
return false;
}

// Print the solution
printSolution(color);
return true;
}

/* A utility function to print solution */
void printSolution(int color[])
{
std::cout << "Solution Exists:"
" Following are the assigned colors \n";
for (int i = 0; i < V; i++)
std::cout << color[i];
std::cout << "\n";
}

// driver program to test above function
int main()
{

/* Create following graph and test whether it is 3 colorable
(3)---(2)
| / |
| / |
| / |
(0)---(1)
*/
bool graph[V][V] = {
{ 0 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , },
{ 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , },
{ 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , },
{ 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , },
{ 1 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , },
{ 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 ,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 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 1 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
};
int m = 5; // Number of colors
graphColoring(graph, m);
//return 0;

system("pause");
}

有没有办法从文件中获取这个矩阵而不是将其硬编码到程序中?括号和逗号很容易在文件中添加或删除。当硬编码到着色程序中时,它们是必需的,但如果我可以从文件中读取,我认为它们不是必需的。

为了尽可能清楚,在 Color.cpp 中看到 24x24 矩阵的地方,我希望该部分是从 out.txt 中读取的 AdjacencyMatrix 矩阵。 cpp 创建。

编辑/更新

到目前为止,我在 color.cpp 的开头有这个

int createMatrix(int myArray[V][V]) 
{
ifstream in("C:\\Users\\Austin\\Documents\\adjMatrix\\output.txt");

for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
in >> myArray[i][j];
}
}
return myArray[V][V];
}

我知道这里有些问题,但如果我能让它工作,我计划使用类似的东西

bool graph[V][V] = 
{
myArray[V][V]
};

main() 中。

到目前为止,VisualStudio 说 myArray 是一个未声明的标识符,并且在 main() 中未定义。

最佳答案

您的 createMatrix 函数几乎已经完成,但是 return myArray[V][V]; 不是您想做的。这就是说返回数组边界之外的单个 int(大小为 V 的数组在 0 到 V-1 之间有效。Undefined Behaviour 破坏数组的边界将导致错误。

幸运的是,您不必退回该死的东西。 myArray 是指向调用 createMatrix 的数组的指针(参见 What is array decaying? )所以 in >> myArray[i][j]; 正在将文件直接读入数组。

因此进行一些检查以确保读取成功:

bool createMatrix(int myArray[V][V]) 
{
ifstream in("C:\\Users\\Austin\\Documents\\adjMatrix\\output.txt");
if (in) // if the file is open and readable
{
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (!(in >> myArray[i][j]))
{ // failed to read a value from the file. Bad file
// may want to log the error here so you know why it failed
return false;
}
}
}
}
else
{ // couldn't open the file
// may want to log the error here so you know why it failed
return false;
}
return true; // read everything we wanted to read. all done.
}

然后在 main

int main()
{
int graph[V][V];
if (createMatrix(graph))
{ // read the file successfully
int m = 5; // Number of colors
graphColoring(graph, m);
}
else
{ // did not read the file successfully
// display failure message
}
system("pause");
}

关于c++ - 如何读取文件并将其存储为矩阵(二维数组)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49908871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com