stablo.h
###################
#include<iostream>
using namespace std;
#include <string>
struct tnode{
char *bosanski,*engleski;
struct tnode *left,*right;
};
class BinaryTree
{
private:
struct tnode *root;
struct tnode * addtreework(struct tnode *,char *, char *);
struct tnode * searchtreework(struct tnode *,char *);
int treesizework(struct tnode *);
void treeprintinorderwork(struct tnode *);
void treeprintpreorderwork(struct tnode *);
void treeprintpostorderwork(struct tnode *);
void treedelete(struct tnode *);
public:
BinaryTree()
{
root=NULL;
}
~BinaryTree()
{
treedelete(root);
}
void addtree(char *, char *);
struct tnode* searchtree(char *);
int treesize(void);
void treeprintinorder();
void treeprintpostorder();
void treeprintpreorder();
};
###################################
stablo.cpp
##################################
#include "stablo.h"
#include<iostream>
#include<string>
using namespace std;
struct tnode * BinaryTree::addtreework(struct tnode *p,char *bos, char *eng)
{
int cond;
char *b,*e;
if(p==NULL)
{
p=new struct tnode;
b=new char[strlen(bos)+1];
e=new char[strlen(eng)+1];
strcpy(b,bos);
strcpy(e,eng);
p->bosanski=b;
p->engleski=e;
p->left=NULL;
p->right=NULL;
}
if((cond=strcmp(bos,p->bosanski))==0);
else if(cond<0)
p->left=addtreework(p->left,bos,eng);
else
p->right=addtreework(p->right,bos,eng);
return p;
}
void BinaryTree::addtree(char *bos, char *eng)
{
root=addtreework(root,bos,eng);
}
struct tnode * BinaryTree::searchtreework(struct tnode *p,char *word)
{
int cond;
if(p==NULL);
else if(((cond=strcmp(p->bosanski,word))&&(cond=strcmp(p->engleski,word)))==0);
else if(cond<0)
p=searchtreework(p->left,word);
else
p=searchtreework(p->right,word);
return p;
}
struct tnode * BinaryTree::searchtree(char *word)
{
struct tnode *ptr;
ptr=searchtreework(root,word);
return ptr;
}
void BinaryTree::treedelete(struct tnode *p)
{
if(p!=NULL)
{
treedelete(p->left);
treedelete(p->right);
cout<<" BRISEM:\t "<<p->bosanski<<"\t"<<p->engleski<<endl;
delete p;
}
}
int BinaryTree::treesizework(struct tnode *p)
{
if (p == NULL)
return 0;
else
return (1 + treesizework(p->left) + treesizework(p->right));
}
int BinaryTree::treesize(void)
{
int i;
i = treesizework(root);
return i;
}
void BinaryTree::treeprintinorderwork(struct tnode * p)
{
if( p != NULL)
{
treeprintinorderwork(p->left);
cout<<p->bosanski<<"\t"<<p->engleski<<endl;
treeprintinorderwork(p->right);
}
}
void BinaryTree::treeprintinorder(void)
{
treeprintinorderwork(root);
}
void BinaryTree::treeprintpreorderwork(struct tnode * p)
{
if(p != NULL)
{
cout<<"\n"<<p->bosanski<<"\t"<<p->engleski<<endl;
treeprintpreorderwork(p->left);
treeprintpreorderwork(p->right);
}
}
void BinaryTree::treeprintpreorder(void)
{
treeprintpreorderwork(root);
}
void BinaryTree::treeprintpostorderwork(struct tnode * p)
{
if(p != NULL)
{
treeprintpostorderwork(p->left);
treeprintpostorderwork(p->right);
cout<<"\n"<<p->bosanski<<"\t"<<p->engleski<<endl;
}
}
void BinaryTree::treeprintpostorder(void)
{
treeprintpostorderwork(root);
}
############################
main_programm
###########################
#include "stablo.h"
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
BinaryTree rijecnik;
tnode *ptr;
int izbor=1;
//i varijabla sluzi samo na kraju da ustavi program
rijecnik.addtree("kuca","house");
rijecnik.addtree("magarac","donkey");
rijecnik.addtree("mama","mother");
rijecnik.addtree("tata","dady");
rijecnik.addtree("zadaca","homework");
//unos par rijeci unutar rijecnika da nije prazan
do{
cout<<"::::::::::::::BOSANSKO-ENGLESKI,ENGLESKO-BOSANSKI Rijecnik::::::::::::::"<<endl;
cout<<":::::::::::::: ::::::::::::::"<<endl;
cout<<"::::::::::::::....................MENI....................::::::::::::::"<<endl;
cout<<endl;
cout<<" #1.Bosansko-Engleski Rijecnik"<<endl;
cout<<" #2.Englesko-Bosanski Rijecnik"<<endl;
cout<<" #3.Dodati nove rijeci u rijecnik"<<endl;
cout<<" #4.Izlistaj sve rijeci u rijecniku"<<endl;
cout<<" #5.Quit"<<endl;
cout<<"\n\n";
cout<<"Vas izbor:\t";
cin>>izbor;
switch (izbor)
{
case 1:
{
cout<<"\n\n\n--------------------------------------------------------------------------------";
char *r=new char[25]; //moglo je i sa stringom
cout<<"Prevedi rijec:\t";
cin>>r;
cout<<endl;
ptr=rijecnik.searchtree(r);
delete r;
if(ptr!=NULL) //u slucaju da nema rezultata ptr prima vrijednost NULL i kao takava ne moze isprintat nista sto dovodi do prekida programa
cout<<ptr->bosanski<<"\t"<<ptr->engleski<<endl;
else
cerr<<"nema takva rijec u rijecniku"<<endl;
cout<<"--------------------------------------------------------------------------------\n\n"<<endl;
}break;
case 2:
{
cout<<"\n\n\n--------------------------------------------------------------------------------";
char *r=new char[25];
cout<<"Prevedi rijec:\t";
cin>>r;
cout<<endl;
ptr=rijecnik.searchtree(r);
delete r;
if(ptr!=NULL)
cout<<ptr->engleski<<"\t"<<ptr->bosanski<<endl;
else
cerr<<"nema takva rijec u rijecniku"<<endl;
cout<<"--------------------------------------------------------------------------------\n\n"<<endl;
}break;
case 3:
{
cout<<"\n\n\n--------------------------------------------------------------------------------";
char *rijecbos=new char[25];
char *rijeceng=new char[25];
cout<<"bos:\t";
cin>>rijecbos;
cout<<"\nend:\t";
cin>>rijeceng;
rijecnik.addtree(rijecbos,rijeceng);
delete rijecbos;
delete rijeceng;
cout<<"--------------------------------------------------------------------------------\n\n"<<endl;
}break;
case 4:
{
cout<<"\n\n\n--------------------------------------------------------------------------------";
cout<<"Velicina stabla: "<<rijecnik.treesize()<<endl;
cout<<endl;
rijecnik.treeprintinorder();
cout<<"--------------------------------------------------------------------------------\n\n"<<endl;
}break;
}
}while(izbor!=5);
return 0;
}
####################################
pitanje:
kako da smijestam ove rijeci u neki file na disku
znam da je
file *dat;
fwrite("c:\\dat.bin",sizeof(struct tnode),1,dat);
ono sto mi je potrebno za pisanje
Medjutim ja nemam vishe struktura nego samo pointer na memorijsku lokaciju gdje je alocirana struktura, i da napravim rucno neki ja rijecnik ne znam onda kako cu ucitavati tako da ove funkcije mogu citati iz tog fila i pretrazivati sve to.
pomoc!!
refa