This commit is contained in:
Bob Polis 2018-01-23 13:17:40 +01:00
parent 64ea959b51
commit 1df4d28705
2 changed files with 75 additions and 0 deletions

35
Makefile Normal file
View File

@ -0,0 +1,35 @@
CC=gcc
CXX=g++
RM=rm -f
CP=cp
MKDIR=mkdir -p
CPPFLAGS=-Wall -O2 -std=c++11
LDFLAGS=
LDLIBS=
TOOL=ranlin
SRCS=main.cpp
OBJS=$(subst .cpp,.o,$(SRCS))
all: $(TOOL)
$(TOOL): $(OBJS)
$(CXX) $(LDFLAGS) -o $(TOOL) $(LDLIBS) $(OBJS)
depend: .depend
.depend: $(SRCS)
$(RM) ./.depend
$(CXX) $(CPPFLAGS) -MM $^>>./.depend;
clean:
$(RM) $(OBJS)
dist-clean: clean
$(RM) *~ ./.depend $(TOOL)
install:
$(RM) ~/bin/$(TOOL)
$(CP) $(TOOL) ~/bin/
include .depend

40
main.cpp Normal file
View File

@ -0,0 +1,40 @@
//
// main.cpp
// ranlin
//
// Created by Bob Polis on 05-03-15.
// Copyright (c) 2015 Thalictrum. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <string>
#include <random>
using namespace std;
double next_random() {
static random_device dev;
static default_random_engine engine {dev()};
static uniform_real_distribution<double> dist {0, 1};
return dist(engine);
}
string random_line_from_file(istream& file) {
string line;
string result;
for (int nr {1}; getline(file, line); ++nr) {
if (next_random() * nr < 1.0) {
result = line;
}
}
return result;
}
int main(int argc, const char * argv[]) {
if (argc > 1) {
ifstream infile {argv[1]};
cout << random_line_from_file(infile) << '\n';
} else {
cout << random_line_from_file(cin) << '\n';
}
}