简体   繁体   English

字符串比较C ++

[英]String Compare C++

I am trying to write a program that is to read in a text file which consists of 4 columns (time stamp, signed in/out, what is being signed out/in, user signing it out) 我正在尝试编写一个程序来读取一个包含4列的文本文件(时间戳,登录/注销,正在注销/登录,用户注销)

ex

    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

I want this program to sort the list and only write the lines that were not signed back in (ie if signed out there should be a later sign in in the list. The list is in order but time so no worries on that.) 我希望该程序对列表进行排序,只写未登录的行(即,如果退出,则该列表中应有一个以后的登录。该列表是有序的,但时间有限,因此不必担心。)

Here is what I have so far: 这是我到目前为止的内容:

    #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;
    }

Any thoughts would be great cause I am running out of ideas :) 任何想法都将是个好主意,因为我的想法不多了:)

I would start by converting each line of input into a logical record. 我将从将输入的每一行转换为逻辑记录开始。 Then I'd insert a record into a map for each "signed out" record, and remove the matching record for each "signed in" record. 然后,我将针对每个“已注销”记录的记录插入到map中,并删除针对每个“已登录”记录的匹配记录。 Depending on what (if anything) else you're doing, a set might be a little easier than a map . 根据您在做什么(如果有的话), set可能比map容易一些。

If you have a "signed in" record for an item that's not currently signed out, you signal a problem with the input. 如果您有当前未注销的项目的“已登录”记录,则表明输入存在问题。

When you're done, write out all the items in the map (or set ). 完成后,写出map (或set )中的所有项目。 Those are the ones that have been signed out but not signed back in. 这些是已注销但尚未重新登录的帐户。

Edit: code would look something like this: 编辑:代码看起来像这样:

#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";
}

I would use map and set to get what you want: 我将使用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;
} 

The output for the sample input is: 样本输入的输出为:

CONHorodeski@ALNWSPCNTRCTR1
mlulchak@alnsvpmillterm

(the first one logged out twice at 16:07 and 16:10 but just logged in once at 16:11 and the second one logged in at 17:49 and never logged out) (第一个在16:07和16:10退出两次,但在16:11才登录一次,第二个在17:49登录并且从未退出)

Take a look at this solution for your problem, I suggest to you to use stl sort algorithm for sort the output and struct for store data. 请看一下针对您的问题的解决方案,我建议您使用stl排序算法对输出进行排序,并对存储数据进行结构化。

#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;
}

The output for this algo is: 该算法的输出为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM