Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <clocale>
- using namespace std;
- int main() {
- setlocale(LC_CTYPE, "rus");
- int N, i;
- fstream file;
- file.open("a.dat", ios::out|ios::binary);
- struct figura{
- int type;
- char color[20];
- union{
- short int radius; // радиус круга
- int side; // сторона квадрата
- float length; // длина отрезка
- };
- };
- cout<<"Количество фигур: ";
- cin>>N;
- figura my_figura;
- for (i = 0; i < N; i++){
- cout<<"Выберите тип фигуры "<<i+1<<"\n1 - Круг\n2 - Квадрат\n3 - Отрезок\n>";
- cin>>my_figura.type;
- cout<<"Введите цвет фигуры: ";
- cin>>my_figura.color;
- switch (my_figura.type){
- case 1: {
- cout<<"Введите радиус круга: ";
- cin>>my_figura.radius;
- break;
- }
- case 2: {
- cout<<"Введите длину стороны квадрата: ";
- cin>>my_figura.side;
- break;
- }
- case 3: {
- cout<<"Введите длину отрезка: ";
- cin>>my_figura.length;
- break;
- }
- }
- file.write((char *)&my_figura,sizeof(my_figura));
- }
- file.close();
- file.open("a.dat", ios::in|ios::binary);
- cout<<"\n-------------------------------------------------------------------------\n";
- for (int i = 0; i < N; i++) {
- file.read((char *)&my_figura,sizeof(my_figura));
- switch (my_figura.type){
- case 1: {
- cout<<"Круг, радиус="<<my_figura.radius;
- break;
- }
- case 2: {
- cout<<"Квадрат, сторона="<<my_figura.side;
- break;
- }
- case 3: {
- cout<<"Отрезок, длина="<<my_figura.length;
- break;
- }
- }
- cout<<", цвет "<<my_figura.color<<endl;
- }
- file.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement