View Single Post
Old 04-20-2003, 03:50 PM   #1 (permalink)
kashif
Registered User
 
Join Date: Apr 2003
Posts: 4
kashif is on a distinguished road
dynamic allocation..urgent help needed!!!

hi, I need some help....i have an assignment for which i have written the complete code, but the thing is that the assignment requires that we use dynamic allocation. The functions also have been provided. But i do not know how to use them in my code, which works flawlessly. The instructions for the assignment with the dynamic allocation functions are here http://kashif.pcplayground.com/phonebook.html ....i need urgent help bec this is due tomm.
thanks
I am posting the code in here:-
Code:
/***********************************************************************
Description:This is an interactive menu driven program. It is designed
to implement an automated telephone book. Each entry in this automated
telephone book will contain a name and a telephone number.
***********************************************************************/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TEL_BOOK_SIZE 75
#define NAME_SIZE 22
#define TEL_NUM_SIZE 14

struct tel_book_element
{	char name[NAME_SIZE];
	char tel_num[TEL_NUM_SIZE];
};

typedef struct tel_book_element Tel_Book_Element;

struct tel_book
{	Tel_Book_Element entry[TEL_BOOK_SIZE];
	int n;
};

typedef struct tel_book Tel_Book;

int binarySearch(const Tel_Book *tbp, const char searchName[], int *ip);
int linearSearch(const Tel_Book *tbp, const char num[]);
void putTelBook(const char *fname,Tel_Book *tbp);
void getTelBook(const char *fname,Tel_Book *tbp);
void displayMenu(void);
char *strToUpper(char *s);
void lookUpName(Tel_Book *tbp);
void listName(Tel_Book *tbp);
void modify(Tel_Book *tbp);
void del(Tel_Book *tbp);
void inputNewBook(char *fname,Tel_Book *tbp);
void sortTelBook(Tel_Book *tbp);
void lookupnumber(Tel_Book *tbp);

int main()
{  Tel_Book tb;
	char save,keyHit,fname[NAME_SIZE]="a:tbfile.txt";

	getTelBook(fname,&tb);
	do
	{  displayMenu();
		printf("Enter Your Selection (a-f) ");
		keyHit=getchar();
		getchar();      /* get rid of the new line character */
		switch(keyHit)
		{	case '1': inputNewBook(fname,&tb); break;
		    case '2': lookUpName(&tb); break;
			case '4': modify(&tb); break;
			case '5': del(&tb); break;
			case '6': listName(&tb); break;
			case '3': lookupnumber(&tb);break;
			case '7':
				printf("Do you want to save it (y/n)? "); save=getchar();
				if(save=='y'||save=='Y')
					putTelBook(fname,&tb);
				break;
			default:
				printf("Option is not correct. Hit Enter to continue");
				getchar();
				break;
		}
	}while(keyHit!='7');

	return 0;
}

/***********************************************************************
The function inputNewBook close the current TelBook and read the new
TelBook from file, uppercase name, and sort in alphabetical order.
***********************************************************************/
void inputNewBook(char *fname,Tel_Book *tbp)
{  int m;
	char save;

	printf("Do you want to save the current one (y/n)? ");
	save=getchar(); getchar();
	if(save=='y'||save=='Y')
		putTelBook(fname,tbp);
	printf("Enter the filename: ");
	gets(fname);
	getTelBook(fname,tbp);
	for(m=0;m<tbp->n;m++)
		strToUpper(tbp->entry[m].name);
	sortTelBook(tbp);
	printf("New telephone book has been inputed.\n");
	printf("Press Enter to continue");         /* pause the output */
	getchar();
}

/***********************************************************************
The function sortTelBook sort all the name in alphabetical order.
***********************************************************************/
void sortTelBook(Tel_Book *tbp)
{  int pass,m;
	char hold[NAME_SIZE];

	for(pass=1;pass<tbp->n;pass++)
		for(m=0;m<(tbp->n)-pass;m++)
			if(strcmp(tbp->entry[m].name,tbp->entry[m+1].name)>0)
			{	/* swap 2 elements */
				strcpy(hold,tbp->entry[m].name);
				strcpy(tbp->entry[m].name,tbp->entry[m+1].name);
				strcpy(tbp->entry[m+1].name,hold);
				strcpy(hold,tbp->entry[m].tel_num);
				strcpy(tbp->entry[m].tel_num,tbp->entry[m+1].tel_num);
				strcpy(tbp->entry[m+1].tel_num,hold);
			}
}

/***********************************************************************
This function displays the options of the interactive-user program.
***********************************************************************/
void displayMenu(void)
{
  printf("Automated Telephone Book Menu\n");	
	printf("1) New telephone book-upper case and sort it\n");
	printf("2) Look up name to get number\n");
	printf("3) Look up number to get name\n");
	printf("4) Insert new/modified element\n");
	printf("5) Delete element\n");
	printf("6) List names starting with given letter\n");
	printf("7) Quit\n");
}

/***********************************************************************
This function changes a string to uppercase.
***********************************************************************/
char *strToUpper(char *s)
{	int i;

	for(i=0;s[i]!='\0';i++)
		s[i]=toupper(s[i]);
	return s;
}

/***********************************************************************
This function search a name, return 1 if found, 0 if not found, and ip
is the position found or should be inserted.
***********************************************************************/
int binarySearch(const Tel_Book *tbp, const char searchName[], int *ip)
{	int low,high,middle;

	low=0;high=tbp->n-1;
	while(low<=high)
	{	middle=(low+high)/2;
		if(strcmp(searchName,tbp->entry[middle].name)==0)
		{	*ip=middle;
			return 1;
		}
		else if(strcmp(searchName,tbp->entry[middle].name)<0)
			high=middle-1;
		else
			low=middle+1;
	}
	*ip=low;
	return 0;
}

/***********************************************************************
This function read information from a file, and store it to a struct
typed TelBook.
***********************************************************************/
void getTelBook(const char *fname,Tel_Book *tbp)
{  FILE *fp;
	int m=0;

	if ((fp=fopen(fname, "r"))==NULL)
	{	printf("File could not be opened to read\n");
		exit(1);   /* terminate the program  */
	}
	while(fgets(tbp->entry[m].name,NAME_SIZE,fp)!=NULL)
	/* read while not EOF */
		fgets(tbp->entry[m++].tel_num,TEL_NUM_SIZE,fp);
	tbp->n=m;      /* store the total of entries */
	fclose(fp);
}

/***********************************************************************
This function output information from a struct TelBook to a file.
***********************************************************************/
void putTelBook(const char *fname,Tel_Book *tbp)
{ 	int m;
	FILE *fp;

	if ((fp=fopen(fname, "w"))==NULL)
	{	printf("File could not be opened to write\n");  exit(1);
	}
	for(m=0;m<tbp->n;m++)
	{	fputs(tbp->entry[m].name,fp);
		fputs(tbp->entry[m].tel_num,fp);
	}
	fclose(fp);
}

/***********************************************************************
This function provides an option that user can look up an entry of the
telephone book.
***********************************************************************/
void lookUpName(Tel_Book *tbp)
{	char searchName[NAME_SIZE];
	int i;

	printf("Enter a name you want you look up: ");
	fgets(searchName,NAME_SIZE,stdin);
	strToUpper(searchName);
	if(binarySearch(tbp,searchName,&i))
	{	printf("Name found:\n");
		printf("%s",tbp->entry[i].name);
		printf("%s",tbp->entry[i].tel_num);
	}
	else
		printf("Name not Found\n");
	printf("Press Enter to continue");
	getchar();
}

/***********************************************************************
This function provides an option that user can list all the names which
begin with the first name inputted.
***********************************************************************/
void listName(Tel_Book *tbp)
{	char name[NAME_SIZE];
	int i,found=0;

	printf("Enter a name you want to list: ");
	gets(name);
	strToUpper(name);
	for(i=0;i<tbp->n;i++)
		if(tbp->entry[i].name==strstr(tbp->entry[i].name,name))
		{  found=1;
			printf("%s",tbp->entry[i].name);
			printf("%s",tbp->entry[i].tel_num);
		}
	if (!found)
		printf("Name not in the list\n");
	printf("Press Enter to continue");
	getchar();
}

/***********************************************************************
This function provides an option that user can input an entry, if found
then modify, if not found then insert as a new entry.
***********************************************************************/
void modify(Tel_Book *tbp)
{  int i,j;
	char name[NAME_SIZE],tel[TEL_NUM_SIZE];

	printf("Enter the name to modify or insert: ");
	fgets(name,NAME_SIZE,stdin);
	strToUpper(name);
	printf("Enter the telephone number: ");
	fgets(tel,TEL_NUM_SIZE,stdin);
	if(binarySearch(tbp,name,&i))             /* found entry */
	{	printf("Modified Entry Inserted\n");
		strcpy(tbp->entry[i].name,name);    /* i is position modified */
		strcpy(tbp->entry[i].tel_num,tel);
	}
	else           /* not found the entry, i is position inserted */
	{
		printf("New Entry Inserted\n");
/*		moving entries up by one position*/
		for(j=tbp->n;j>i;j--)
		{	strcpy(tbp->entry[j].name,tbp->entry[j-1].name);
			strcpy(tbp->entry[j].tel_num,tbp->entry[j-1].tel_num);
		}
		strcpy(tbp->entry[i].name,name);
		strcpy(tbp->entry[i].tel_num,tel);
		tbp->n++;
	}
	printf("Press Enter to continue");
	getchar();

}

/***********************************************************************
This function provides an option that user can delete one entry.
***********************************************************************/
void del(Tel_Book *tbp)
{  int i,j;
	char name[NAME_SIZE];

	printf("Enter the name to delete: ");
	fgets(name,NAME_SIZE,stdin);
	strToUpper(name);
	if(binarySearch(tbp,name,&i))
	{	printf("Entry Deleted\n");
		for(j=i;j<tbp->n-1;j++)   /*	moving entries down by one position*/
		{	strcpy(tbp->entry[j].name,tbp->entry[j+1].name);
			strcpy(tbp->entry[j].tel_num,tbp->entry[j+1].tel_num);
		}
		tbp->n--;
	}
	else
		printf("Name not Found\n");
	printf("Press Enter to continue");
	getchar();
}

/***********************************************************************
This function looks up the number
***********************************************************************/

void lookupnumber(Tel_Book *tbp)
{
	char num[TEL_NUM_SIZE];
	int i=0,k;

	printf("Enter a number to look up: ");
	fgets(num,TEL_NUM_SIZE,stdin);
	k=linearSearch(tbp,num);
	if(k!=-1)
	{
		printf("Name found:\n");
		printf("%s",tbp->entry[k].name);
		printf("%s",tbp->entry[k].tel_num);
	}
	else
	printf("Name not Found\n");
	printf("Press Enter to continue");
	getchar();
}

/***********************************************************************
performs linear search
***********************************************************************/

int linearSearch(const Tel_Book *tbp, const char num[])
{
	int i=0;

	for ( i = 0 ; i < tbp->n ; i++ ) {
  if ( strcmp( num, tbp->entry[i].tel_num ) == 0 ) return i;
}
return -1;

}
kashif is offline   Reply With Quote