Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------MAIN-----------------------------------------------
- #include "pch.h"
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- #include "header.h"
- int main() {
- setlocale(LC_ALL, "Russian");
- FILE *file;
- int count = 0, b_count = 0, c_count = 0;
- just_count(&count, &b_count, &c_count);
- int current_year;
- char buf[256] = "";
- Ship *ships = (Ship *)malloc(count * sizeof(Ship));
- Military *militarys = (Military *)malloc(b_count * sizeof(Military));
- Civil *civils = (Civil *)malloc(c_count * sizeof(Civil));
- file = fopen("file.txt", "r");
- int c = 0, cb = 0, cc = 0;
- while (!feof(file)) {
- char type = ' ';
- char *name = (char *)malloc(32 * sizeof(char));
- char *country = (char *)malloc(32 * sizeof(char));
- int yearB;
- int i;
- fgets(buf, 256, file);
- if (c == 0)
- current_year = atoi(buf);
- if (c > 0) {
- sscanf(buf, "%c %s %s %i %i", &type, name, country, &yearB, &i);
- Ship ship(name, country, yearB);
- ships[c - 1] = ship;
- switch (type) {
- case 'В': {
- Military military(name, country, yearB, i);
- militarys[cb] = military;
- cb++;
- break;
- }
- case 'П': {
- Civil civil(name, country, yearB, i);
- civils[cc] = civil;
- cc++;
- break;
- }
- }
- }
- c++;
- }
- FILE *f_file, *b_file, *c_file;
- f_file = fopen("Ship.txt", "w");
- for (int i = 0; i < count; i++) {
- ships[i].print(f_file);
- }
- fclose(f_file);
- b_file = fopen("Military.txt", "w");
- for (int i = 0; i < b_count; i++) {
- militarys[i].print(b_file, current_year);
- }
- fclose(b_file);
- c_file = fopen("Civil.txt", "w");
- printf("%i", civils[0].yearB);
- for (int i = 0; i < c_count; i++) {
- civils[i].print(c_file, current_year);
- }
- fclose(c_file);
- return 0;
- }
- //-------------------------------------header.h---------------------------------------
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- class Ship {
- public:
- Ship(char *name, char *country, int yearB) {
- this->name = name;
- this->country = country;
- this->yearB = yearB;
- }
- char *name;
- char *country;
- int yearB;
- void print(FILE *file) { fprintf(file, "%s %s %d \n", name, country, yearB); }
- void change_N(char *name) { strcpy(this->name, name); }
- void change_C(char *country) { strcpy(this->country, country); }
- void change_Y(int yearB) { this->yearB = yearB; }
- };
- class Military : public Ship {
- public:
- Military(char *name, char *country, int yearB, int power)
- : Ship(name, country, yearB) {
- this->power = power;
- }
- int power;
- void print(FILE *file, int current_year) {
- fprintf(file, "%s %s ", name, country);
- if (current_year < yearB)
- fprintf(file, "%s", " еще не выпущен");
- else if ((current_year - yearB) > 20)
- fprintf(file, "%s", " требуется ремонт");
- else
- fprintf(file, "%d", yearB);
- fprintf(file, " %d \n", power);
- }
- void change_C(int power) { this->power = power; }
- };
- class Civil : public Ship {
- public:
- Civil(char *name, char *country, int yearB, int box)
- : Ship(name, country, yearB) {
- this->box = box;
- }
- int box;
- void print(FILE *file, int current_year) {
- fprintf(file, "%s %s ", name, country);
- if (current_year < yearB)
- fprintf(file, "%s", " еще не выпущен");
- else if ((current_year - yearB) > 30)
- fprintf(file, "%s", " требуется ремонт");
- else
- fprintf(file, "%d", yearB);
- fprintf(file, " %d \n", box);
- }
- void change_C(int box) { this->box = box; }
- };
- void just_count(int *count, int *b_count, int *c_count) {
- FILE *file;
- char buf[256];
- file = fopen("file.txt", "r");
- int ccount = 0, cb_count = 0, cc_count = 0;
- while (!feof(file)) {
- fgets(buf, 256, file);
- switch (buf[0]) {
- case 'В':
- cb_count++;
- break;
- case 'П':
- cc_count++;
- break;
- }
- ccount++;
- }
- *count = ccount - 1;
- *b_count = cb_count;
- *c_count = cc_count;
- fclose(file);
- }
- /*
- 2019
- В Аляска Россия 1900 1
- В Аризона Великобритания 2017 5
- П Аркхем Ангертина 1950 20
- В Вызывающий Украина 2020 20
- П ЛаФудр Шведцария 2017 15
- П Пекод Швеция 2021 10
- В Небойся Турция 2050 4*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement