Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Translator.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <string>
- #include <map>
- #include "fstream"
- #include <iostream>
- using namespace std;
- map<string, string> ed;
- map<string, string> dr;
- void tolow(string* str)
- {
- for (string::iterator it = (*str).begin(); it != (*str).end(); it++)
- {
- *it = tolower(*it);
- }
- }
- string* split(string s, string delimiter)
- {
- size_t pos = 0;
- int k = 0;
- for (int i = 0; i < s.length(); i++)
- {
- if (s[i] == *" ")
- {
- k++;
- }
- }
- string* mass = new string[k+1];
- int kPos = 0;
- while ((pos = s.find(delimiter)) != string::npos) {
- string token = s.substr(0, pos);
- mass[kPos] = token;
- s.erase(0, pos + delimiter.length());
- kPos++;
- }
- mass[kPos] = s;
- return mass;
- }
- void addDict()
- {
- struct stat sb;
- if (stat("edDict.txt", &sb) == 0)
- {
- ofstream ofs("edDict.txt", ios_base::app);
- ofs.close();
- }
- ifstream fin("edDict.txt");
- string enWord;
- fin » enWord;
- while (fin.is_open() && !fin.eof())
- {
- tolow(&enWord);
- string deWord;
- fin » deWord;
- tolow(&deWord);
- ed.insert(make_pair(enWord, deWord));
- fin » enWord;
- }
- fin.close();
- if (stat("drDict.txt", &sb) == 0)
- {
- ofstream ofs("drDict.txt", ios_base::app);
- ofs.close();
- }
- fin.open("drDict.txt");
- string deWord;
- fin » deWord;
- while (fin.is_open() && !fin.eof())
- {
- tolow(&deWord);
- string rusWord;
- fin » rusWord;
- tolow(&rusWord);
- dr.insert(make_pair(deWord, rusWord));
- fin » deWord;
- }
- fin.close();
- }
- void addToDict(string firstWord, string secWord, string dict)
- {
- tolow(&firstWord);
- tolow(&secWord);
- ofstream fout(dict, ios_base::app);
- fout « firstWord « endl « secWord « endl;
- fout.close();
- }
- string* translate(string enPhrase)
- {
- string* str = split(enPhrase, " ");
- string* trans=new string[2];
- int k = 0;
- for (int i = 0; i < enPhrase.length(); i++)
- {
- if (enPhrase[i] == *" ")
- {
- k++;
- }
- }
- for (int i = 0; i<=k; i++) {
- string enWord = str[i];
- tolow(&enWord);
- try {
- string deutchWord;
- deutchWord = ed.at(enWord);
- trans[0] += deutchWord+" ";
- try {
- string rusWord = dr.at(deutchWord);
- trans[1] += rusWord+" ";
- }
- catch (out_of_range e)
- {
- trans[0] += " ... ";
- }
- }
- catch (out_of_range e)
- {
- trans[1] += " ... ";
- }
- }
- return trans;
- }
- void insertED(string enWord, string deutchWord)
- {
- try {
- ed.at(enWord);
- //слово уже существует в словаре
- }
- catch (out_of_range e)
- {
- addToDict(enWord, deutchWord, "edDict.txt");
- ed.insert(make_pair(enWord, deutchWord));
- }
- }
- void insertDR(string deutchWord, string rusWord)
- {
- try {
- dr.at(deutchWord);
- //слово уже существует в словаре
- }
- catch (out_of_range e)
- {
- addToDict(deutchWord, rusWord, "drDict.txt");
- dr.insert(make_pair(deutchWord, rusWord));
- }
- }
- int main(int argc, char *argv[])
- {
- addDict();
- while (true)
- {
- string com;
- cin » com;
- if (com == "t")
- {
- string phrase;
- cin » phrase;
- string* trans=translate(phrase);
- cout « " translated russ: " « trans[1] « endl;
- }
- else if (com=="aed"){
- string fw;
- string sw;
- cin » fw;
- cin » sw;
- insertED(fw, sw);
- cout « "inserted";
- }
- else if (com == "adr")
- {
- string fw;
- string sw;
- cin » fw;
- cin » sw;
- insertDR(fw, sw);
- cout « "inserted";
- }
- }
- return 0;
- }
- 01:49:19
- Денис
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement