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 this question and send me the video clip how to do it This...
Please solve me this question and send me the video clip how to do it This problem contains loops, sorting arrays, random generation, and variable matching. Include an analysis (data requirements) and algorithm in your reply. Write a “lottery engine” program to simulate drawing lottery numbers. You will draw 7 numbers, randomly selected from 1 to 16. a) Allow the user to enter their own selection of numbers first, Example numbers: 2 3 7 9 10 11 15 b) then...
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 identify each error present, explain the type of error (syntax/logic/run-time), and provide a fix which...
Please identify each error present, explain the type of error (syntax/logic/run-time), and provide a fix which changes the given code as little as possible (i.e. don't rewrite the whole thing; just point out what is wrong and a fix for it). 5) // Assign a grade based on the score char score; cin>>score; if score < 0 && score > 100     cout<<"The score entered is invalid"<<endl; if (score <= 90)     grade = "A"; if (score <= 80)    ...
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 what is wrong with this two code that is showing error message when run them?...
Please what is wrong with this two code that is showing error message when run them? >>>data =[1,2,3,4] >>>reduce(lambda x,y:x+y,data) #Produce the sum 10 >>> reduce(lambda x,y:x*y, data) # Produce the product 24 ___________________________________________________________________________________________________________- def sum(lower,upper): “””Returns the sum of the numbers from lower to upper.””” if lower> upper: return 0 else: return reduce(lambda x,y:x+y, range(lower, upper+1))
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT