| Код | #include <iostream> #include <vector> #include <algorithm> #include <fstream> #include <string>
struct pair { char m_symbol; int m_count; pair (char symbol, int count = 0 ):m_symbol(symbol), m_count(count){} };
void display (pair* obj) { std::cout << obj->m_symbol << ' ' << obj->m_count << std::endl; }
int main(int argc, char* argv[]) { std::string path_alpha; std::string path_text; std::cout << "Enter path to the file with alphabet: "; std::getline(std::cin, path_alpha); std::ifstream in_file( path_alpha.c_str() ); if ( !in_file.is_open() ) { std::cout << "File does not exist"; std::cin.get(); return -1; } std::vector<pair*> sym_stats; while (true) { char buf = '\0'; in_file.get(buf); if ( in_file.eof() ) break; if (buf != ' ' && buf != '\n') sym_stats.push_back( new pair(buf) ); } in_file.close(); in_file.clear(); std::cout << "Enter path to the file with text: "; std::getline(std::cin, path_text); in_file.open( path_text.c_str() ); if ( !in_file.is_open() ) { std::cout << "File does not exist"; std::cin.get(); return -1; } if ( !in_file.is_open() ) return -1; while (true) { char buf = '\0'; in_file.get(buf); if ( in_file.eof() ) break; if (buf != ' ' && buf != '\n') { for (std::vector<pair*>::const_iterator iter = sym_stats.begin(); iter != sym_stats.end(); ++iter) if ( (*iter)->m_symbol == buf ) ++(*iter)->m_count; } } std::cout << "Results:\n" std::for_each(sym_stats.begin(), sym_stats.end(), display); std::cin.sync(); std::cin.get(); return 0; }
|
|