LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2003, 03:25 AM   #1
calypso_craig
LQ Newbie
 
Registered: Oct 2003
Location: shoalhaven heads, australia
Posts: 3

Rep: Reputation: 0
cant get main to call functions, unsure of parameters. c++


seems like a simple problem to call the functions in my program but i can't figure out the parameters to use. i believe it has to deal with using new nodes and pointers. problem lies in the main function, trying to create the avl tree with the dictionary

#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <string>
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>

using namespace std;

typedef struct NODE_avl
{
char *info;
char isdeleted;
struct NODE_avl * left;
struct NODE_avl * right;
int height;
} NODE_AVL, * AVL;

int h(NODE_AVL *a);
void s_rotation_right(NODE_AVL *k2);
void s_rotation_left(NODE_AVL *k2);
NODE_AVL * search_(char *x,NODE_AVL *a);
void saving_AVL_to_file(NODE_AVL *a);
void go_through(FILE *f,NODE_AVL *a);
void create_AVL_from_file(NODE_AVL *a);
void menu();

void main()
{
menu();
int a = NULL;
malloc(a*sizeof(NODE_AVL));
create_AVL_from_file(NODE_avl);
}

void d_rotation_left(NODE_AVL *k3)
{
s_rotation_right(k3->left);
s_rotation_left(k3);
}

void d_rotation_right(NODE_AVL *k3)
{
s_rotation_left(k3->right);
s_rotation_right(k3);
}

void s_rotation_left(NODE_AVL *k2)
{
NODE_AVL *k1;
k1= k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = (h(k2->left), h(k2->right)) + 1;
k1->height = (h(k1->left), k2->height) + 1;
k2 = k1;
}

void s_rotation_right(NODE_AVL *k2)
{
NODE_AVL *k1;
k1= k2->right;
k2->right = k1->left;
k1->left = k2;
k2->height = (h(k2->right), h(k2->left)) + 1;
k1->height = (h(k1->right), k2->height) + 1;
k2 = k1;
}

int h(NODE_AVL *a)
{
if (a == NULL)
return -1;
else
return a->height;
}

void insert(char* x,NODE_AVL *a)
{
int err;
err = 0;
if (a == NULL)
/*{//create a new tree
a=malloc(sizeof(NODE_AVL));
if (a == NULL)
err = 1; //eroare_depasire_heap;
else*/
{
a->info = x;
a->isdeleted=0;
a->left = NULL;
a->right = NULL;
a->height = 0;
}
//}
else
{
if (strcmp(x,a->info)>0)
{
insert(x,a->left);
if ( h(a->left)-h(a->right) == 2)

if (strcmp(x,a->left->info)>0)
s_rotation_left(a);
else
d_rotation_right(a);
else
a->height=(h(a->left),h(a->right))+1;
}
if (strcmp(x,a->info)<0)
{
insert(x,a->right);
if ( h(a->right)-h(a->left) == 2)
{
if (strcmp(x,a->right->info)>0)
s_rotation_right(a);
else
d_rotation_left(a);
}
else
a->height=(h(a->right),h(a->left))+1;
}
}
}

int idelete(char *x,NODE_AVL *a)
{
NODE_AVL* NODEe;
NODEe = search_(x,a);
if (NODEe == NULL) return -1;
a->isdeleted=1;
return 1;
}

NODE_AVL * search_(char *x,NODE_AVL *a)
{
if (a == NULL)
return NULL;
if (strcmp(x,a->info)>0)
return search_(x,a->left);
else
if (strcmp(x,a->info)<0)
return search_(x,a->right);
else
return a;
}

void saving_AVL_to_file(NODE_AVL *a)
{
FILE *f;
if ((f = fopen("dictionary.txt","w")) == NULL)
{
printf("Error opening file");
return;
}
go_through(f,a);
}

void go_through(FILE *f,NODE_AVL *a)
{
if (a == NULL) return;
/*here insert the operation for each element;
(Example: for saving the AVL into dictionary write here
fprintf(f,"%s",a->info) where f is the file);
*/
if (!a->isdeleted) fprintf(f,"%s",a->info);
go_through(f,a->left);
go_through(f,a->right);
}

void create_AVL_from_file(NODE_AVL *a)
{
FILE *f;
char *s;
if ((f = fopen("dictionary.txt","r")) == NULL)
{
printf("Error opening file");
return;
}
while(!feof(f))
{
s=(char*)malloc(50);
fscanf(f,"%s",s);
insert(s,a);
}
}

void menu()
{
cout<<"\t\t***************************\n";
cout<<"\t\t\t== MENU ==\n"
<<"\t\t***************************\n"
<< "\nHere are your choices:\n"
<< "\n P: Print the whole dictionary.\n"
<< " A: Add a word into the dictionary.\n"
<< " D: Delete a word from the dictionary.\n"
<< " N: Count how many words contained in the dictionary.\n"
<< " C: Check spelling of a text file provided by the user.\n"
<< " Q: Exit the program.\n";
}
 
Old 10-28-2003, 03:27 AM   #2
ashjam10
Member
 
Registered: Sep 2003
Location: UK
Distribution: Mandrake 10.0
Posts: 121

Rep: Reputation: 15
I may be wrong, I've not done anything with C in 3 years, but don't functions have to be defined BEFORE you can call them? ie. the main function always goes at the end of the program.
 
Old 10-28-2003, 03:36 AM   #3
calypso_craig
LQ Newbie
 
Registered: Oct 2003
Location: shoalhaven heads, australia
Posts: 3

Original Poster
Rep: Reputation: 0
yes ur right, functions are to be defined at the beginning of the program but can be called at any time. the position of the main is irrelevent
 
Old 10-28-2003, 04:10 AM   #4
ashjam10
Member
 
Registered: Sep 2003
Location: UK
Distribution: Mandrake 10.0
Posts: 121

Rep: Reputation: 15
Sorry, must have been thinking of PASCAL
 
Old 10-28-2003, 04:27 AM   #5
calypso_craig
LQ Newbie
 
Registered: Oct 2003
Location: shoalhaven heads, australia
Posts: 3

Original Poster
Rep: Reputation: 0
no wucken furries, thanx anyways
 
Old 10-28-2003, 05:47 AM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Re: cant get main to call functions, unsure of parameters. c++

Quote:
Originally posted by calypso_craig

int a = NULL;
malloc(a*sizeof(NODE_AVL));
I didn't get what you are trying to do . You can't assign NULL to an integer. Malloc is also wrong ....
Code:
int main()
{
        menu();
        AVL root_avl;
        root_avl=(AVL)malloc(sizeof(NODE_AVL));
        create_AVL_from_file(root_avl);
        return 0;
}
From what I have understood from your code the main should be written like this

Let me explain what I have done line by line:-
1.Create a pointer of type NODE_AVL i.e, AVL root_avl;
2.ALlocate memory to it [ malloc does that even new does that but, I haven't ever used "new" ]
3.Then passing it as parameter to create_AVL_from_file

Hope this does it for you
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to call functions aizkorri Programming 7 12-20-2017 01:20 PM
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 02:40 PM
changing objects parameters via getters inside functions in c++? markhod Programming 1 03-08-2005 05:30 AM
libtiff problem: cannot call outside main() proc zhang_mdev Programming 1 12-14-2004 02:17 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration