Created
March 2, 2015 02:50
-
-
Save Fiona-J-W/a5c0abcc51fdf76b1c62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <vector> | |
| #include <iostream> | |
| #include <fstream> | |
| std::vector<unsigned> calculate_frequencies(std::istream& stream) { | |
| auto frequencies = std::vector<unsigned>(0x100); | |
| auto c = char{}; | |
| while (stream.get(c)) { | |
| if(!std::isspace(c) and !std::ispunct(c)) { | |
| ++frequencies.at(static_cast<unsigned char>(c)); | |
| } | |
| } | |
| return frequencies; | |
| } | |
| int main(int argc, char** argv) { | |
| if (argc != 2) { | |
| std::cerr << "Usage: " << argv[0] << " <input-file>\n"; | |
| return 1; | |
| } | |
| std::ifstream input{argv[1]}; | |
| if(!input.is_open()) { | |
| std::cerr << "Could not open File " << argv[1] << '\n'; | |
| return 2; | |
| } | |
| const auto frequencies = calculate_frequencies(input); | |
| std::ofstream output{"Freq.dat"}; | |
| if(!output.is_open()) { | |
| std::cerr << "Could not open output (Freq.dat)\n"; | |
| return 3; | |
| } | |
| for (auto i = std::size_t{}; i < 0x100; ++i) { | |
| if (frequencies.at(i)) { | |
| output << i << " " << static_cast<char>(i) << " " << frequencies.at(i) << '\n'; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment