Advertisement
Julia_S

2.5_option17

Oct 20th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. //------------------------------------MAIN-----------------------------------------------
  2. #include "pch.h"
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <iostream>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "header.h"
  8.  
  9.  
  10. int main() {
  11.  
  12. setlocale(LC_ALL, "Russian");
  13.  
  14. FILE *file;
  15.  
  16. int count = 0, b_count = 0, c_count = 0;
  17. just_count(&count, &b_count, &c_count);
  18.  
  19. int current_year;
  20. char buf[256] = "";
  21.  
  22. Ship *ships = (Ship *)malloc(count * sizeof(Ship));
  23. Military *militarys = (Military *)malloc(b_count * sizeof(Military));
  24. Civil *civils = (Civil *)malloc(c_count * sizeof(Civil));
  25.  
  26. file = fopen("file.txt", "r");
  27.  
  28. int c = 0, cb = 0, cc = 0;
  29. while (!feof(file)) {
  30. char type = ' ';
  31. char *name = (char *)malloc(32 * sizeof(char));
  32. char *country = (char *)malloc(32 * sizeof(char));
  33. int yearB;
  34. int i;
  35.  
  36. fgets(buf, 256, file);
  37. if (c == 0)
  38. current_year = atoi(buf);
  39. if (c > 0) {
  40. sscanf(buf, "%c %s %s %i %i", &type, name, country, &yearB, &i);
  41. Ship ship(name, country, yearB);
  42. ships[c - 1] = ship;
  43.  
  44. switch (type) {
  45. case 'В': {
  46. Military military(name, country, yearB, i);
  47. militarys[cb] = military;
  48. cb++;
  49. break;
  50. }
  51. case 'П': {
  52. Civil civil(name, country, yearB, i);
  53. civils[cc] = civil;
  54. cc++;
  55. break;
  56. }
  57. }
  58. }
  59. c++;
  60. }
  61.  
  62. FILE *f_file, *b_file, *c_file;
  63.  
  64. f_file = fopen("Ship.txt", "w");
  65. for (int i = 0; i < count; i++) {
  66. ships[i].print(f_file);
  67. }
  68. fclose(f_file);
  69.  
  70. b_file = fopen("Military.txt", "w");
  71. for (int i = 0; i < b_count; i++) {
  72. militarys[i].print(b_file, current_year);
  73. }
  74. fclose(b_file);
  75.  
  76. c_file = fopen("Civil.txt", "w");
  77. printf("%i", civils[0].yearB);
  78. for (int i = 0; i < c_count; i++) {
  79. civils[i].print(c_file, current_year);
  80. }
  81. fclose(c_file);
  82. return 0;
  83. }
  84. //-------------------------------------header.h---------------------------------------
  85. #include <iostream>
  86. #include <stdlib.h>
  87. #include <string.h>
  88.  
  89. class Ship {
  90. public:
  91. Ship(char *name, char *country, int yearB) {
  92. this->name = name;
  93. this->country = country;
  94. this->yearB = yearB;
  95. }
  96. char *name;
  97. char *country;
  98. int yearB;
  99.  
  100. void print(FILE *file) { fprintf(file, "%s %s %d \n", name, country, yearB); }
  101.  
  102. void change_N(char *name) { strcpy(this->name, name); }
  103.  
  104. void change_C(char *country) { strcpy(this->country, country); }
  105.  
  106. void change_Y(int yearB) { this->yearB = yearB; }
  107. };
  108.  
  109. class Military : public Ship {
  110. public:
  111. Military(char *name, char *country, int yearB, int power)
  112. : Ship(name, country, yearB) {
  113. this->power = power;
  114. }
  115. int power;
  116.  
  117. void print(FILE *file, int current_year) {
  118. fprintf(file, "%s %s ", name, country);
  119.  
  120. if (current_year < yearB)
  121. fprintf(file, "%s", " еще не выпущен");
  122. else if ((current_year - yearB) > 20)
  123. fprintf(file, "%s", " требуется ремонт");
  124. else
  125. fprintf(file, "%d", yearB);
  126.  
  127. fprintf(file, " %d \n", power);
  128. }
  129.  
  130. void change_C(int power) { this->power = power; }
  131. };
  132.  
  133. class Civil : public Ship {
  134. public:
  135. Civil(char *name, char *country, int yearB, int box)
  136. : Ship(name, country, yearB) {
  137. this->box = box;
  138. }
  139. int box;
  140.  
  141. void print(FILE *file, int current_year) {
  142. fprintf(file, "%s %s ", name, country);
  143.  
  144. if (current_year < yearB)
  145. fprintf(file, "%s", " еще не выпущен");
  146. else if ((current_year - yearB) > 30)
  147. fprintf(file, "%s", " требуется ремонт");
  148. else
  149. fprintf(file, "%d", yearB);
  150.  
  151. fprintf(file, " %d \n", box);
  152. }
  153.  
  154. void change_C(int box) { this->box = box; }
  155. };
  156.  
  157. void just_count(int *count, int *b_count, int *c_count) {
  158. FILE *file;
  159. char buf[256];
  160. file = fopen("file.txt", "r");
  161. int ccount = 0, cb_count = 0, cc_count = 0;
  162. while (!feof(file)) {
  163. fgets(buf, 256, file);
  164. switch (buf[0]) {
  165. case 'В':
  166. cb_count++;
  167. break;
  168. case 'П':
  169. cc_count++;
  170. break;
  171. }
  172. ccount++;
  173. }
  174. *count = ccount - 1;
  175. *b_count = cb_count;
  176. *c_count = cc_count;
  177. fclose(file);
  178. }
  179. /*
  180. 2019
  181. В Аляска Россия 1900 1
  182. В Аризона Великобритания 2017 5
  183. П Аркхем Ангертина 1950 20
  184. В Вызывающий Украина 2020 20
  185. П ЛаФудр Шведцария 2017 15
  186. П Пекод Швеция 2021 10
  187. В Небойся Турция 2050 4*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement