file: latin.cpp
Code:
#include <string>
#include <fstream>
#include <iostream>
#define LATIN_FILE "latin.txt"
#define ENGLISH_FILE "english.txt"
using namespace std;
struct tree
{
string english;
string latin;
tree *left;
tree *right;
tree(string eng = "", string lat = "")
{
english = eng;
latin = lat;
left = NULL;
right = NULL;
}
};
bool exists(tree *root, string english, string latin)
{
if (root == NULL)
{
return false;
}
else if (english == root->english)
{
root->latin = latin + ", " + root->latin;
return true;
}
else if (english < root->english)
{
return exists(root->left, english, latin);
}
else
{
return exists(root->right, english, latin);
}
}
void insert(tree *&root, string english, string latin)
{
if (root == NULL)
{
root = new tree(english, latin);
}
else
{
if (!exists(root, english, latin))
{
if (english < root->english)
{
insert(root->left, english, latin);
}
else
{
insert(root->right, english, latin);
}
}
}
}
void print(tree *root, ofstream &output)
{
if (root != NULL)
{
print(root->left, output);
output << root->english << ": " << root->latin << endl;
print(root->right, output);
}
}
void reset(tree *&root)
{
if (root->left != NULL)
{
reset(root->left);
}
if (root->right != NULL)
{
reset(root->right);
}
delete root;
root = NULL;
}
string trim(const string &line)
{
size_t start = line.find_first_not_of(" \t\r\n");
if (start != string::npos)
{
return line.substr(start, line.find_last_not_of(" \t\r\n") - start + 1);
}
return "";
}
int main(void)
{
tree *root;
string unit;
string line;
bool start = false;
root = NULL;
remove(ENGLISH_FILE);
ifstream input(LATIN_FILE);
ofstream output(ENGLISH_FILE);
while (getline(input, line))
{
line = trim(line);
if (!line.empty())
{
if (line.substr(0, 1) == "%")
{
if (root != NULL)
{
if (!start)
{
start = true;
}
else
{
output << endl;
}
output << unit << endl;
print(root, output);
reset(root);
unit = "";
}
unit = line;
}
else
{
string word;
string latin;
string english;
for (size_t index = 0; index < line.length(); index++)
{
string character = line.substr(index, 1);
if (character == ":")
{
word = trim(word);
if (!word.empty())
{
latin = trim(word);
word = "";
}
}
else if (character == ",")
{
if (!latin.empty())
{
english = trim(word);
if (!english.empty())
{
insert(root, english, latin);
word = "";
}
}
}
else
{
word += character;
}
}
if (!latin.empty())
{
english = trim(word);
if (!english.empty())
{
insert(root, english, latin);
}
}
}
}
}
if (root != NULL)
{
if (start)
{
output << endl;
}
output << unit << endl;
print(root, output);
reset(root);
}
output.close();
input.close();
return 0;
}
file: latin.txt
Code:
% Unit 5
ante: before, in front of, previously
antiquus: ancient
ardeo: burn, be in fire, desire
arma: arms, weapons
aurum: gold
aureus: golden, of gold
% Unit 6
animal: animal
Athenae: Athens
atque: and
ac: and
aurora: dawn
% Unit 7
amo: love
amor: love
annus: year
Asia: Asia
i-compile na ang latin.cpp then i-save na ang file nga latin.txt sa directory kung asa ang executable file nakabutang nga resulta sa pag-compile. after ana pwede na siya ma-run, kanang latin.txt kay mao nay input file sa executable. after ana ug run, naay file nga ma-create same directory sa executable nga ang filename kay english.txt which is the final output. instead nga sa screen nko gi-output kay sa file na lang nko gi-save kay i'm sure kabalo nka unsaon pag-output sa screen, mas beneficial kung sa file padulong kay at least intro pud sa file input/output, dili ra kay puro data structs. anyway, dali ra man pud na usbon kung ganahan ka.
i-check lang kung mao ba ang output. gcc sa linux akong gigamit pero mo-run na bisan asa basta ang c++ compiler nga gamiton kay modern lang. dili katong panahon pa sa mga dinosaur. joke.
ako na lang pud gi-upload ang files nga naka-zip na... http://www.mediafire.com/?v8mbac35b1da0g6 <-- download then unzip, then i-open ang latin.cpp using dev-c++, compile then run.