Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. typedef struct rec{
  7. char name[30];
  8. char add[30];
  9. int phone;
  10. }rec;
  11.  
  12.  
  13. int i;
  14.  
  15.  
  16. void delete(rec*,int,int);
  17. void insert(rec*,int*,int,int);
  18. void display(rec*,int);
  19. void sortstuctstr(rec*,int);
  20.  
  21.  
  22. int main(){
  23. int n;
  24. int pos,elem;
  25. char opt;
  26.  
  27. printf("enter number of entries: ");
  28. scanf("%i",&n);
  29.  
  30. rec *arr=(rec*)calloc(n,sizeof(rec));
  31.  
  32. for(i=0;i<n;++i){
  33. printf("enter name: ");
  34. scanf(" %[^\n]",arr[i].name);
  35. printf("enter address: ");
  36. scanf(" %[^\n]",arr[i].add);
  37. printf("enter phone number: ");
  38. scanf(" %i",&arr[i].phone);
  39. }
  40.  
  41. printf("do you want to delete or insert a record? Y/N ");
  42. scanf(" %c",&opt);
  43.  
  44.  
  45. while(toupper(opt)!='N'){
  46. printf("insert(I) or delete(D) a record? ");
  47. scanf(" %c",&opt);
  48.  
  49. sortstuctstr(arr,n);
  50. if(toupper(opt)=='I'){
  51. printf("enter position & number of elements to be inserted: ");
  52. scanf("%i%i",&pos,&elem);
  53.  
  54. insert(arr,&n,pos,elem);
  55. display(arr,n);
  56.  
  57. printf("would you like to delete or insert again? Y/N ");
  58. scanf(" %c",&opt);
  59. }
  60.  
  61. else{
  62. printf("enter position that will be deleted: ");
  63. scanf("%i",&pos);
  64.  
  65. delete(arr,pos,n);
  66. display(arr,n);
  67.  
  68. printf("would you like to delete or insert again? Y/N ");
  69. scanf(" %c",&opt);
  70. }
  71. }
  72.  
  73.  
  74. sortstuctstr(arr,n);
  75.  
  76. if(toupper(opt)=='N'){
  77. display(arr,n);
  78. return 0;
  79. }
  80. }
  81.  
  82.  
  83. void insert(rec* arr,int* size,int pos,int numelem){
  84. int range=pos+numelem-1;
  85.  
  86. arr=realloc(arr,*size+numelem);
  87. for(i=*size-1;i>=pos-1;--i)
  88. arr[i+numelem]=arr[i];
  89.  
  90. for(i=pos-1;i<range;++i){
  91. printf("enter name: ");
  92. scanf(" %[^\n]",arr[i].name);
  93. printf("enter address: ");
  94. scanf(" %[^\n]",arr[i].add);
  95. printf("enter phone number: ");
  96. scanf("%i",&arr[i].phone);
  97. }
  98.  
  99. *size=*size+numelem;
  100. }
  101.  
  102.  
  103. void delete(rec* arr,int pos,int size){
  104. for(i=pos-1;i<size;++i){
  105. arr[i]=arr[i+1];
  106. if(i==size-1){
  107. arr[i].name[0]='\0';
  108. arr[i].add[0]='\0';
  109. arr[i].phone=0;
  110. }
  111. }
  112. }
  113.  
  114.  
  115. void sortstuctstr(rec* arr,int size){
  116. int lo_i=0,hi_i=size,j;
  117. rec temp;
  118.  
  119. while(lo_i<hi_i){
  120. for(j=lo_i+1;j<hi_i;++j){
  121. if(strcmp(arr[lo_i].name,arr[j].name)>0){
  122. temp=arr[lo_i];
  123. arr[lo_i]=arr[j];
  124. arr[j]=temp;
  125. }
  126. }
  127. ++lo_i;
  128. }
  129. }
  130.  
  131.  
  132. void display(rec* arr,int size){
  133. printf("name \t address \t phone number\n");
  134. for(i=0;i<size;++i)
  135. printf("%s \t %s \t %i\n",arr[i].name,arr[i].add,arr[i].phone);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement