0

我正在尝试编写一个程序,该程序要读取由 4 列组成的文本文件(时间戳、登录/注销、正在注销/登录的内容、用户注销)

前任

    15:47:10 OUT: "RS5K_700.EXE" SYSTEM@ALNWSPCNTRCTR1
    15:47:10 IN: "RS5K_700.EXE" SYSTEM@ALNWSPCNTRCTR1
    16:07:48 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    16:10:15 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    16:11:28 IN: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    16:46:34 IN: "RS500.EXE" mlulchak@alnsvpmillterm
    17:01:49 OUT: "RS500.EXE" mlulchak@alnsvpmillterm
    17:49:32 IN: "RS500.EXE" mlulchak@alnsvpmillterm
    9:30:28 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    15:39:32 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    15:40:10 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    15:40:31 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    0:10:59 IN: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    1:28:20 IN: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    1:28:38 IN: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
    1:28:41 IN: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1

我希望这个程序对列表进行排序,只写没有重新登录的行(即,如果退出,列表中应该有以后登录。列表是有序的,但时间如此,所以不用担心。)

这是我到目前为止所拥有的:

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

    int main()
    {
    ifstream inFile;
    string array[2000][4];

    int count, i, j, h;
    inFile.open("test.rtf");

    if(!inFile){
    cout << "Unable to open file" << endl;
    exit(1);
    }
    i=0;
    while(!inFile.eof()) {
    inFile>> array[i][0]   >>array[i][1]   >>array[i][2] >>array[i][3];
    i++;
            }
        inFile.close();


    j=0;
    h=i;

    while(j<i-1)
    {   
cout << j;
count=j+1;

 if (array[j][1]== "OUT:")
 {
     while(count < i)
     {
         if(array[count][1]=="IN:" && array[count][2]==array[j][2] && array[count][3]==array[j][3])
         {
            for (int k = count; k < i; k++)
            {
            array[k][0] = array[k+1][0]; 
            array[k][1] = array[k+1][1];
            array[k][2] = array[k+1][2];
            array[k][3] = array[k+1][3];
            }
        array[i-1][0] = " ";
            array[i-1][1] = " ";
            array[i-1][2] = " ";
            array[i-1][3] = " ";
            i--;
            for (int k = j; k < i; k++){
            array[k][0] = array[k+1][0]; 
            array[k][1] = array[k+1][1];
            array[k][2] = array[k+1][2];
            array[k][3] = array[k+1][3];
            }
            array[i-1][0] = " ";
            array[i-1][1] = " ";
            array[i-1][2] = " ";
            array[i-1][3] = " ";

            i--;
            count=i;

         }
         else if (array[j][1]== "IN:")
 {
     for (int k = j; k < i; k++)
     {
        array[k][0] = array[k+1][0]; 
        array[k][1] = array[k+1][1];
        array[k][2] = array[k+1][2];
        array[k][3] = array[k+1][3];            
     }
        array[i-1][0] = " ";
        array[i-1][1] = " ";
        array[i-1][2] = " ";
        array[i-1][3] = " ";
        j++;
        i--;
 }
         else if (count=i)
         {
             j++;

         }

         else
         {
             count++;
         }

     }

 }
 else if (array[j][1]== "IN:")
 {
     for (int k = j; k < i; k++)
     {
        array[k][0] = array[k+1][0]; 
        array[k][1] = array[k+1][1];
        array[k][2] = array[k+1][2];
        array[k][3] = array[k+1][3];            
     }
        array[i-1][0] = " ";
        array[i-1][1] = " ";
        array[i-1][2] = " ";
        array[i-1][3] = " ";
        j++;
        i--;
 }

    }




    h=i;
    i=0;
    ofstream outFile;
    outFile.open("test2.rtf");
    while(h!=0){
    outFile << array[i][0] << " " << array[i][1] << " " <<array[i][2] << " "  <<array[i][3]<<endl;
    i++;
    h--;                
    }
    inFile.close();

    return 0;
    }

任何想法都会很好,因为我已经没有想法了:)

4

3 回答 3

0

我会使用mapset得到你想要的:

#include <iostream>
#include <string>
#include <map>
#include <set>

int main(int, char**) {
  using namespace std;

  string time, in_out, exe, user;
  map<string,int> out;

  while( cin >> time >> in_out >> exe >> user ) {
    if( in_out == "OUT:" ) 
      out[user]++;
    else
      out[user]--;
    if( out[user] == 0 )
      out.erase(user);
  }

  set<string> ordered;
  for( auto& s: out ) {
    ordered.insert(s.first);
  }
  for( auto& s: ordered ) {
    cout << s << endl;
  } 

  return 0;
} 

样本输入的输出是:

CONHorodeski@ALNWSPCNTRCTR1
mlulchak@alnsvpmillterm

(第一个在 16:07 和 16:10 登出两次,但在 16:11 只登入一次,第二个在 17:49 登入,从未登出)

于 2013-06-19T16:11:40.473 回答
0

我首先将每一行输入转换为逻辑记录。然后我会为每个“已注销”记录插入一条map记录,并删除每个“已登录”记录的匹配记录。根据您正在做的其他事情(如果有的话), aset可能比 a 更容易一些map

如果您有当前未注销的项目的“已登录”记录,则表明输入存在问题。

完成后,写下map(或set)中的所有项目。这些是已注销但未重新登录的那些。

编辑:代码看起来像这样:

#include <string>
#include <set>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iostream>

struct record {
    std::string time;
    std::string inout;
    std::string item;
    std::string user;

    bool operator<(record const &other) const { 
        return item < other.item;
    }
};

std::istream &operator>>(std::istream &is, record &r) { 
    std::string line;
    std::getline(is, line);
    std::stringstream buffer(line);

    buffer >> r.time >> r.inout >> r.item >> r.user;
    return is;
}

std::ostream &operator<<(std::ostream &os, record const &i) { 
    return os << i.item << "\t" << i.user;
}

int main() {
    std::set<record> items;
    record temp;
    std::ifstream in("test.rtf");

    while (in >> temp) {
        if (temp.inout == "OUT:")
            items.insert(temp);
        else
            items.erase(temp);
    }

    for (auto const & i : items) 
        std::cout << i << "\n";
}
于 2013-06-19T15:36:09.557 回答
0

看看这个解决方案来解决您的问题,我建议您使用 stl 排序算法对输出进行排序,并使用 struct 来存储数据。

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

#define MAX 1005

struct record{
    string A,B,C,D;
    record(string A, string B, string C, string D) :
        A(A), B(B), C(C), D(D) {}

    bool operator < (const record& o) const
    {
        string tmp1 = A + " " + B + " " + C + " " + D;
        string tmp2 = o.A + " " + o.B + " " + o.C + " " + o.D;
        return (tmp1 < tmp2);
    }
};

int main() {
    string A,B,C,D;
    vector<record> my_vect;
    vector<record>::iterator it;

    ifstream inFile;
    inFile.open("test1.rtf");

    while(inFile >> A >> B >> C >> D) {
        if(B == "OUT:")
            my_vect .push_back( record(A,B,C,D) );
    }

    inFile.close();

    sort(my_vect.begin(), my_vect.end());

    ofstream outFile;
    outFile.open("test2.rtf");
    for(it = my_vect.begin(); it != my_vect.end(); ++it) {
        outFile << (*it).A << " " << (*it).B << " " << (*it).C << " " << (*it).D << endl;
    }
    outFile.close();

    return 0;
}

这个算法的输出是:

15:39:32 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
15:40:10 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
15:40:31 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
15:47:10 OUT: "RS5K_700.EXE" SYSTEM@ALNWSPCNTRCTR1
16:07:48 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
16:10:15 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
17:01:49 OUT: "RS500.EXE" mlulchak@alnsvpmillterm
9:30:28 OUT: "RS5K_700.EXE" CONHorodeski@ALNWSPCNTRCTR1
于 2013-06-19T16:21:12.637 回答