【问题标题】:Calculating sum and mean in a list pulled from a file计算从文件中提取的列表中的总和和平均值
【发布时间】:2015-12-06 05:41:23
【问题描述】:

我想在请求帮助之前,我应该提到这是我的计算机科学课程中的各种练习之一,以帮助我们进一步了解如何从文件等中提取输入。任何帮助表示赞赏,无论是直接“放弃”答案还是伪代码类型的响应。所以,这里是提示。

编写一个 C++ 程序,从文件中读取列表并报告每个列表的名称、大小、平均值(如果合适)和第二大数字(如果合适);并报告列表名称和总和最大的列表的总和。要读取的文件包括: 一个文件哨兵,一个不等于任何列表哨兵的数字......然后是零个或多个: 一个哨兵列表,一个不等于此列表中任何值的数字;列表名称,可作为 C++ STL 字符串读取;零个或多个由空格分隔的列表值;列表哨兵然后是文件哨兵。

它包含一个示例,下面是示例显示的内容。

31 17.3 第一 26.2 -11.2 8.1 17.3 0.0 第二 0.0 31

预期输出:

第一个大小是 3,平均值是 7.7,第二个最大值是 8.1。第二个大小为 0。第一个具有最大列表和 23.1。

抱歉,我知道这有点令人困惑。我自己也很困惑,但那是你的大学课程。所以,无论如何,这就是我到目前为止所得到的!

#include <iostream> 
#include <string> 
#include <fstream>
using namespace std;

int main()
{
    // loop once per file 
    // stop if filename string is "end" or "done" 
    while (true) {
        string filename;
        cout << "Enter filename or \"end\" to quit: ";
        cin >> filename;

        if (filename == "end" || filename == "done")
        {
            break;
        } 

        // open the file 
        ifstream infile(filename);
        cout << "Processing " << filename << ", please wait...\n"; 

        // processing just if file is opened 
        if (infile) 
        { 
            // count the number of lists
            int numLists = 0;
            int x =  0;
            infile >> x;

            while (!infile.fail())
            {
                ++numLists; 
                // read one list 
                while (x != 31)
                {
                    infile >> x;
                } 
                infile >> x;
            }

            cout << filename << " contains " << numLists << " lists.\n";
        }
    }
}

所以,到目前为止,我的输出正在确定每个文件中有多少个列表,但是,我尝试了几种方法来获取列表的总和和平均值,但我只是不知道如何存储每个列表,每个文件的值。我考虑过数组或向量,但我真的不知道。任何人都可以提供任何帮助,我将不胜感激。请!

【问题讨论】:

  • 您应该使用std::map。键是列表的名称,值是一个结构,其中包含您希望保存的有关每个列表的信息的名称(大小、平均值等)。
  • 您上面的代码似乎根本不起作用。你测试过吗?首先,您不阅读文件 sentinel.您也不会阅读或检查任何列表哨兵。您不阅读列表名称。最后,该文件包含浮点值,但您编码读取整数。我认为它根本不符合你被要求做的任务。在你做任何其他事情之前,你应该解决这个问题。
  • 上面的代码远非正确我不知道该怎么想。你似乎没有读过这个问题。

标签: c++ arrays loops vector iostream


【解决方案1】:

这里有一些代码可以按照问题提出的方式阅读列表。我没有测试过这段代码,所以对任何错误表示歉意,但它确实与问题相对应。

ifstream in(filename);
// read the file sentinel first
double file_sentinel;
in >> file_sentinel;
for (;;)
{
    // read next sentinel
    int sentinel;
    in >> sentinel;
    // if it's a file sentinel then we are done
    if (sentinel == file_sentinel)
        break;
    // it must be a list sentinel so read the list starting with the name
    string name;
    in >> name;
    // start of list processing
    ...
    for (;;)
    {
        // read the next item in the list
        double item;
        in >> item;
        // check if the item is actually the list sentinel, if so the we're done with this list
        if (item == sentinel)
            break;
        // now process list item
        ...
    }
    // now process whole list 
    ...
}

没有错误检查(不确定是否有必要),我为处理问题要求您做的处理留了一些空白。希望它能给你一个开始。

【讨论】:

  • 我休学了一年的编程时间,这就是为什么这么简单的概念在我脑海中如此混乱的原因。很抱歉,现在我考虑一下上面的代码是非常错误的。无论如何,我遇到的主要问题之一是如何存储列表项。我应该将它们存储到数组还是向量中,或者我应该如何去做?因为基本上,每个元素都需要存储,以便可以对它们进行数学计算(平均值、总和等),然后必须保存最高总和......或者类似的东西。我不知道该怎么做。
  • 您必须计算的所有内容(大小、平均值、总和、第二大)都不需要您将整个列表存储在数组或向量中。对于总和,您只需将它们相加即可,平均而言,您只需将总和除以大小等。
  • 明白了。我会尽我所能并尽快报告,感谢@john 帮助我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
相关资源
最近更新 更多