Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "iostream"
- #include "stdio.h"
- using namespace std;
- struct Info {
- char Name[20];
- int Cost;
- int Year;
- int Probeg;
- };
- class Auto {
- Info* mas;
- int N;
- public:
- void Input();
- double AvgCost();
- void Sort();
- void WriteBinFL();
- void WriteFile();
- };
- void Auto::Input() {
- cout << "Enter amount" << endl;
- cin >> N;
- mas = (Info*)malloc(sizeof(Info));
- for (int i = 0; i < N; i++)
- {
- cout << "Enter name" << endl; cin >> mas[i].Name;
- cout << "Enter cost" << endl; cin >> mas[i].Cost;
- cout << "Enter year" << endl; cin >> mas[i].Year;
- cout << "Enter pr" << endl; cin >> mas[i].Probeg;
- }
- }
- double Auto::AvgCost() {
- int Sum = 0;
- for (int i = 0; i < N; i++)
- {
- Sum += mas[i].Cost;
- }
- return Sum / N;
- }
- void Auto::WriteFile() {
- FILE *f;
- fopen_s(&f, "Auto.txt", "wt");
- fprintf(f, "%-20c", "Name");
- fprintf(f, "%-20c", "Year");
- fprintf(f, "%-20c", "Probeg");
- fprintf(f, "%-20c", "Cost");
- fprintf(f, "\n");
- for (int i = 0; i < N; i++)
- {
- if (mas[i].Cost < AvgCost())
- {
- fprintf(f, "%-20c", mas[i].Name);
- fprintf(f, "%-20d", mas[i].Year);
- fprintf(f, "%-20d", mas[i].Probeg);
- fprintf(f, "%-20d", mas[i].Cost);
- fprintf(f, "\n");
- }
- }fclose(f);
- cout << "Произведена запись в тхт файл" << endl;
- }
- void Auto::Sort() {
- Info zap;
- int f;
- for (int i = 0; i < N-1; i++)
- {
- for (int j = i+1; j < N; j++)
- {
- f = mas[i].Cost > mas[j].Cost;
- if (f) {
- zap = mas[i];
- mas[i] = mas[j];
- mas[j] = zap;
- }
- }
- }
- }
- void Auto::WriteBinFL() {
- FILE *q;
- fopen_s(&q, "Auto.bin", "wb");
- for (int i = 0; i < N; i++) {
- fwrite(mas + i, sizeof(Info), 1, q);
- }
- fclose(q);
- cout << "Произведена запись в бин файл";
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- Auto q;
- q.Input();
- q.AvgCost();
- q.Sort();
- q.WriteFile();
- q.WriteBinFL();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement