Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <clocale>
  6. using namespace std;
  7.  
  8. struct Knigi
  9. {
  10. int nomer;
  11. char avtor[15];
  12. char nazvanie[15];
  13. int god;
  14. int polka;
  15. };
  16. char X[15];
  17.  
  18. void outfile(fstream& f, int n) //функция вывода на экран содержимого файла
  19. {
  20. Knigi a;
  21. f.seekg(0); //установить указатель файла на начало
  22. cout<<setw(8)<<"NUMBER|"<<setw(15)<<"AUTHOR|"<<setw(15)<<"NAME|"<<setw(8)<<"YAER"<<setw(8)<<"STACK|"<<endl;
  23. cout<<"_________________________________________________________"<<endl;
  24. for(int i=0; i<n; i++)
  25. {
  26. f.read((char *)&a,sizeof a); //чтение из файла
  27. cout<<setw(8)<<a.nomer<<setw(15)<<a.avtor<<setw(15)<<a.nazvanie<<setw(8)<<a.god<<setw(8)<<a.polka;
  28. cout<<endl;
  29. }
  30. cout<<endl;
  31. }
  32.  
  33. void sort(fstream& f, int n) //функция сортировки по фамиилии авторов
  34. {
  35. Knigi min,a;
  36. int n_min;
  37. for(int i=0; i<n; i++)
  38. {
  39. f.seekg(i*(sizeof a));
  40. f.read((char *)&min,sizeof min);
  41. n_min=i;
  42. for(int j=i+1;j<n;j++)
  43. {
  44. f.read((char *)&a,sizeof a);
  45. if(strcmp(a.avtor,min.avtor)<0)
  46. {
  47. min=a;
  48. n_min=j;
  49. }
  50. }
  51. f.seekg(i*(sizeof a));
  52. f.read((char *) &a,sizeof a);
  53. f.seekp(i*(sizeof a));
  54. f.write((char *) &min,sizeof min);
  55. f.seekp(n_min*(sizeof a));
  56. f.write((char *) &a,sizeof a);
  57.  
  58. }
  59. }
  60.  
  61. void outfileX(fstream& f, int n, char* X)
  62. {
  63. Knigi a;
  64. f.seekg(0);
  65. cout<<setw(8)<<"NUMBER|"<<setw(15)<<"AUTHOR|"<<setw(15)<<"NAME|"<<setw(8)<<"YAER"<<setw(8)<<"STACK|"<<endl;
  66. cout<<"_________________________________________________________"<<endl;
  67. for(int i=0; i<n; i++)
  68. {
  69. f.read((char *)&a,sizeof a);
  70. if(strcmp(a.avtor, X)!=0) continue;
  71. cout<<setw(8)<<a.nomer<<setw(15)<<a.avtor<<setw(15)<<a.nazvanie<<setw(8)<<a.god<<setw(8)<<a.polka;
  72. cout<<endl;
  73. }
  74. cout<<endl;
  75. }
  76.  
  77. int read_f(fstream& f, fstream& out)
  78. {
  79. int i=0;Knigi a;
  80. while(!f.eof())
  81. {
  82. f>>a.nomer>>a.avtor>>a.nazvanie>>a.god>>a.polka;
  83. out.write((char *) &a, sizeof a);
  84. i++;
  85. }
  86. return i;
  87. }
  88.  
  89. int main()
  90. {
  91. setlocale(LC_CTYPE, "Russian");
  92. fstream in("Knigi.txt", ios::in);
  93. if(!in)
  94. {
  95. cout<<"error opening data.txt"<<endl;
  96. return 1;
  97. }
  98. fstream out("Knigi.dat", ios::out | ios::binary);
  99. if(!out)
  100. {
  101. cout<<"error opening data.dat"<<endl;
  102. return 1;
  103. }
  104. int size_f=read_f(in,out);
  105. in.close();out.close();
  106. fstream out_f("Knigi.dat", ios::in | ios::out | ios::binary);
  107. if(!out_f)
  108. {
  109. cout<<"error opening data.dat"<<endl;
  110. return 1;
  111. }
  112. outfile(out_f,size_f);
  113. sort(out_f,size_f);
  114. outfile(out_f,size_f);
  115. //out_f.close();
  116.  
  117. cout<<"INPUT X:";
  118. cin>>X;
  119. outfileX(out_f,size_f, X);
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement