c ++以7为一组分解向量

时间:2014-11-22 13:25:30

标签: c++ regex vector

我将stdout数据插入到一个向量中,我需要在模式匹配后将其放入7个组中(反映入站数据),以便每个元素[0-6]可以运行计算(即vrecords [2] =“狗“;

我要么最终得到一个无限循环,只有前7个,7个循环7次循环7次或者垃圾。我不能生成4组7个独特的记录。

入站数据:

198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
198397685
2014-11-14 15:10:13
Buy
0.00517290
0.00100000
0.00100000
0.00000517
198398295
2014-11-14 15:11:14
Buy
0.00517290
0.00100000
0.00100000
0.00000517
203440061
2014-11-21 16:13:13
Sell
0.00825550
0.00100000
0.00100000
0.00000826

预期产出:

found buy
198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
found buy
198397685
2014-11-14 15:10:13
Buy
0.00517290
0.00100000
0.00100000
0.00000517
found buy
198398295
2014-11-14 15:11:14
Buy
0.00517290
0.00100000
0.00100000
0.00000517
203440061
2014-11-21 16:13:13
Sell
0.00825550
0.00100000
0.00100000
0.00000826

代码:

char *data()
{
        char buff[BUFSIZ];
        FILE *fp = stdout
        char * cstr = buff;
        std::vector<std::string>::const_iterator g;
        vector<std::string> vrecords;
        while(std::fgets(buff, sizeof buff, fp) != NULL){
                //get rid of null termination from fgets
                size_t n = std::strlen( buff );
                if ( n && buff[n-1] == '\n' ) buff[n-1] = '\0';
                //push everything into vector
                if ( buff[0] != '\0' ) vrecords.push_back( buff );
        }
        int count;
        count = 0;
        for(int t = 0; t < vrecords.size(); ++t){
                auto vecbuy = std::find( vrecords.begin(), vrecords.end(), "Buy" );
                auto vecsell = std::find( vrecords.begin(), vrecords.end(), "Sell" );
                if ( vecbuy != vrecords.end() ){
                        //cout << vrecords[t] << " " << endl;
                        cout << "found buy" << endl;
                }
                if ( vecsell != vrecords.end() ){
                        cout << "found sell" << endl;
                }
                if ( count == 6){
                        for(g=vrecords.begin(); g!=vrecords.end(); ++g){
                                std::cout<<(*g)<<std::endl;
                        }
                        count = 0;
                }
                ++count;
                //cout << vrecords[t] << " " << endl;

        }
}

现有产出: output

3 个答案:

答案 0 :(得分:0)

对不起我不是c ++流利,但逻辑就在这里。您可以使用模数并存储每个块的两个第一个值:

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    FILE * pFile;
    char buff [50], value[2][50];
    int i=0, j=0;

    pFile = fopen ("myfile.txt" , "r");
    if (pFile == NULL) perror ("Error opening file");
    else {
        while ( fgets (buff , 50 , pFile) != NULL )
            if (2 > (j = i++%7)) // buff contains one of the two first items
                strcpy(value[j], buff); // these items are stored in the value array
            else if (j==2) {            // the third item
                if (!strcmp(buff, "Buy\n"))
                    cout << "Buy found" << endl;
                cout << value[0]
                     << value[1]
                     << buff;
            } else // other items
                cout << buff;
        fclose (pFile);
   }
   return 0;
}

答案 1 :(得分:0)

代替难以理解的循环,编写代码使其不仅易于理解,而且必要时可以轻松维护和调试。

创建&#34;交易&#34;类型:

#include <string>

struct Transaction
{
    bool m_isbuy;  // buy or sell
    std::string trans_date;  // date
    std::string trans_number;  // number
    std::vector<std::string> trans_data;  // vector of the data
};

现在,创建一个函数,当给定记录计数时,使用数据填充Transaction中的字段:

void addToTransaction(Transaction& trans, const std::string& data, int nWhich)
{
    switch (nWhich)
    {
        case 1:
            trans.trans_number = data;
        break;
        case 2:
            trans.trans_date = data;
        break;
        case 3:
            trans.m_isbuy = (data == "Buy");
        break;
        default:
            trans.trans_data.push_back(data);
        break;
    }
}

请注意,switch语句中的每个case都会更改传入的Transaction类型中的项目。

接下来,我们进入输出功能。这很简单:

void outputInformation(const Transaction& trans)
{
    cout << "found " << (trans.m_isbuy ? "buy" : "sell") << "\n";
    cout << trans.trans_number << "\n";
    cout << trans.trans_date << "\n";
    cout << (trans.m_isbuy ? "Buy" : "Sell") << "\n";
    copy(trans.trans_data.begin(), trans.trans_data.end(), ostream_iterator<std::string>(cout, "\n"));
}

基本上,我们采取交易,并输出交易的信息。代码可能有点高级,但它只是输出。如果需要,您可以将其更改为更简单的内容。

最后,main()函数:

int main()
{
    // open the data file
    ifstream ifs("data.txt");
    if (ifs)
    {
        std::string bufLine;
        // start with new transaction
        int counter = 0;
        Transaction curTrans;

        // start loop
        while (getline(ifs, bufLine))
        {
            // change the transaction field
            addToTransaction(curTrans, bufLine, counter+1);
            ++counter;

            // check if we need to output the info
            if (counter == 7)
            {
                // output info and start all over
                outputInformation(curTrans);
                curTrans = Transaction();
                counter = 0;
            }
        }
    }
}

您认为我们没有做任何事情来寻找回车。这很简单。

1)我们打开文件。

2)我们在0处开始计数。对于文件中读取的每一行,我们调用该函数来更改事务数据类型中的项。

3)当计数器点击7时,我们输出交易并清除另一笔交易的交易。

代码比循环稍长,但是将此代码的可读性与您编写的循环进行比较。任何对C ++知之甚少的人都可以理解并遵循它。

以下是我的代码的实时演示:http://ideone.com/GIUeKQ

演示只接受来自cin的输入,因此它与答案中的代码略有不同。只需将输入从cin更改为ifs即可。

答案 2 :(得分:0)

在这里,试试这个。 它应该是你正在寻找的更清洁版本

char *data(){
        FILE *fp = stdout;
        if (fp == NULL) perror ("Error opening file");
        char buff[BUFSIZ];
        bool more = true;
        do {
                vector<string> vrecords;
                for (int t = 0; (t < 7) && (more = (fgets(buff, BUFSIZ, fp) != NULL)); ++t) {
                        size_t n = strlen(buff);
                        if (n && buff[n - 1] == '\n')
                                buff[n - 1] = '\0';
                        //push everything into vector
                        if (buff[0] != '\0')
                                vrecords.push_back(buff);
                }
                bool buy, sell;
                if ((buy = find(vrecords.begin(), vrecords.end(), "Buy") != vrecords.end())){
                        cout << "Found buy!" << endl;
                        cout << vrecords[2] << endl;
                }
                if ((sell = find(vrecords.begin(), vrecords.end(), "Sell") != vrecords.end())){
                        cout << "Found sell!" << endl;
                }
                if (buy || sell) {
                        for (auto it = vrecords.begin(); it != vrecords.end(); ++it)
                                cout << *it << " " << endl;
                        cout << endl;
                }
        } while (more);
}