-4

正如您从标题中可以理解的那样,我真的是这个编程环境的新手。我想向您寻求帮助:我需要阅读以下文件:

11:02,11:06
15:45,16:05
...

这些数据是一个在另一个之上。所以我们有 11 列(11 个字符加上换行符)和 239 行。在原始文件中,它们都是随机排序的。我应该按升序对它们进行排序。最后,可视化输出当然会很好。到目前为止,我已经完成了第一步,我正在读取文件“char by char”。但我不能再继续下去了;我的知识不允许我这样做。如果有人可以帮助我,我会非常高兴。我真的很需要这些东西。

4

1 回答 1

0

这是 Perl 中的数据生成器脚本;它在每一行上生成 2 次,第二次总是晚于第一次:

#!/usr/bin/env perl
use strict;
use warnings;

foreach my $i (1..239)
{
    my $h1 = 10 + rand 8;
    my $h2 = $h1 + 1;
    my $m1 = rand 60;
    my $m2 = rand 60;
    printf "%.2d:%.2d,%.2d:%.2d\n", $h1, $m1, $h2, $m2;
}

这是一个适度冗长的 C++ 程序,它对 Perl 生成的数据文件进行排序,其方式与 Unixsort程序相同。

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<std::string> input;
    std::string line;

    while (std::getline(std::cin, line))
        input.push_back(line);

    std::sort(input.begin(), input.end());

    //C++03
    //for (std::vector<std::string>::iterator it = input.begin(); it != input.end(); ++it)
    //    std::cout << *it << std::endl;

    //C++11
    for (auto &it : input)
        std::cout << it << std::endl;
}

可能有压缩 C++ 的方法,但它适用于我(Mac OS X 10.8.3 上的 G++ 4.7.1):

    g++ -O3 -g -std=c++11 -Wall -Wextra rs.cpp -o rs

肯定有压缩 Perl 代码的方法。

于 2013-05-30T01:02:28.310 回答