Guest User

Untitled

a guest
Apr 10th, 2015
260
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 <stdlib.h>
  3. #include <string.h>
  4. struct para {
  5. int nr_indeksu;
  6. char nazwisko[30];
  7. float ocena;
  8. struct para *nast;
  9. struct para *poprz;
  10. };
  11. int main(){
  12. struct para *head=NULL, *ptr=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL,*tmp=NULL;
  13. FILE *f;
  14. int i=0, is_in=0;
  15. char inputfile[35];
  16. char outputfile[35];
  17. int tmp1[30];
  18. printf("Podaj nazwe pliku tekstowego:\n");
  19. scanf("%s", inputfile);
  20. f = fopen(inputfile, "r");
  21. if (f == NULL){
  22. printf("Nie znaleziono pliku!\n");
  23. return 0;
  24. }
  25. if(fscanf(f, "%s", &tmp1)!=1){
  26. printf("Plik jest pusty\n");
  27. return 0;
  28. }
  29. rewind(f);
  30. while(!feof(f)){
  31. if (head==NULL){
  32. head=ptr=(struct para*)malloc(sizeof(struct para));
  33. if (ptr==NULL){
  34. printf("Blad przydzialu pamieci!\nKoniec programu!\n");
  35. return 0;
  36. }
  37. fscanf(f, "%d %s %f", &ptr->nr_indeksu, &ptr->nazwisko, &ptr->ocena);
  38. printf("%d\t%s\t%.1f\n", ptr->nr_indeksu, ptr->nazwisko, ptr->ocena);
  39. ptr->nast=NULL;
  40. ptr->poprz = NULL;
  41. i++;
  42. }
  43. else{
  44. //ptr->nast=NULL;
  45. tmp=(struct para*)malloc(sizeof(struct para));
  46. /*ptr2=ptr;
  47. ptr=ptr->nast;
  48. ptr->poprz=ptr2;*/
  49. if (tmp==NULL){
  50. printf("Blad przydzialu pamieci!\nKoniec programu!\n");
  51. return 0;
  52. }
  53. fscanf(f, "%d %s %f", &tmp->nr_indeksu, &tmp->nazwisko, &tmp->ocena);
  54. printf("%d\t%s\t%.1f\n", tmp->nr_indeksu, tmp->nazwisko, tmp->ocena);
  55. ptr3=head;
  56. is_in=0;
  57. while(ptr3!=NULL){
  58. if(ptr3->nr_indeksu < tmp->nr_indeksu){
  59. if(ptr3->nast==NULL){
  60. ;
  61. }
  62. else{
  63. ptr3=ptr3->nast;
  64. }
  65. }
  66. else{
  67. is_in=1;
  68. }
  69. if(is_in==1){
  70. if(ptr3==head){
  71. ptr4=ptr3;
  72. ptr->poprz=tmp;
  73. tmp->nast=ptr4;
  74. head=tmp;
  75. head->poprz=NULL;
  76. break;
  77. }
  78. else if(ptr3->nast!=NULL){
  79. ptr4=ptr3->poprz;
  80. ptr4->nast=tmp;
  81. tmp->poprz=ptr4;
  82. tmp->nast=ptr3;
  83. ptr3->poprz=tmp;
  84. break;
  85. }
  86. else{
  87. ptr3->nast=tmp;
  88. tmp->nast=NULL;
  89. }
  90. }
  91. }
  92. /*if (tmp->nr_indeksu < ptr2->nr_indeksu){
  93. if (ptr2=head){
  94. head->nast=tmp;
  95. }
  96. else{
  97. ptr2->nast=tmp;
  98. }
  99. tmp->poprz=ptr2;
  100. }*/
  101. //ptr=tmp;
  102. //free(tmp);
  103. //ptr->nast=NULL;
  104. i++;
  105. }
  106. }
  107. fclose(f);
  108. printf("\n\nPosortowana lista wedlug indeksu:\n");
  109. ptr=head;
  110. while(ptr->nast!=NULL){
  111. //if (ptr->ocena<4.0)
  112. printf("%d %s %.1f\n", ptr->nr_indeksu, ptr->nazwisko, ptr->ocena);
  113. ptr=ptr->nast;
  114. }
  115. printf("Podaj nazwe pliku wynikowego:\n");
  116. scanf("%s", outputfile);
  117. f = fopen(outputfile, "w");
  118. ptr=head;
  119. while(ptr->nast!=NULL){
  120. fprintf(f, "%i %s %.1lf\n", ptr->nr_indeksu, ptr->nazwisko, ptr->ocena);
  121. ptr=ptr->nast;
  122. }
  123. fclose(f);
  124. ptr=head;
  125. while (head != NULL) {
  126. tmp = head;
  127. head = head->nast;
  128. free(tmp);
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment