Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string>
- #include <fstream>
- #include <sstream>
- using namespace std;
- typedef map<string, short> KeyMap;
- typedef pair<string, short> Key;
- void processString(const string &s, ofstream &ofile)
- {
- KeyMap numberMap;
- Key b2("abc", 2);
- Key b3("def", 3);
- Key b4("ghi", 4);
- Key b5("jkl", 5);
- Key b6("mno", 6);
- Key b7("pqrs", 7);
- Key b8("tuv", 8);
- Key b9("wxyz", 9);
- Key b0(" ", 0);
- numberMap.insert(b2);
- numberMap.insert(b3);
- numberMap.insert(b4);
- numberMap.insert(b5);
- numberMap.insert(b6);
- numberMap.insert(b7);
- numberMap.insert(b8);
- numberMap.insert(b9);
- numberMap.insert(b0);
- typedef KeyMap::const_iterator iter;
- string space;
- iter iterator_result;
- iter j;
- bool found = false;
- for(unsigned int i = 0; i < s.size(); i++) {
- for(j= numberMap.begin(); j != numberMap.end(); j++ ) {
- space = "";
- if(j->first.find(s[i]) != string::npos) {
- found = true;
- if(j->first.find(s[i+1]) != string::npos)
- space = " ";
- break;
- }
- }
- if( found ) {
- for( unsigned int k = 0; k < j->first.size() ; k++) {
- ofile << j->second;
- if(s[i] == j->first[k]) {
- if((s[i] == s[i+1]))
- ofile << ' ';
- ofile << space;
- break;
- }
- }
- }
- }
- ofile << endl;
- }
- void T9SpellingMain()
- {
- ifstream ifile("T9input.txt");
- ofstream ofile("T9output.txt");
- stringstream ss;
- int n;
- string s;
- while(std::getline(ifile, s)) {
- ss.str(s);
- ss >> n;
- for(int i = 1; i <= n; i++) {
- s.clear();
- std::getline(ifile, s);
- ofile << "Case #" << i << ": ";
- processString(s, ofile);
- //ofile << ' ';
- }
- ss.clear();
- }
- if(ifile.is_open())
- ifile.close();
- if(ofile.is_open())
- ofile.close();
- }
- int main() {
- T9SpellingMain();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement