Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "translator.h"
- #include <iostream>
- using namespace std;
- #include <fstream>
- #include <cstring>
- #include <string>
- #include <algorithm>
- Dictionary::Dictionary(const char dictFileName[])
- {
- fstream str;
- str.open(dictFileName, ios::in);
- int i=0;
- while (!str.eof())
- {
- str >> englishWord[i];// Here says take first word slot into english array
- str >> elvishWord[i];
- i++;
- }
- numEntries=i;
- }
- Translator::Translator(const char dictFileName[]) : dict(dictFileName)// This is the constructor with an initalizer list
- {
- }
- void Translator::toElvish(char out_s[], const char s[])
- {
- int len = strlen(s);
- int i=0;
- do{out_s[i] ='\0';i++;}while(i<MAX_WORD_LEN);
- char temp[MAX_WORD_LEN] = {'\0'};
- char returned[MAX_WORD_LEN] = {'\0'};
- string str2;
- for (int a = 0; a < len; a++)
- {
- // if it has punctuation should put it into the outer array
- int b = 0;
- /* if(temp[b] ='.'||','||'!')
- { temp[b] = '.'||','||'!';
- }
- b = 0;*/
- while(s[a] != ' '&&s[a] != ','&&s[a] !='.')// Check is this is a cpitals if so store infro in Boolen
- {// is alpha sa pr sa you -
- temp[b] = s[a];
- // check words to see if they
- if (temp[0]>='A' && temp[0]<='Z')
- {temp[0]= temp[0]-'A' + 'a';}
- b++;
- a++;
- }
- // cycle through the letters and check for caps first letter / after .
- temp[b] = '\0';
- dict.translate(returned, temp);
- str2 = str2 + returned;
- if(s[a] == ' ' || s[a] == ','||s[a] == '.'||s[a] == '-') // Did we have captial at point in word if so make cpital this postion
- {
- //cout << s[a]<<endl;
- str2 = str2 + s[a];
- //cout << str2<<endl;}
- strcpy(out_s, str2.c_str());
- // cout << <<endl;
- }
- }
- }
- void Translator::toEnglish(char out_s[], const char s[])
- {
- int len = strlen(s);
- int i=0;
- do{out_s[i] ='\0';i++;}while(i<MAX_WORD_LEN);
- char temp[MAX_WORD_LEN] = {'\0'};
- char returned[MAX_WORD_LEN] = {'\0'};
- string str2;
- //const char *cs;
- for (int a = 0; a < len; a++)
- {
- if(s[a]!=' ')
- {
- int b = 0;
- while(s[a] != ' '&&s[a] != ','&&s[a] !='.')
- {
- if (temp[0]>='A' && temp[0]<='Z')
- {temp[0]= temp[0]-'A' + 'a';}
- temp[b] = s[a];
- b++;
- a++;
- }
- temp[b] = '\0';
- dict.translate2(returned, temp);
- str2 = str2 + returned;
- if(s[a] == ' ' || s[a] == ','||s[a] == '.'||s[a] == '-')
- str2 = str2 + s[a];
- strcpy(out_s, str2.c_str());
- }
- }
- }
- void Dictionary::translate(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
- {
- int i = 0;
- for (i=0;i<numEntries;i++)
- {
- if (strcmp(englishWord[i], s)==0)
- break;
- // cout << 1 << endl;
- }
- if(i<numEntries)
- {
- strcpy(out_s, elvishWord[i]);
- //cout << 2 << endl;
- }
- else{
- string not_found;
- cout << 3 << endl;
- not_found = '*' + s + '*';
- cout << 4 << endl;
- strcpy(out_s, not_found.c_str());
- cout << 5 << endl;
- }
- // what do I do if I didn't find the word?
- // might want another parameter or return value what
- // indicates that the word wasn't found
- }
- void Dictionary::translate2(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
- {
- int i = 0;
- for (i=0;i<numEntries;i++)
- {
- if (strcmp(elvishWord[i], s)==0)
- break;
- }
- if ( i<numEntries)
- {
- strcpy(out_s, elvishWord[i]);
- // what do I do if I didn't find the word?
- // might want another parameter or return value what
- // indicates that the word wasn't found
- }
- else{
- string not_found;
- cout << 3 << endl;
- not_found = '*' + s + '*';
- cout << 4 << endl;
- strcpy(out_s, not_found.c_str());
- cout << 5 << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment