从二维数组和字符串流数据显示 [英] Displaying data from 2d array and stringstream

查看:152
本文介绍了从二维数组和字符串流数据显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个程序,它从一个看起来像文件读取数据:

 W¯¯žW¯¯ŸŸW¯¯W WŸW¯¯arrow-W¯¯旋转Z Y
žW WW¯¯ŸŸW¯¯W¯¯旋转Z YW¯¯žW¯¯Y Y
žW¯¯Z ZŸZ ZW¯¯W W Y Y YžW¯¯
žW¯¯Z ZŸZ ZW¯¯W W Y Y YžW¯¯
žW¯¯Z ZŸZ ZW¯¯W W Y Y YžW¯¯

这些字符(W,Y或Z)使用解析的字符串流并存储在一个二维数组这是5x15。现在,我有麻烦的每行计算的次数,每个字符 显示金额。我试着在我的函数中使用计数器变量实现这一目标的多种方法,但我至今没有得到它。仅供参考,这个想法是再创建一个报告,以显示所有这些数据的每一行,然后总。

我怎样才能做到这一点呢? (我只能用这种分配阵列(无载体,结构或类))。谢谢你们。

 的#include<&iostream的GT;
#包括LT&;&sstream GT;
#包括LT&;&的fstream GT;使用命名空间std;无效FROMFILE(ifstream的&安培;,焦项目[] [15],const int的,const int的,字符);诠释的main()
{
    在ifstream的;
    in.open(file.txt的);    const int的A_ROWS = 5,A_COLUMNS = 15;    焦炭项目[A_ROWS] [A_COLUMNS];
    焦炭itemDetails;    FROMFILE(中,物品,A_ROWS,A_COLUMNS,itemDetails);    附寄();
    返回0;
}无效FROMFILE(ifstream的&安培;中,焦炭项目[] [15],const int的A_ROWS,const int的A_COLUMNS,焦炭itemDetails)
{
    对于(INT行= 0;行和LT; A_ROWS;行++)
    {
        对于(INT列= 0;&列LT; A_COLUMNS;列++)
        {
            字符串海图=;
            如果(函数getline(在,海图))
            {
                stringstream的STR(海图);
                而(STR>> itemDetails)
                {
                    项目[行] [列] = itemDetails;
                    //测试显示从文件中的所有数据正常
                    // COUT<<项目[行] [列]<< ;
                }
                COUT<< ENDL;
            }
        }
    }
}


解决方案

我会用一个简单数组来解释所有的字母。
字母表有26个字母(A-Z),所以我将创建26个长度,其中重$ P $每个位置psents一个字母的数组。
然后,您可以通过从每个字母减去'A'在数组中查找每个字母的位置。它的工作pretty很像一张地图。

下面是一个片段:

  INT字母[26] = {0};
字母['Z' - '一'] + = 1; //字母[25],或第26元件。

I'm writing a program that reads data from a file that looks like:

W Z W Y W Y W W Y W Y Z W Z Y
Z W W W Y W Y W Z Y W Z W Y Y  
Z W Z Z Y Z Z W W W Y Y Y Z W
Z W Z Z Y Z Z W W W Y Y Y Z W 
Z W Z Z Y Z Z W W W Y Y Y Z W 

These characters (W, Y, or Z) are parsed using a stringstream and stored in a 2D array that's 5x15. Now I'm having trouble counting the amount of times that each character appears on each line. I've tried multiple ways of achieving this by using counter variables in my function but I haven't gotten it so far. FYI, the idea is to then create a report to display this data for each line and then a total for all.

How can I achieve this then? (I can only use arrays for this assignment (no vectors, structures, or classes)). Thanks guys.

#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

void fromFile(ifstream &, char items[][15], const int, const int, char);

int main() 
{
    ifstream in;
    in.open("file.txt");

    const int A_ROWS = 5, A_COLUMNS = 15;

    char items[A_ROWS][A_COLUMNS];
    char itemDetails;

    fromFile(in, items, A_ROWS, A_COLUMNS, itemDetails);

    in.close();
    return 0;
}

void fromFile(ifstream & in, char items[][15], const int A_ROWS, const int A_COLUMNS, char itemDetails)
{
    for (int rows = 0; rows < A_ROWS; rows++)
    {
        for (int columns = 0; columns < A_COLUMNS; columns++)
        {
            string theData = "";
            if (getline(in, theData))
            {
                stringstream str(theData);
                while (str >> itemDetails)
                {
                    items[rows][columns] = itemDetails;
                    // test to display all the data from the file properly
                    // cout << items[rows][columns] << " ";
                }
                cout << endl;
            }
        }
    }
}

解决方案

I would use a simple array to account for all the letters. The alphabet has 26 letters (a-z), so I would create an array with a length of 26 where each position represents a letter. You can then find the position of each letter in the array by subtracting 'a' from each letter. It would work pretty much like a map.

Here is a snippet:

int letters[26] =  {0};
letters['z' - 'a'] += 1; // letters[25], or 26th element.

这篇关于从二维数组和字符串流数据显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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