Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //translator.h
- #ifndef TRANSLATOR_H
- #define TRANSLATOR_H
- #include <string>
- #include <cstring>
- const int MAXLINE=1000;
- const int MAXWORDS=4000;
- class dict
- {
- public:
- dict (const char dictFileName[]);
- string lookup_english(const string inputword);
- string lookup_elvish(const string inputword);
- private:
- string english_dict[MAXWORDS];
- string elvish_dict [MAXWORDS];
- };
- class Translator
- {
- public:
- Translator(const char s[]);
- void toElvish(char * outputline, const string inputline);
- void toEnglish(char * outputline, const string inputline);
- private:
- dict dictionary;
- };
- #endif
- //main.cpp
- #include <iostream>
- #include <fstream>
- #include "Translator.h"
- using namespace std;
- // maximum number of characters in a line of the text
- const int MAXLINE=1000;
- int main(int argc, char *argv[])
- {
- if (argc<2)
- {
- cout << "No story file specified." << endl;
- return -1;
- }
- fstream infile;
- infile.open(argv[1], ios::in);
- if (infile.fail())
- {
- cout << "Could not open the story file." << endl;
- return -1;
- }
- Translator translator("englishtoelvish.txt");
- fstream outfile;
- outfile.open("story_in_elvish.txt", ios::out);
- if (outfile.fail())
- {
- cout << "Could not open the output file." << endl;
- return -1;
- }
- char english_line[MAXLINE], elvish_line[MAXLINE];
- // Translate the story into Elvish
- while (!infile.fail())
- {
- infile.getline(english_line, MAXLINE, '\n');
- if (!infile.fail())
- {
- translator.toElvish(elvish_line, english_line);
- outfile << elvish_line << endl;
- }
- }
- outfile.close();
- infile.close();
- // Read the translated story and re-translate into English
- infile.open("story_in_elvish.txt", ios::in);
- outfile.open("story_backto_english.txt", ios::out);
- while (!infile.eof())
- {
- infile.getline(elvish_line, MAXLINE, '\n');
- if (!infile.fail())
- {
- translator.toEnglish(english_line, elvish_line);
- outfile << english_line << endl;
- }
- }
- infile.close();
- outfile.close();
- }
- //translator.cpp
- Translator::Translator(const char dictFileName[]) : dictionary(dictFileName)
- {
- }
- void Translator::toElvish(char * outputline, const std::string inputline)
- {
- string inputword;
- string outputstore;
- string outputword;
- int i=0;
- while(i<inputline.length())
- {
- while(isalpha(inputline[i]) || inputline[i]=='-' || inputline[i]=='*')
- {
- inputword+=inputline[i];
- i++;
- }
- if(inputword[0]>='A' && inputword[0]<='Z')
- {
- inputword[0]=tolower(inputword[0]);
- outputword=dictionary.lookup_english(inputword);
- if(outputword[0]=='*')
- {
- outputword[1]=toupper(outputword[1]);
- }
- else
- {
- outputword[0]=toupper(outputword[0]);
- }
- outputstore+=outputword;
- }
- else
- {
- outputstore+=dictionary.lookup_english(inputword);
- }
- inputword.clear();
- while(!(isalpha(inputline[i])) && !(inputline[i]=='-') && !(inputline[i]=='*'))
- {
- outputstore+=inputline[i];
- i++;
- }
- }
- strcpy(outputline, outputstore.c_str());
- }
- void Translator::toEnglish(char * outputline, const std::string inputline)
- {
- string inputword;
- string outputword;
- string outputstore;
- int i=0;
- while(i<inputline.length())
- {
- while(isalpha(inputline[i]) || inputline[i]=='-' || inputline[i]=='*')
- {
- inputword+=inputline[i];
- i++;
- }
- if(inputword[0]=='*')
- {
- inputword.erase(inputword.begin());
- inputword = inputword.substr(0, inputword.size()-1);
- outputstore+=inputword;
- }
- else
- if(inputword[0]>='A' && inputword[0]<='Z')
- {
- inputword[0]=tolower(inputword[0]);
- outputword=dictionary.lookup_elvish(inputword);
- outputword[0]=toupper(outputword[0]);
- outputstore+=outputword;
- }
- else
- {
- outputstore+=dictionary.lookup_elvish(inputword);
- }
- inputword.clear();
- while(!(isalpha(inputline[i])) && !(inputline[i]=='-') && !(inputline[i]=='*'))
- {
- outputstore+=inputline[i];
- i++;
- }
- }
- strcpy(outputline, outputstore.c_str());
- }
- dict::dict(const char dictFileName[])
- {
- fstream str;
- str.open(dictFileName, ios::in);
- int i=0;
- std::string wordin_english;
- std::string wordin_elvish;
- while (!str.eof())
- {
- str >> wordin_english;
- while(!english_dict[i].empty())
- i++;
- english_dict[i]=wordin_english;
- str >> wordin_elvish;
- elvish_dict[i]=wordin_elvish;
- }
- }
- string dict::lookup_english(const std::string inputword)
- {
- string wordout;
- int i=0;
- while(!(inputword==english_dict[i]))
- {
- i++;
- if(i>1748)
- break;
- }
- if(i>1748)
- {
- wordout+="*";
- wordout+=inputword;
- wordout+="*";
- return wordout;
- }
- else
- {
- return elvish_dict[i];
- }
- }
- string dict::lookup_elvish(const std::string inputword)
- {
- string wordout;
- int i=0;
- while(!(inputword==elvish_dict[i]))
- {
- i++;
- if(i>1748)
- break;
- }
- if(i>1748)
- {
- wordout+="*";
- wordout+=inputword;
- wordout+="*";
- return wordout;
- }
- else
- {
- return english_dict[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement