Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. // 6.3.4..правильный.cpp :6.3. БИНАРНЫЙ ФАЙЛ СТРУКТУР.Написать программу для работы с базой данных, содержащей записи со сведениями о студентах : ФИО, возраст, пол, курс, успеваемость, в которой должны выполняться следующие действия :создание файла/просмотр файла/выполнение задания 6.2.
  2.  
  3.  
  4.  
  5. // Задача: создание бинарного файла случайных целых чисел,
  6. // вывод чисел на экран, создание нового файла только из
  7. // четных чисел исходного файла
  8. #include "stdafx.h"
  9. #include <ctime>
  10. #include <stdlib.h>
  11. #include <fstream>
  12. #include <string.h>
  13. #include <iostream>
  14. using namespace std;
  15.  
  16. //
  17. const int m = 40;
  18. struct student // объявление структуры student
  19. {
  20. char name[m];
  21. int kyrs;
  22. int vozrast;
  23. bool pol;
  24. double yspevaemost;
  25. };
  26.  
  27. student read(); // ввод значений полей структуры
  28. int yslovie(char *stroka, int k);
  29. void vyvod(char *stroka);
  30. void vvod(int n, char *stroka);
  31. void print(student stud);
  32.  
  33.  
  34.  
  35. int main()
  36. {
  37. int n;
  38. const int Max = 80;
  39. char stroka[Max];
  40. cout << "Enter Kolvo man = " << endl;
  41. cin >> n;
  42. strcpy_s(stroka,"file.bin");
  43. vvod(n,stroka);
  44. vyvod(stroka);
  45. int k;
  46. cout << "Enter Kyrs = " << endl;
  47. cin >> k;
  48. int kolvo=yslovie(stroka, k);
  49. cout << "Kolvo students pol=m,kyrs-k = " << kolvo << endl;
  50. return 0;
  51. }
  52.  
  53.  
  54. //
  55. student read() // ввод значений полей структуры
  56. {
  57. student stud;
  58. cin.get();
  59. cout << " FIO = ";
  60. cin.getline(stud.name, m);
  61. cout << endl;
  62. cout << "Kyrs = ";
  63. cin >> stud.kyrs;
  64. cout << endl;
  65. cout << "Vozrast = ";
  66. cin >> stud.vozrast;
  67. cout << endl;
  68. cout << "Pol 1-M,0-W = ";
  69. cin >> stud.pol;
  70. cout << endl;
  71. cout << "Yspevaemost = ";
  72. cin >> stud.yspevaemost;
  73. cout << endl; // ввод значения key
  74. //cin.get();
  75. return stud;
  76. }
  77.  
  78. //
  79. void vyvod(char *stroka)
  80. {
  81. student stud;
  82. ifstream filein(stroka, ios::binary);
  83. while (!filein.eof())
  84. {
  85. filein.read(reinterpret_cast <char*> (&stud), sizeof(student));
  86. print(stud);
  87. }
  88. filein.close();
  89. }
  90.  
  91. void vvod(int n, char *stroka)
  92. {
  93. student stud;
  94. ofstream fileout(stroka, ios::binary);
  95. for (int i = 1;i <= n;i++)
  96. {
  97. stud = read();
  98. fileout.write(reinterpret_cast <char*> (&stud), sizeof(student));
  99. }
  100. fileout.close();
  101. }
  102.  
  103. void print(student stud) // вывод значений полей структуры
  104. {
  105. cout << " FIO = " << stud.name;
  106. cout << " Kyrs = " << stud.kyrs;
  107. cout << " Vozrast = " << stud.vozrast;
  108. cout << " Pol = " << stud.pol;
  109. cout << " Yspevaemost = " << stud.yspevaemost << endl;
  110.  
  111. }
  112.  
  113. int yslovie(char *stroka, int k)
  114. {
  115. int kolvo = 0;
  116. student stud;
  117. ifstream filein(stroka, ios::binary);
  118. while (!filein.eof())
  119. {
  120. filein.read(reinterpret_cast <char*> (&stud), sizeof(student));
  121. if (stud.kyrs == k && stud.pol == 1)
  122. {
  123. kolvo++;
  124. }
  125. }
  126. filein.close();
  127. return kolvo;
  128. }
  129. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement