检查随机序列重复[C++]

/*
* File: Main.cpp
* Author: 88250 <[email protected]>, http://blog.csdn.net/DL88250
*
* Created on May 13, 2008, 6:25 PM
*/

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <time.h>

using namespace std;

/**
* Check the same record in a file.
*
* Every record in data file is a random serial, like the followings:
* // data file
* 1902323484354370234844
* 1928473090393719374
* ....
*/

vector<string> records; // store the data records
vector<vector<string> > statistics; //statistics

/**
* Read the records from the data file which named "data.txt" into memory,
* using a list store them.
*/
void readRecords() {
cout << "Get starting read records...." << endl;
ifstream fin("data.txt");

if (!fin) {
cout << "Cannot open input file!" << endl;
return;
}

string aLine;
while (getline(fin, aLine)) {
records.push_back(aLine);
}
fin.close();
cout << "The amount of records: " << records.size() << endl;
}

/**
* Display the data records in console.
* @param amount display amount, start from {@link #records}'s beginning
*/
void displayRecords(int amount) {
if (amount < 0 || amount > records.size()) {
cout << "The specified amount exceeds the Data records" <<
"size!" << endl;
}
cout << "Display: " << endl;
for (int i = 0; i < amount; i++) {
cout << records.at(i) << endl;
}
cout << endl;
}

/**
* Display the statistic results in console.
*/
void displayStats() {
cout << "Statistics: " << endl;
cout << "A amount of the same data records: " << statistics.size() << endl;
for (int i = 0; i < statistics.size(); i++) {
vector<string> aEqualities = statistics.at(i);
cout << aEqualities.at(0) << " occurs " << aEqualities.size() << endl;
}
}
/*
bool greater(string s1, string s2){
return s1.compare(s2);
}
*/

/**
* Check the same data records.
*/
void checkTheSame() {

sort(records.begin(), records.end()); // sort them

// displayRecords(10);
for (int i = 0; i < records.size() - 1; i++) {
string record1 = records.at(i);
string record2 = records.at(i + 1);
if (record1 == record2) {
vector<string> equalities;
equalities.push_back(record1);
equalities.push_back(record2);
statistics.push_back(equalities);
}
}
displayStats();
}

int main(int argc, char** argv) {
readRecords();

displayRecords(10);
checkTheSame();
cout << "Elapsed time: " << clock() / CLOCKS_PER_SEC << endl;
return (EXIT_SUCCESS);
}


你可能感兴趣的:(C++)