Речник С++.

C/C++, Visual Basic, Pascal и други...
Post Reply
User avatar
rogue
V.I.P
V.I.P
Posts: 371
Joined: 06 Feb 2009, 11:17
Favorite version: CS: 1.6
Location: София
Contact:

Речник С++.

Post by rogue »

Здравейте, момчета и момичета!

Отдавна не бях писал тук, но се сетих, че има добри програмисти на С++. Поне по мое време имаше, когато бях редовен. Не ми е сила системното програмиране, но имам един task, с който се боря. Очевидно, неуспешно до този момент. Ако някой помогне, с радост ще го почерпя една бира :) Та, таскът е следният:

Command-line dictionary
Write a program command-line dictionary. The program represents a simple off-line English-Bulgarian dictionary. The dictionary works in command line regime (terminal). The correspondence between the words is kept in two text files: one for translation from English to Bulgarian, and the other – for Bulgarian to English. The program must contain at least one class.
User avatar
Misuri
Misuri
Misuri
Posts: 140
Joined: 07 Sep 2010, 23:55
CS Nick: Morpheus.
Favorite version: CS: 1.6

Re: Речник С++.

Post by Misuri »

Идеята е следната - един файлов поток, четеш файла , думите са на .txt файл от сорта на tent - палатка и всяка дума е на нов ред.Като искат превод от bg на en, намираш думата, като зимаш целия ред като string, после с substring четеш ред-а след -+1character и това е превода до края на реда.
Аналогично от EN на BG е същото, само че цикъла ще се върти отзад напред.

abacus - абак, вид сметало
abandon - напуска, изоставям

Примерно - на програмата подаваш думата от командния ред, чете се файла, ако намери думата изписва превода, ако не я намери или е грешна - съответното съобщение.
User avatar
rogue
V.I.P
V.I.P
Posts: 371
Joined: 06 Feb 2009, 11:17
Favorite version: CS: 1.6
Location: София
Contact:

Re: Речник С++.

Post by rogue »

Това и аз го видях в нета като коментар :D
User avatar
rogue
V.I.P
V.I.P
Posts: 371
Joined: 06 Feb 2009, 11:17
Favorite version: CS: 1.6
Location: София
Contact:

Re: Речник С++.

Post by rogue »

Програмката се оказа приятна. Направих я, предоставям и сорса, ако на някой някога му потрябва. Иначе - затваряйте темата!

Code: Select all

#include <iostream>
#include "string"
#include <fstream>
#include <vector>


using namespace std;

class Dictionary {
private:
vector<string> bg_words;
vector<string> en_words;
void load_bg_lang_source_file( string source_file ) {
   ifstream infile( source_file );
   string word;
   while (infile >> word ) {
      bg_words.push_back( word );
   }
}

void load_en_lang_source_file( string source_file ) {
   ifstream infile( source_file );
   string word;
   while (infile >> word ) {
      en_words.push_back( word );
   }
}

public:
Dictionary( string bg_word_file, string en_word_file ) {
   load_bg_lang_source_file( bg_word_file );
   load_en_lang_source_file( en_word_file );

}

string translate_en_to_bg( string word ) {
   int index = get_index_for_word( en_words, word );
   if ( index == -1 ) {
      cout << "Error: not existing translation for: " << word << endl;
      return "";
   }
   return bg_words[index];
}

string translate_bg_to_en( string word ) {
   int index = get_index_for_word( bg_words, word );
   if ( index == -1 ) {
      cout << "Error: not existing translation for: " << word << endl;
      return "";
   }
   return en_words[index];
}

int get_index_for_word( vector<string> word_list, string search_for ) {
   for ( int i = 0; i < word_list.size(); i++) {
      string current_word = word_list[i];
      if (  current_word == search_for ) {
         return i;
      }
   }
   return -1;
}

};

int main()
{
   int is_bg_to_en = 0;
   cout << "for bg to en enter 1. enter 0 for en to bg: ";
   cin >> is_bg_to_en;

   string word_to_translate;
   cout << "input a word you want translated:";
   cin >> word_to_translate;

   Dictionary d( "bg_dictionary.txt", "en_dictionary.txt" );
   string translated_word;
   if ( is_bg_to_en ) {
      translated_word = d.translate_bg_to_en( word_to_translate );
   } else {
      translated_word = d.translate_en_to_bg( word_to_translate );
   }

   cout << "Translation for: " << word_to_translate << " is: " << translated_word << endl;
}
Post Reply

Return to “Системно програмиране”