Advertisement
Ilitid

Untitled

Feb 6th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <clocale>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     setlocale(LC_CTYPE, "rus");
  10.  
  11.     int N, i;
  12.  
  13.     fstream file;
  14.     file.open("a.dat", ios::out|ios::binary);
  15.  
  16.     struct figura{
  17.         int type;
  18.         char color[20];
  19.         union{
  20.             short int radius; // радиус круга
  21.             int side; // сторона квадрата
  22.             float length; // длина отрезка
  23.         };
  24.     };
  25.  
  26.     cout<<"Количество фигур: ";
  27.     cin>>N;
  28.  
  29.     figura my_figura;
  30.     for (i = 0; i < N; i++){
  31.         cout<<"Выберите тип фигуры "<<i+1<<"\n1 - Круг\n2 - Квадрат\n3 - Отрезок\n>";
  32.         cin>>my_figura.type;
  33.         cout<<"Введите цвет фигуры: ";
  34.         cin>>my_figura.color;
  35.         switch (my_figura.type){
  36.             case 1: {
  37.                 cout<<"Введите радиус круга: ";
  38.                 cin>>my_figura.radius;
  39.                 break;
  40.             }
  41.             case 2: {
  42.                 cout<<"Введите длину стороны квадрата: ";
  43.                 cin>>my_figura.side;
  44.                 break;
  45.             }
  46.             case 3: {
  47.                 cout<<"Введите длину отрезка: ";
  48.                 cin>>my_figura.length;
  49.                 break;
  50.             }
  51.         }
  52.         file.write((char *)&my_figura,sizeof(my_figura));
  53.     }
  54.     file.close();
  55.  
  56.     file.open("a.dat", ios::in|ios::binary);
  57.     cout<<"\n-------------------------------------------------------------------------\n";
  58.     for (int i = 0; i < N; i++) {
  59.         file.read((char *)&my_figura,sizeof(my_figura));
  60.         switch (my_figura.type){
  61.             case 1: {
  62.                 cout<<"Круг, радиус="<<my_figura.radius;
  63.                 break;
  64.             }
  65.             case 2: {
  66.                 cout<<"Квадрат, сторона="<<my_figura.side;
  67.                 break;
  68.             }
  69.             case 3: {
  70.                 cout<<"Отрезок, длина="<<my_figura.length;
  71.                 break;
  72.             }
  73.         }
  74.         cout<<", цвет "<<my_figura.color<<endl;
  75.     }
  76.  
  77.     file.close();
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement