Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- typedef struct rec{
- char name[30];
- char add[30];
- int phone;
- }rec;
- int i;
- void delete(rec*,int,int);
- void insert(rec*,int*,int,int);
- void display(rec*,int);
- void sortstuctstr(rec*,int);
- int main(){
- int n;
- int pos,elem;
- char opt;
- printf("enter number of entries: ");
- scanf("%i",&n);
- rec *arr=(rec*)calloc(n,sizeof(rec));
- for(i=0;i<n;++i){
- printf("enter name: ");
- scanf(" %[^\n]",arr[i].name);
- printf("enter address: ");
- scanf(" %[^\n]",arr[i].add);
- printf("enter phone number: ");
- scanf(" %i",&arr[i].phone);
- }
- printf("do you want to delete or insert a record? Y/N ");
- scanf(" %c",&opt);
- while(toupper(opt)!='N'){
- printf("insert(I) or delete(D) a record? ");
- scanf(" %c",&opt);
- sortstuctstr(arr,n);
- if(toupper(opt)=='I'){
- printf("enter position & number of elements to be inserted: ");
- scanf("%i%i",&pos,&elem);
- insert(arr,&n,pos,elem);
- display(arr,n);
- printf("would you like to delete or insert again? Y/N ");
- scanf(" %c",&opt);
- }
- else{
- printf("enter position that will be deleted: ");
- scanf("%i",&pos);
- delete(arr,pos,n);
- display(arr,n);
- printf("would you like to delete or insert again? Y/N ");
- scanf(" %c",&opt);
- }
- }
- sortstuctstr(arr,n);
- if(toupper(opt)=='N'){
- display(arr,n);
- return 0;
- }
- }
- void insert(rec* arr,int* size,int pos,int numelem){
- int range=pos+numelem-1;
- arr=realloc(arr,*size+numelem);
- for(i=*size-1;i>=pos-1;--i)
- arr[i+numelem]=arr[i];
- for(i=pos-1;i<range;++i){
- printf("enter name: ");
- scanf(" %[^\n]",arr[i].name);
- printf("enter address: ");
- scanf(" %[^\n]",arr[i].add);
- printf("enter phone number: ");
- scanf("%i",&arr[i].phone);
- }
- *size=*size+numelem;
- }
- void delete(rec* arr,int pos,int size){
- for(i=pos-1;i<size;++i){
- arr[i]=arr[i+1];
- if(i==size-1){
- arr[i].name[0]='\0';
- arr[i].add[0]='\0';
- arr[i].phone=0;
- }
- }
- }
- void sortstuctstr(rec* arr,int size){
- int lo_i=0,hi_i=size,j;
- rec temp;
- while(lo_i<hi_i){
- for(j=lo_i+1;j<hi_i;++j){
- if(strcmp(arr[lo_i].name,arr[j].name)>0){
- temp=arr[lo_i];
- arr[lo_i]=arr[j];
- arr[j]=temp;
- }
- }
- ++lo_i;
- }
- }
- void display(rec* arr,int size){
- printf("name \t address \t phone number\n");
- for(i=0;i<size;++i)
- printf("%s \t %s \t %i\n",arr[i].name,arr[i].add,arr[i].phone);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement