int main() { PRINT("This example loads dictionaries from the EDS library."); std::unordered_map<kaco::Address, kaco::Entry> dictionary; std::unordered_map<std::string, kaco::Address> name_to_address; kaco::EDSLibrary library(dictionary, name_to_address); bool success = library.lookup_library(); if (!success) { ERROR("EDS library not found."); return EXIT_FAILURE; } success = library.load_default_eds(402); if (!success) { ERROR("load_default_eds(402) failed."); } else { print_dictionary(dictionary); } // This should fail. dictionary.clear(); name_to_address.clear(); success = library.load_default_eds(405); if (!success) { ERROR("load_default_eds(405) failed."); } else { print_dictionary(dictionary); } return EXIT_SUCCESS; }
void print_dictionary(std::ostream& os, const bnode<symbol>* root, std::string& s) { //std::cout << "traversing root " << root->value.count <<std::endl; if ((root->left == 0) && (root->right == 0)) { os << root->value.value << " " << s << std::endl; return; } else if ((root->left == 0) || (root->right == 0)) { std::cout << "incorrect tree" << std::endl; throw std::runtime_error("incorrect tree"); } // visit left '0' path s.push_back('0'); print_dictionary(os, root->left, s); s.resize(s.size() - 1); // visit right '1' path s.push_back('1'); print_dictionary(os, root->right, s); s.resize(s.size() - 1); } // print_dictionary
int main(int argc, char* argv[]) { if (argc != 2) { std::cout << "usage: ./a7 file" << std::endl; return -1; } // get input text std::ifstream f(argv[1]); std::string s; std::vector<std::string> lines; while (!f.eof()) { s = ""; std::getline(f, s); if (!s.empty()) lines.push_back(s); } f.close(); // create a list of symbols std::vector<symbol> A; symbols(lines.begin(), lines.end(), std::back_inserter(A)); // process the list (student's code) bnode<symbol>* tree = huffman_tree(A.begin(), A.end()); // print dictionary print_dictionary(std::cout, tree); // free memory (student's code) release_tree(tree); // print dictionary print_dictionary(std::cout, tree); return 0; } // main
int main(){ struct dictionary *d = NULL, *tmp; char *text = "Ciao a tutti ciao ciao a tutti tutti ciao ciao ciao tutti ciao a a"; char *word; do{ word = next_word(text); if(d==NULL) d=new_dictionary_element(word, 1); if( (tmp = search_word(d, word)) != NULL){ tmp->occurences++; } else { insert_word(d, word, 1); } if(text=strchr(text, ' ')) text++; }while( text != NULL ); print_dictionary(d); }
inline void print_dictionary(std::ostream& os, const bnode<symbol>* root) { std::string s; print_dictionary(os, root, s); } // print_dictionary