Tuesday, 1 November 2011

6. Mobile user Information


  /*Title : Mobile user data searching and sorting */



/* header files */
#include<stdio.h>
#include<conio.h>
#include<string.h>

typedef struct customer   /* Declaration of structure */
{
char mob[20];
char name[10];
float bill;

}customer;

 int main(void)
{
/* Declaration & Initialization */
int icNum=0, iSwChoice=0;
char choice='\0', no[20]={'\0'};
customer C[10]={0};

void accept(customer [], int)  ; /* Function prototype */
void display(customer [], int);
int binary_search(customer [], int, char []);
int insertion_sort(customer [], int);
int selection_sort(customer [], int);

clrscr();


do
{
printf("\n\n\tMenu");
printf("\n1 : Accept");
printf("\n2 : Display");
printf("\n3 : Selection Sort (ascending order of name)");
printf("\n4 : Insertion Sort (descending order of mobile no.)");
printf("\n5 : Binary Search");
printf("\n6 : Exit");

printf("\nEnter your choice : ");
scanf("%d",&iSwChoice);

switch(iSwChoice)
{
case 1 :  Label:if(icNum==0)
{
printf("\nEnter the number of customers\t");
scanf("%d",&icNum);
accept(C,icNum);
}
else
{
printf("\nDo you wish to overwrite the database?\t");
flushall();
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
icNum=0;
goto Label;
}
}
break;

case 2 : if(icNum!=0)
{
printf("\n\t\t\tCustomer database");
display(C,icNum);
}
else
{
printf("\nDatabase is empty!");
}
break;

case 3 : if(icNum!=0)
{
printf("\nThe sorted records are");
selection_sort(C,icNum);
display(C,icNum);
}
else
{
printf("\nDatabase is empty!");
}
break;

case 4 : if(icNum!=0)
{
insertion_sort(C,icNum);
display(C,icNum);
}
else
{
printf("\nDatabase is empty!");
}
break;

case 5 : if(icNum!=0)
{
printf("\nEnter the no. to be searched:\t");
scanf("%s",no);
insertion_sort(C,icNum);
binary_search(C,icNum,no);
}
else
{
printf("\nDatabase is empty!");
}
break;

} /* End of switch */
} /* End of do while loop */
while(iSwChoice<=5&&iSwChoice!=0);

getch();
return 0;
}

void accept(struct customer C[], int n) /* Function Definition */
{
int i=0, j=0;
float billamt=0.0;

for(i=0;i<n;i++)
{
printf("\nCustomer %d",i+1);
printf("\nEnter mobile number : ");
Label : scanf("%s",&C[i].mob);
for(j=0;j<i;j++)
{
if(strcmp(C[i].mob,C[j].mob)==0)
{
printf("\nMobile number already exists\nEnter again : ");
/* Mobile number must be unique */
goto Label;
}
}
printf("Enter name : ");
scanf("%s",&C[i].name);
printf("Enter bill amount : ");
flushall();
scanf("%f",&billamt);
C[i].bill=billamt;
}
}

/* For displaying all records */
void display(struct customer C[], int total)
{
int i=0;
printf("\n\tMobile No.\t\t\Name\t\tBill Amount");
for(i=0;i<total;i++)
{
printf("\n\t%s\t\t%s\t\t%f",C[i].mob,C[i].name,C[i].bill);
}
printf("\n");
}

int selection_sort(customer C[],int n)
{
int i=0, j=0;
customer temp;

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(C[i].name,C[j].name)>0)
{
temp=C[i];
C[i]=C[j];
C[j]=temp;
}
}
}
return 0;
}

int insertion_sort(customer C[], int n)
{
int i=0, j=0;
customer temp;

for(i=1;i<n;i++)
{
temp=C[i];
for(j=i-1;j>=0;j--)
{
if(strcmp(C[j].mob,temp.mob)<0)
{
C[j+1]=C[j];
}
else
{
break;
}
}
C[j+1]=temp;
}
return 0;
}

int binary_search(customer C[], int n, char no[20])
{
int mid=0, count=1, end=0 ,start=0, flag=0;

end=n-1;
while(end>=start)
{
mid=(start+end)/2;

if(strcmp(C[mid].mob,no)==0)
{
flag=1;
break;
}
else
{
if(strcmp(C[mid].mob,no)>0)
{
start=mid+1;
}
else
{
end=mid-1;
}
}
count++;
}
printf("\n\nNumber of passes : %d",count);

if(flag==1)
{
printf("\nRecord found at %d",mid);
if(count==1)
{
printf("\n\nBest Case!");
}
else if(count==(n-1)/2)
{
printf("\n\nWorst Case!");
}
else
{
printf("\n\nAverage Case!");
}
}
else
{
printf("\n\nWorst Case!");
printf("\n\nRecord does not exist!");

}

return 0;
}

No comments:

Post a Comment