Advertisement
kqlul123

Untitled

Jan 29th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "stdio.h"
  4.  
  5. using namespace std;
  6.  
  7. struct Info {
  8. char Name[20];
  9. int Cost;
  10. int Year;
  11. int Probeg;
  12. };
  13.  
  14. class Auto {
  15. Info* mas;
  16. int N;
  17. public:
  18. void Input();
  19. double AvgCost();
  20. void Sort();
  21. void WriteBinFL();
  22. void WriteFile();
  23. };
  24.  
  25. void Auto::Input() {
  26. cout << "Enter amount" << endl;
  27. cin >> N;
  28. mas = (Info*)malloc(sizeof(Info));
  29. for (int i = 0; i < N; i++)
  30. {
  31. cout << "Enter name" << endl; cin >> mas[i].Name;
  32. cout << "Enter cost" << endl; cin >> mas[i].Cost;
  33. cout << "Enter year" << endl; cin >> mas[i].Year;
  34. cout << "Enter pr" << endl; cin >> mas[i].Probeg;
  35. }
  36. }
  37.  
  38. double Auto::AvgCost() {
  39. int Sum = 0;
  40. for (int i = 0; i < N; i++)
  41. {
  42. Sum += mas[i].Cost;
  43. }
  44. return Sum / N;
  45. }
  46.  
  47. void Auto::WriteFile() {
  48. FILE *f;
  49. fopen_s(&f, "Auto.txt", "wt");
  50. fprintf(f, "%-20c", "Name");
  51. fprintf(f, "%-20c", "Year");
  52. fprintf(f, "%-20c", "Probeg");
  53. fprintf(f, "%-20c", "Cost");
  54. fprintf(f, "\n");
  55.  
  56. for (int i = 0; i < N; i++)
  57. {
  58. if (mas[i].Cost < AvgCost())
  59. {
  60. fprintf(f, "%-20c", mas[i].Name);
  61. fprintf(f, "%-20d", mas[i].Year);
  62. fprintf(f, "%-20d", mas[i].Probeg);
  63. fprintf(f, "%-20d", mas[i].Cost);
  64. fprintf(f, "\n");
  65. }
  66. }fclose(f);
  67. cout << "Произведена запись в тхт файл" << endl;
  68. }
  69.  
  70.  
  71. void Auto::Sort() {
  72. Info zap;
  73. int f;
  74. for (int i = 0; i < N-1; i++)
  75. {
  76. for (int j = i+1; j < N; j++)
  77. {
  78. f = mas[i].Cost > mas[j].Cost;
  79. if (f) {
  80. zap = mas[i];
  81. mas[i] = mas[j];
  82. mas[j] = zap;
  83. }
  84. }
  85. }
  86. }
  87.  
  88. void Auto::WriteBinFL() {
  89. FILE *q;
  90. fopen_s(&q, "Auto.bin", "wb");
  91. for (int i = 0; i < N; i++) {
  92. fwrite(mas + i, sizeof(Info), 1, q);
  93. }
  94. fclose(q);
  95. cout << "Произведена запись в бин файл";
  96. }
  97.  
  98. int main()
  99. {
  100. setlocale(LC_ALL, "Russian");
  101.  
  102. Auto q;
  103. q.Input();
  104. q.AvgCost();
  105. q.Sort();
  106. q.WriteFile();
  107. q.WriteBinFL();
  108.  
  109. system("pause");
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement