gpt4 book ai didi

c++ - 将二维数组传输到 csv 文件?

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:14 27 4
gpt4 key购买 nike

我有一个名为“temp_matrix”的矩阵,看起来有点像这样:

0.0,100.0,100.0,100.0,
0.0,45.1,60.6,66.2,0,
0.0,45.1,60.6,66.2,0,
0.0,100.0,100.0,100.0,0,

...除了我的是 20x20 矩阵。

我试过:

ofstream excel_plate("plate.csv");
excel_plate.write(temp_matrix[0],20);
excel_plate.close();
cout << endl;

但无法编译。

我试过:

ofstream excel_plate("plate.csv");
excel_plate << temp_matrix[0], 20, 20;
excel_plate.close();

...但只需获取一个 csv 文件,在第一个单元格中包含一串字符,如下所示:0052EE98。

这是我的代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <math.h>
#include <Windows.h>
#include <algorithm>
#include <string>
#include <array>
#undef max

using namespace std;

const double neighbors = 4;

// START FUNCTION: Update Elements
void average(double *temp_matrix, int ROWS, int COLUMNS)
{
int i = 0;
int j = 0;

double row_left = 0;
double row_right = 0;
double column_up = 100;
double column_down = 0;
double sum = 0;

double av = 0;

for (int i = 1; i < ROWS - 1; i++)
{
for (int j = 1; j < COLUMNS - 1; j++)
{
/*
cout << "row_left:" << *(temp_matrix + i*ROWS + (j - 1)) << " ";
cout << "row_right:" << *(temp_matrix + i*ROWS + (j + 1)) << " ";
cout << "column_up:" << *(temp_matrix + (i - 1)*ROWS + j) << " ";
cout << "column_down:" << *(temp_matrix + (i + 1)*ROWS + j) << " ";
*/

row_left = *(temp_matrix + i*ROWS + (j-1));
row_right = *(temp_matrix + i*ROWS + (j + 1));
column_up = *(temp_matrix + (i-1)*ROWS + j);
column_down = *(temp_matrix + (i+1)*ROWS + j);

sum = row_left + row_right + column_up + column_down;
av = sum / neighbors;

*(temp_matrix + i*ROWS + j) = av;
}
}
}
// END FUNCTION: Update Elements

// START FUNCTION: Updtate Until Stable
void update(double *temp_matrix, int ROWS, int COLUMNS)
{
int i = 0;
int j = 0;

double row_left = 0;
double row_right = 0;
double column_up = 100;
double column_down = 0;
double sum = 0;

double old_num = 0;
double new_num = 0;

double difference = 0;
double max_change = .11;

while (max_change > .1)
{
max_change = -1;
for (int i = 1; i < ROWS - 1; i++)
{
for (int j = 1; j < COLUMNS - 1; j++)
{
old_num = *(temp_matrix + i*ROWS + j);

/*
cout << "row_left:" << *(temp_matrix + i*ROWS + (j - 1)) << " ";
cout << "row_right:" << *(temp_matrix + i*ROWS + (j + 1)) << " ";
cout << "column_up:" << *(temp_matrix + (i - 1)*ROWS + j) << " ";
cout << "column_down:" << *(temp_matrix + (i + 1)*ROWS + j) << " ";
*/

row_left = *(temp_matrix + i*ROWS + (j - 1));
row_right = *(temp_matrix + i*ROWS + (j + 1));
column_up = *(temp_matrix + (i - 1)*ROWS + j);
column_down = *(temp_matrix + (i + 1)*ROWS + j);

sum = row_left + row_right + column_up + column_down;
new_num = sum / neighbors;

difference = new_num - old_num;
if (difference > max_change)
{
max_change = difference;
}

*(temp_matrix + i*ROWS + j) = new_num;
}
}
}
}
// END FUNCTION: Update Until Stable

// START FUNCTION: Print Array
void print_array(double *temp_matrix, int ROWS, int COLUMNS)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
if (j > 0)
{
cout << ",";
}
cout << fixed << setw(5) << setprecision(1) << *(temp_matrix + i*ROWS + j);
}
cout << endl;
}
}
// END FUNCTION: Print Array

// START FUNCTION: Print 4 Excel
void print_excel(double *temp_matrix, int ROWS, int COLUMNS)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
cout << fixed << setprecision(1) << *(temp_matrix + i*ROWS + j) << ",";
}
cout << endl;
}
}
// END FUNCTION: Print 4 Excel

int main()
{

const int ROWS = 20;
const int COLUMNS = 20;

double temp_matrix[ROWS][COLUMNS] =
{
{ 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0 },
{ 0, 0, 0,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0 }
};


print_array(temp_matrix[0], 20, 20);

system("pause");
cout << endl << endl;

average(temp_matrix[0], 20, 20);
print_array(temp_matrix[0], 20, 20);

system("pause");
cout << endl << endl;

update(temp_matrix[0], 20, 20);
print_array(temp_matrix[0], 20, 20);

system("pause");
cout << endl << endl;

print_excel(temp_matrix[0], 20, 20);

ofstream excel_plate("plate.csv");
if (excel_plate.is_open())
{
excel_plate << temp_matrix[0], 20, 20;
excel_plate.close();
}
else
{
cout << "Unable to open file.";
}

/*
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
if (j > 0)
{
cout << ",";
}
cout << temp_matrix[i][j];
}
cout << endl;
}
*/

system("pause");
return 0;
}

这应该非常非常简单。请帮助一个菜鸟。 :)

附言因为这是一项作业,您会注意到我打印了几个矩阵。最后打印出来的是矩阵需要转为.csv文件的格式。

最佳答案

最简单的方法如下(假设您可以使用 c++11)

std::ofstream out("test.csv");

for (auto& row : temp_matrix) {
for (auto col : row)
out << col <<',';
out << '\n';
}

关于c++ - 将二维数组传输到 csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26499307/

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