读取任意大小的任意阵列 [英] Reading arbitrary array of any size

查看:144
本文介绍了读取任意大小的任意阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code阅读包含两个5X5阵列中两个.txt文件时,工作正常。

 的#include<&iostream的GT;
    #包括LT&;串GT;
    #包括LT&;&的fstream GT;
    #包括LT&;&sstream GT;
    #包括LT&;&stdio.h中GT;
    #包括LT&;矢量>
    #包括LT&;&sstream GT;    使用命名空间std;    诠释的main()
    {
        串MYFILE,mysecondFile,MyString的;
        串DIR;
        串延伸;
        INT总= 0;        INT NUMBER_OF_LINES = 0;
        串线;        扩展=.TXT;
        DIR =H:\\\\ \\\\ YEAR2 \\\\ EE273 \\\\ EE273 \\\\ Week6;        COUT<< 输入文件的名称:\\ t的;
        CIN>> MYFILE;
        COUT<< 进入第二个文件的名称:\\ t的;
        CIN>> mysecondFile;        MYFILE = DIR + MYFILE +扩展名;
        mysecondFile = DIR + mysecondFile +扩展名;        ifstream的INFILE;
        ifstream的inFile2;    INT I = 5;
    INT J = 5;
    INT I2 = 5;
    INT J2 = 5;
    INT I3 = 5;
    INT J3 = 5;
    时int k;
    INT升;int数组[5] [5];
INT ARRAY2 [5] [5];
INT ARRAY3 [5] [5];
串attempt1,ATTEMPT2;
INT行= 0;
INT COL = 0;
INT ROW2 = 0;
INT COL2 = 0; // I =行
                    // Y =列inFile.open(myFile.c_str());
如果(!INFILE){
    COUT<<打开文件时出错<<&MYFILE LT;< ENDL;
    返回-1;
}而(!inFile.eof())
{
    函数getline(INFILE,attempt1);
    stringstream的ISS(attempt1);
    字符串结果;
    COL = 0;
    而(函数getline(ISS,结果,''))
    {
        // COUT<<结果<< ENDL;
        数组[行] [山口] =的atoi(result.c_str());
        // J = J + 1;
        COL = COL + 1;    }
    排排= + 1;
}
inFile.close();inFile2.open(mysecondFile.c_str());
如果(!inFile2){
    COUT<<打开文件时出错<< mysecondFile<< ENDL;
    返回-1;
}
而(!inFile2.eof())
{
    函数getline(inFile2,ATTEMPT2);
    stringstream的ISS(ATTEMPT2);
    串RESULT2;
    COL2 = 0;
    而(函数getline(ISS,结果2,','))
    {
        // COUT<<结果2<< ENDL;
        数组2 [ROW2] [COL2] =与atoi(result2.c_str());
        COL2 = COL2 + 1;
    }
    ROW2 = ROW2 + 1;
}
inFile2.close();/ *的for(int i = 0;我小于5;我++){
    为(中间体J = 0; J&小于5; J ++){
        COUT<<数组[i] [j]的<< ENDL;}}
为(中间体I2 = 0; I2小于5; I2 ++){
    为(中间体J2 = 0; J2小于5; J2 ++){
        COUT<< ARRAY2 [12] [J2<< ENDL;
    }}

在这里,我执行两个矩阵的乘法和写作所得的值到第三矩阵。

  INT合计= 0;
I = 0;
J2 = 0;
J = 0;
J3 = 0;
为(I3 = 0; i3的小于5; I3 ++){
    而(J3小于5){
            而(J小于5){
            为(I2 = 0; I2小于5; I2 ++){
            总+ =阵列[I] [J] * ARRAY2 [12] [J2];
            J ++;
            ARRAY3 [I3] [J3] =总;            }}
            J = 0;
            J2 ++;
            J3 ++;
            总= 0;
            }
    我++;
    J = 0;
    J2 = 0;
    J3 = 0;
    总= 0;
}

我的问题是:什么是修改code,以便它可以读取包含的任意大小的数组两位.txt文件最简单的方法,然后成功地开展了乘法<? / p>

修改的我只做到这一点使用数组,我不能不幸用向量。

我在想在运营商参与纠正?


解决方案

在最简单的方法是做一些幼稚,就像重新读入文件一旦完全得到行/ COLS数,然后读取文件其实值存储在矩阵:

  unsigned int类型行= 0;
unsigned int类型COLS = 0;性病::串线;
而(的std ::函数getline(INFILE,线)){
    行++;
    性病:: stringstream的SS(线);    标准::字符串关口;
    而(的std ::函数getline(SS,列,'')){
        COLS ++;
    }
}//现在分配行* COLS矩阵
INT **矩阵=新INT * [行]
的for(int i = 0; I&LT;行;我++){
    矩阵[i] =新的INT [COLS]
}//和阅读你的价值观到矩阵...
//矩阵[M] [N] = XXX

这是pretty效率低下读取文件两次;还有其它的方法来事先获得的大小。例如,你可以在你的输入文件中约定,包括矩阵宽/高数据前:

  [infile.txt]
3,3
1,2,3
4,5,6
7,8,9

现在,你可以读取该文件的第一行,你就会知道,这个文件的剩余部分包含了一个3x3矩阵。分配你的矩阵(类似于上面的例子),然后继续读取文件的剩余部分内容。

记住以清理你的动态分配与矩阵删除[] 。应该有1调用删除每次调用

 的for(int i = 0; I&LT;行;我++){
    删除[]矩阵[I]
}
删除[]矩阵;

The following code works fine when reading two .txt files containing two 5X5 array.

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

    using namespace std;

    int main()
    {
        string myFile, mysecondFile, mystring;
        string DIR;
        string extension;
        int total = 0;

        int number_of_lines = 0;
        string line;

        extension = ".txt";
        DIR = "H:\\Year2\\EE273\\EE273\\Week6\\";

        cout << "Enter the name of the file: \t";
        cin >> myFile;
        cout << "Enter the name of the second file: \t";
        cin >> mysecondFile;

        myFile = DIR + myFile + extension;
        mysecondFile = DIR + mysecondFile + extension;

        ifstream inFile;
        ifstream inFile2;

    int i=5;
    int j=5;
    int i2=5;
    int j2=5;
    int i3=5;
    int j3=5;
    int k;
    int l;

int Array[5][5];
int Array2[5][5];
int Array3[5][5];
string attempt1,attempt2;
int row = 0;
int col = 0;
int row2 = 0;
int col2 = 0;//i = row
                    //y = column

inFile.open(myFile.c_str());


if (!inFile) {
    cout <<"Error opening file"<<myFile<<endl;
    return -1;
}

while (!inFile.eof())
{
    getline(inFile, attempt1);
    stringstream iss( attempt1 );
    string result;
    col = 0;
    while (getline( iss, result, ','))
    {
        //cout << result << endl;
        Array[row][col] = atoi(result.c_str());
        //j = j + 1;
        col = col + 1;

    }
    row = row + 1;
}
inFile.close();

inFile2.open(mysecondFile.c_str());
if (!inFile2) {
    cout <<"Error opening file"<<mysecondFile<<endl;
    return -1;
}
while (!inFile2.eof())
{
    getline(inFile2, attempt2);
    stringstream iss( attempt2 );
    string result2;
    col2 = 0;
    while (getline( iss, result2, ','))
    {   
        //cout << result2 << endl;
        Array2[row2][col2] = atoi(result2.c_str());
        col2 = col2 + 1;
    }
    row2 = row2 + 1;
}
inFile2.close();

/*for (int i=0;i<5;i++){
    for (int j=0; j<5; j++){
        cout<<Array[i][j]<<endl;}}
for (int i2=0;i2<5;i2++){
    for (int j2=0; j2<5; j2++){
        cout<<Array2[i2][j2]<<endl;
    }}

Here I am carrying out the multiplication between the two matrices and writing the resulting values to a third matrix.

int Total=0;
i=0;
j2=0;
j=0;
j3=0;
for (i3=0; i3<5; i3++) {
    while(j3<5){
            while (j<5){
            for (i2=0;i2<5;i2++){
            Total += Array[i][j]*Array2[i2][j2];
            j++;
            Array3[i3][j3]=Total;

            }}
            j=0;
            j2++;
            j3++;
            Total=0;
            }
    i++;
    j=0;
    j2=0;
    j3=0;
    Total=0;
}

My question is: what is the easiest way to modify the code so that it can read two .txt files containing an array of any size and then carry out the multiplication successfully?

EDIT I have to do this using arrays only, I can't use vectors unfortunately.

Am I correct in thinking the new operator is involved?

解决方案

The "easiest" way would be to do something naive, like reading the file once fully to get the number of rows/cols, then reading the file again to actually store the values in the matrix:

unsigned int rows = 0;
unsigned int cols = 0;

std::string line;
while (std::getline(inFile, line)) {
    rows++;
    std::stringstream ss(line);

    std::string col;
    while (std::getline(ss, col, ',')) {
        cols++;
    }
}

// Now allocate the rows*cols matrix
int** matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
    matrix[i] = new int[cols];
}

// and read your values into the matrix ...
// matrix[m][n] = xxx

It's pretty inefficient to read a file twice; and there are other ways to obtain the size beforehand. For example you could have a convention in your input file to include the matrix width/height before the data:

[infile.txt]
3,3
1,2,3
4,5,6
7,8,9

Now you can read the first line of the file, and you'll know that the rest of this file contains a 3x3 matrix. Allocate your matrix with new (similar to above example), then continue to read the rest of the file into it.

Remember to clean up your dynamically allocated matrices with delete[]. There should be 1 call to delete for every call to new.

for (int i = 0; i < rows; i++) {
    delete[] matrix[i];
}
delete[] matrix;

这篇关于读取任意大小的任意阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆