zoukankan      html  css  js  c++  java
  • 检查随机序列重复[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);
    }


  • 相关阅读:
    eclipse中android单元测试
    以树形结构的形式输出指定目录下面的所有文件
    在一个文件末尾增加字符串,并在控制台打印出来
    读取一个文件的数据经过某种操作,将结果存储到另外一个文件
    读取一个文件中的字符,统计每个字符出现的次数
    合并两个递增排序的链表
    找出单链表的倒数第K个(从1开始计数)结点的值
    反转一个链表并输出各个结点的值
    输入一个有序数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字
    字符串翻转,单词内不翻转
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6470278.html
Copyright © 2011-2022 走看看