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

/*
* File: Main.cpp
* Author: 88250 <DL88250@gmail.com>, 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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值