Advertisement
Guest User

Untitled

a guest
May 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. typedef struct student
  5. {
  6. char name[20];
  7. char gender;
  8. int years;
  9. struct student *nextp;
  10. struct student *prevp;
  11. }friend;
  12.  
  13. void carinama(friend* pfriend,char nama[20]);
  14. void displaymale(friend* pfriend);
  15. void displayfemale(friend* pfriend);
  16. void displayumurteman(friend* pfriend,int n);
  17.  
  18. int main()
  19. {
  20. friend *pfrtemp,*pfrbefore,*pfrhead,*pfrtail;
  21. FILE *fop;
  22. char comma;
  23. float nilai;
  24. char fname[20];
  25. strcpy(fname,"myfriend.txt");
  26. if(!(fop=fopen(fname,"r")))
  27. {
  28. printf("cannot open this file !!\n");
  29. return 0;
  30. }
  31. else
  32. {
  33. printf("open file success !! \n");
  34. pfrtemp=malloc(sizeof(friend));
  35. fscanf(fop,"%[^,]",pfrtemp->name);
  36. fscanf(fop,"%c",&comma);
  37. if(!feof(fop))
  38. {
  39. pfrhead=pfrtemp;
  40. pfrtemp->prevp=NULL;
  41. }
  42. else
  43. {
  44. free(pfrtemp);
  45. }
  46. while(!feof(fop))
  47. {
  48. fscanf(fop,"%c",&pfrtemp->gender);
  49. fscanf(fop,"%c",&comma);
  50. fscanf(fop,"%d",&pfrtemp->years);
  51. fscanf(fop,"%c",&comma);
  52. pfrbefore=pfrtemp;
  53. pfrtemp=malloc(sizeof(friend));
  54. fscanf(fop,"%[^,]",pfrtemp->name);
  55. fscanf(fop,"%c",&comma);
  56. if(!feof(fop))
  57. {
  58. pfrbefore->nextp=pfrtemp;
  59. pfrtemp->prevp=pfrbefore;
  60. }
  61. else
  62. {
  63. pfrbefore->nextp=NULL;
  64. pfrtail=pfrbefore;
  65. free(pfrtemp);
  66. }
  67. }
  68. }
  69. fclose(fop);
  70.  
  71. displayfromhead(pfrhead);
  72. displayfromtail(pfrtail);
  73. displayndarihead(pfrhead,1);
  74. displayndaritail(pfrtail,1);
  75. carinama(pfrhead,"kamu");
  76. return 0;
  77. }
  78.  
  79. void carinama(friend* pfriend,char nama[20])
  80. {
  81. char *pnama;
  82. pnama=nama;
  83. while(pfriend!=NULL)
  84. {
  85. if(!strcmp(pfriend->name,pnama)){
  86. printf("cari nama:\n");
  87. printf("Name : %s \n",pfriend->name);
  88. printf("Gender : %c \n",pfriend->gender);
  89. printf("registration years : %d \n",pfriend->years);
  90. printf("\n");
  91. break;
  92. }
  93. pfriend=pfriend->nextp;
  94. }
  95. }
  96.  
  97. void displaymale(friend* pfriend)
  98. {
  99. while(pfriend!=NULL)
  100. {
  101. printf("display FROM HEAD:\n");
  102. printf("Name : %s \n",pfriend->name);
  103. printf("Gender : %c \n",pfriend->gender);
  104. printf("registration years : %d \n",pfriend->years);
  105. pfriend=pfriend->nextp;
  106. printf("\n");
  107. }
  108. printf("\n\n");
  109. }
  110.  
  111. void displayfemale(friend* pfriend)
  112. {
  113. while(pfriend!=NULL)
  114. {
  115. printf("display FROM TAIL:\n");
  116. printf("Name : %s \n",pfriend->name);
  117. printf("Gender : %c \n",pfriend->gender);
  118. printf("registration years : %d \n",pfriend->years);
  119. pfriend=pfriend->prevp;
  120. printf("\n");
  121. }
  122. printf("\n\n");
  123. }
  124.  
  125. void displayumurteman(friend* pfriend,int n)
  126. {
  127. int i;
  128. for(i=0;i<n;i++)
  129. {
  130. printf("display N dari head:\n");
  131. printf("Name : %s \n",pfriend->name);
  132. printf("Gender : %c \n",pfriend->gender);
  133. printf("registration years : %d \n",pfriend->years);
  134. pfriend=pfriend->nextp;
  135. printf("\n");
  136. }
  137. printf("\n\n");
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement