Question

In: Computer Science

It have a lot of error. please see it and solve it for me. run it...

It have a lot of error. please see it and solve it for me. run it please. Thank you a lot.

#define MAXWORDLENGTH 20

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include "getline.inc"

struct entry {

char name[81];

};

struct entry part;

struct tnode {

char *name;

int record_number;

struct tnode *left;

struct tnode *right;

};

void lookup(struct tnode *root,char *s);

void treeprint2(struct tnode *root,char *searchkey);

int c;

int n;

FILE *stream;

struct tnode *root, *tree(struct tnode *, char *, int),*dtree(struct tnode *, char *);

void main()

{

char searchkey[81];

char name[MAXWORDLENGTH];

int i=0;

root=NULL;

stream = fopen("data.bin","r+b");

for (;;) {

//printf("enter a name or 'exit' to quit.... ");

//getline(name,81);

//scanf(" %[^\n]",name);

n=fread(&part,sizeof(part),1,stream);

if(!n) break;

//if (stricmp(name,"exit")==0) break;

//root=tree(root,name,i++);

root = tree(root,part.name,i++);

}

treeprint(root);

for(;;){

printf("\n\n\t enter the name to delete or exit");

scanf(" %[^\n]",searchkey);

if(stricmp(searchkey,"exit")==0) break;

root=dtree(root,searchkey);

treeprint(root);

}

for(;;){

printf("\n\n\t enter the name to lookup or exit");

scanf(" %[^\n]",searchkey);

if(stricmp(searchkey,"exit")==0) break;

lookup(root,searchkey);

}

fclose(stream);

}//main

struct tnode *tree(struct tnode *p,char *w,int w2)

{

struct tnode *talloc();

char *strsave(char *);

int cond;

if (p== NULL) {

p=talloc();

p->name=strsave(w);

p->record_number=w2;

p->left=p->right=NULL;

} else if ((cond=stricmp(w,p->name)) ==0)

//printf("\7 duplicate entry !!!!!! \n");

p->right=tree(p->right,w,w2);

else if (cond<0)

p->left=tree(p->left,w,w2);

else

p->right=tree(p->right,w,w2);

return(p);

}// tree

treeprint(struct tnode *root)

{

if (root) {

treeprint(root->left);

printf("\n\n %s %d",root->name,root->record_number);

treeprint(root->right);

}//if

}// treeprint

struct tnode *talloc()

{

return((struct tnode *) malloc(sizeof(struct tnode)));

}

char *strsave(char *s)

{

char *p;

if ((p=malloc(strlen(s)+1)) != NULL)

strcpy(p,s);

return(p);

}

struct tnode *dtree(struct tnode *root,char *searchkey)

{struct tnode *p,*p2;

if (root){

if (stricmp(root->name,searchkey)==0) {

if (root->left == root->right) {free(root);return(NULL);}

else if (root->left==NULL) {p=root->right;free(root);return(p);}

else if (root->right==NULL) {p=root->left;free(root);return(p);}

else {

p2=root->right;

p=root->right;

while(p->left) p=p->left;

p->left=root->left;

free(root);

return(p2);

}

}

if (stricmp(root->name,searchkey) < 0) root->right=dtree(root->right,searchkey);

else root->left=dtree(root->left,searchkey);

return(root);

}

else {printf("\n\n\t\a %s not found !!!\n\n",searchkey);return(NULL);}

}//treedel

void lookup(struct tnode *p,char *s)

{

if(!p)

{printf("\n\a %s not found",s);return;}

else if (strncmpi(s,p->name,strlen(s))==0)

{

fseek(stream,p->record_number *sizeof(part),0);

fread(&part,sizeof(part),1,stream);

printf("\n %s %d",part.name,p->record_number);

puts("\n (y) or (n)????");

fflush(stdin);

c = getchar();

fflush(stdin);

if(c!='y')

//lookup(p->right,s);

treeprint2(p->right,s);

}//else

else if (strncmpi(s,p->name,strlen(s))<0)

lookup(p->left,s);

else lookup(p->right,s);

}//lookup

void treeprint2(struct tnode *root,char *searchkey)

{

if (root) {

treeprint2(root->left,searchkey);

if (strncmpi(searchkey,root->name,strlen(searchkey))==0) printf("\n\n %s %d",root->name,root->record_number);

treeprint2(root->right,searchkey);

}//if

}// treeprint

It have a lot of error. please see it and solve it for me. Thank you a lot.

Solutions

Expert Solution

I have done some changes so the errors are gone. If you have any doubts please feel free to ask.

#define MAXWORDLENGTH 20

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

//#include "getline.inc"

struct entry {

char name[81];

};

struct entry part;

struct tnode {

char *name;

int record_number;

struct tnode *left;

struct tnode *right;

};

void lookup(struct tnode *root,char *s);

void treeprint2(struct tnode *root,char *searchkey);

int c;

int n;

FILE *stream;

struct tnode *root, *tree(struct tnode *, char *, int),*dtree(struct tnode *, char *);

void main()

{

char searchkey[81];

char name[MAXWORDLENGTH];

int i=0;

root=NULL;

stream = fopen("data.bin","r+b");

for (;;) {

//printf("enter a name or 'exit' to quit.... ");

//getline(name,81);

//scanf(" %[^\n]",name);

n=fread(&part,sizeof(part),1,stream);

if(!n) break;

//if (strcmp(name,"exit")==0) break;

//root=tree(root,name,i++);

root = tree(root,part.name,i++);

}

treeprint(root);

for(;;){

printf("\n\n\t enter the name to delete or exit");

scanf(" %[^\n]",searchkey);

if(strcmp(searchkey,"exit")==0) break;

root=dtree(root,searchkey);

treeprint(root);

}

for(;;){

printf("\n\n\t enter the name to lookup or exit");

scanf(" %[^\n]",searchkey);

if(strcmp(searchkey,"exit")==0) break;

lookup(root,searchkey);

}

fclose(stream);

}//main

struct tnode *tree(struct tnode *p,char *w,int w2)

{

struct tnode *talloc();

char *strsave(char *);

int cond;

if (p== NULL) {

p=talloc();

p->name=strsave(w);

p->record_number=w2;

p->left=p->right=NULL;

} else if ((cond=strcmp(w,p->name)) ==0)

//printf("\7 duplicate entry !!!!!! \n");

p->right=tree(p->right,w,w2);

else if (cond<0)

p->left=tree(p->left,w,w2);

else

p->right=tree(p->right,w,w2);

return(p);

}// tree

treeprint(struct tnode *root)

{

if (root) {

treeprint(root->left);

printf("\n\n %s %d",root->name,root->record_number);

treeprint(root->right);

}//if

}// treeprint

struct tnode *talloc()

{

return((struct tnode *) malloc(sizeof(struct tnode)));

}

char *strsave(char *s)

{

char *p;

if ((p=malloc(strlen(s)+1)) != NULL)

strcpy(p,s);

return(p);

}

struct tnode *dtree(struct tnode *root,char *searchkey)

{struct tnode *p,*p2;

if (root){

if (strcmp(root->name,searchkey)==0) {

if (root->left == root->right) {free(root);return(NULL);}

else if (root->left==NULL) {p=root->right;free(root);return(p);}

else if (root->right==NULL) {p=root->left;free(root);return(p);}

else {

p2=root->right;

p=root->right;

while(p->left) p=p->left;

p->left=root->left;

free(root);

return(p2);

}

}

if (strcmp(root->name,searchkey) < 0) root->right=dtree(root->right,searchkey);

else root->left=dtree(root->left,searchkey);

return(root);

}

else {printf("\n\n\t\a %s not found !!!\n\n",searchkey);return(NULL);}

}//treedel

void lookup(struct tnode *p,char *s)

{

if(!p)

{printf("\n\a %s not found",s);return;}

else if (strcmp(s,p->name)&&(strlen(s))==0)

{

fseek(stream,p->record_number *sizeof(part),0);

fread(&part,sizeof(part),1,stream);

printf("\n %s %d",part.name,p->record_number);

puts("\n (y) or (n)????");

fflush(stdin);

c = getchar();

fflush(stdin);

if(c!='y')

//lookup(p->right,s);

treeprint2(p->right,s);

}//else

else if (strcmp(s,p->name)&&(strlen(s))<0)

lookup(p->left,s);

else lookup(p->right,s);

}//lookup

void treeprint2(struct tnode *root,char *searchkey)

{

if (root) {

treeprint2(root->left,searchkey);

if (strcmp(searchkey,root->name)&&(strlen(searchkey))==0) printf("\n\n %s %d",root->name,root->record_number);

treeprint2(root->right,searchkey);

}//if

}// treeprint


Related Solutions

when i run the program on eclipse it gives me this error: Exception in thread "main"...
when i run the program on eclipse it gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at SIM.main(SIM.java:12) how do I fix that ? (please fix it ) import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.lang.Math; public class SIM{    public static void main(String[] args) throws FileNotFoundException {       int cacheSize = Integer.parseInt( args[1] ); int assoc = Integer.parseInt( args[2] ); int replacement = Integer.parseInt(...
Please, solve me these Qs quickly ..I have Exame. Provide a numerical evaluation of the density...
Please, solve me these Qs quickly ..I have Exame. Provide a numerical evaluation of the density of states for electrons in a bulk material with me =0.06m0 and with an energy 100 meV above the conduction band edge. Express your answer in units of eV-1 cm-3. Estimate the total number of states present per unit energy in a microscopic a =1μm InAs cube. The effective electron mass me =0.023m0 and the electron energy of interest is 100meV
Please walk me through stepp by sstep houw you get the answer and a lot of...
Please walk me through stepp by sstep houw you get the answer and a lot of explainng lpease home / study / business / finance / finance questions and answers / (pleqase explain througougly with sentences and correct numbers)(need to walk me step by step ... Question: (pleqase explain througougly with sentences and correct numbers)(need to walk me step by step how... (pleqase explain througougly with sentences and correct numbers)(need to walk me step by step how got the answer)...
can you please solve this for me. please write a paragraph for each question and explain...
can you please solve this for me. please write a paragraph for each question and explain your reasoning. 1) assume that you are a full-time worker earning $10/hour, $80/day, $400/week, $20000/year. would you be willing to quit your jobs or keep working if the tax rate was 10%? 20%? 30%? 40%? what do you think is the best tax rate? what do you think is the best tax rate? 2)the state of California has decided to increase funding for public...
Please give me examples on innovative solutions to solve as a internal control analyst? Please be...
Please give me examples on innovative solutions to solve as a internal control analyst? Please be detailed?
Please tell me how to solve the following questions a) to c) using a graph. In...
Please tell me how to solve the following questions a) to c) using a graph. In particular, please elaborate on c). Cellwave is a cellular phone company. Answer the following questions relating to its pricing policies: a) When Cellwave started, it sold to a group of homogeneous retail customers. Each person’s monthly demand for cell phone minutes was given by P = $2 - 0.02Q , where P = the price per minute and Q = the quantity of minutes...
Please give me a right answer please if you cant answer it let someone else solve...
Please give me a right answer please if you cant answer it let someone else solve it Margin of Safety a. If Canace Company, with a break-even point at $608,000 of sales, has actual sales of $800,000, what is the margin of safety expressed (1) in dollars and (2) as a percentage of sales? Round the percentage to the nearest whole number. 1. $ 2.   % b. If the margin of safety for Canace Company was 30%, fixed costs were...
Please type responses as it is difficult for me to see written responses. Thank you 1)...
Please type responses as it is difficult for me to see written responses. Thank you 1) How does understanding the correlation between variables help us understand regression? 2) Provide two ways on how you might use regression while teaching a classroom of children. 3) What is one easy way to remember the differences between parametric and nonparametric tests? (Note: I'm not asking you to list the differences but HOW you can remember the differences. Feel free to be creative here...
Ever see a spreadsheet that has a lot of ######## so you can't see the numbers...
Ever see a spreadsheet that has a lot of ######## so you can't see the numbers in a specific cell? This means your number is longer than the visible width of the cell.  How do you widen the cell so you can see all of the numbers by pointing and clicking? How do you do this for a whole set of columns that are too narrow?
Balance the equation AsO3 + NO3^- ------------- H3AsO4 + NO Please show me how you solve...
Balance the equation AsO3 + NO3^- ------------- H3AsO4 + NO Please show me how you solve it. I tried doing it, but im getting stuck on the last couple steps. Thank you
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT