Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 计算从文件中提取的列表中的总和和平均值_C++_Arrays_Loops_Vector_Iostream - Fatal编程技术网

C++ 计算从文件中提取的列表中的总和和平均值

C++ 计算从文件中提取的列表中的总和和平均值,c++,arrays,loops,vector,iostream,C++,Arrays,Loops,Vector,Iostream,我想在请求帮助之前,我应该提到,这是我的计算机科学课程中的各种练习之一,帮助我们进一步了解如何从文件等中提取输入。任何帮助都是非常感谢的,无论是直接的“放弃一切”回答还是伪代码类型的回答。这是提示 编写一个C++程序,从文件中读取列表,并在适当的情况下报告名称、大小、平均值和第二个最大数,如果每个列表都合适的话;并报告列表名称和最大和列表的和。要读取的文件包括:一个文件哨兵,一个不等于任何列表哨兵的数字。。。然后零个或多个:一个列表哨兵,一个不等于此列表中任何值的数字;列表名称,可作为C++ S

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

编写一个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。First以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;
int main()
{
//每个文件循环一次
//如果文件名字符串为“结束”或“完成”,则停止
while(true){
字符串文件名;
cout>文件名;
如果(文件名==“结束”| |文件名==“完成”)
{
打破
} 
//打开文件
ifstream-infle(文件名);
cout>x;
} 
infle>>x;
}

cout这里有一些代码,可以按照问题的方式阅读列表。我还没有测试过这段代码,所以对于任何错误我深表歉意,但它确实符合这个问题

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 
    ...
}

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

你应该使用
std::map
。键是列表的名称,值是一个结构,其中包含您希望保存的关于每个列表的信息的名称(大小、平均值等)。您上面的代码似乎根本不起作用。你测试过了吗?首先,你不读sentinel文件。你也不会阅读或检查任何哨兵名单。你不看名单上的名字。最后,该文件包含浮点值,但您需要编写读取整数的代码。我认为这根本不符合你被要求做的任务。在你做任何其他事情之前,你应该先把它整理一下。上面的代码远远不是正确的,我不知道该怎么想。你似乎没有读过这个问题。我请了一年的编程假,这就是为什么这么简单的概念在我脑海中如此混乱的原因。很抱歉,现在我想上面的代码是非常错误的。无论如何,我面临的一个主要问题是如何存储列表项。我应该将它们存储到数组或向量中,还是应该如何进行?因为基本上,每个元素都需要存储,以便对其进行数学计算(平均值、总和等),然后必须保存最高总和。。。或者类似的东西。我不知道该怎么做。你需要计算的东西(大小、平均值、总和、第二大)都不需要你将整个列表存储在数组或向量中。对于总数,你只需在进行过程中将它们相加,对于平均数,你只需将总和除以大小,等等,明白了。“我会尽我最大的努力,很快就回来报到,谢谢你帮我度过难关,”约翰说。