Скачиваний:
5
Добавлен:
22.12.2022
Размер:
3.15 Кб
Скачать
#include <iostream>
#include <fstream> 
#include <windows.h>
#include <cstring>
 
using namespace std; 
 
struct Print
{
    string brand;
    string form;
    int speed;
    int price;
};

void input_struct(struct Print *P, int n);
void output_struct(struct Print *P, int n);
void correct(struct Print *P, int n);
void min_price(struct Print *P, int n);
void newFile (struct Print *P, int n);
 
int main() 
{
    Print *P; //создание указателя на структуру
    P=new Print[3]; //выделение памяти динамичекски
    input_struct(P,3); 
    output_struct(P,3);
    correct(P,3);
    min_price(P,3);
    newFile(P,3);
    delete[]P; //очистка памяти 
    return 0;
}
 
 
 
void input_struct(struct Print *P, int n) 
{
    ifstream fin; //создание обьекта класса ifstream
    fin.open("file2.txt", ios::in); 
    for(int i=0; i<n; i++) //запись файла в поля структуры
    {
        fin>>P[i].brand;
        fin>>P[i].form;
        fin>>P[i].speed;
        fin>>P[i].price;
    }
    fin.close(); 
}
 
void output_struct(struct Print *P, int n) 
{
 
    cout <<"Brand\t" << "Form\t" << "Speed\t" << "Price" <<endl;
    cout << "====================================" << endl; 
    for(int i=0; i<n; i++)
    {
        cout<<P[i].brand; 
        cout<<"\t"<<P[i].form;
        cout<<"\t"<<P[i].speed;
        cout<<"\t"<<P[i].price<<endl;
    }
    cout << "====================================" << endl;
 
}
 
void correct(struct Print *P, int n) 
{
    int key0=0;
    int key=0;
    cout<<"Need to fix(1/0)?"<<endl;
    cin>>key0;
    while (key0!=0) 
    {
 
        if (key0 ==1)
        {
            cout<<"Which line?"<<endl; 
            cin>>key; 
            if ((key != 1) && (key != 2) && (key != 3)  && (key != 4)) 
            {
                cout << "This line is not exist" << endl;
            }
            if ((key ==1) or (key == 2) or (key == 3) or (key == 4))
            {
                cout<<"Enter changes in "<<key<<" line:"<<endl; 
                cout<<"Brand: ";
                cin>> P[key-1].brand; 
                cout<<"Form: ";
                cin>>P[key-1].form;
                cout<<"Speed: ";
                cin>>P[key-1].speed;
                cout<<"Price: ";
                cin>>P[key-1].price;
            }
        }
        cout<<endl<<"Need to fix again(1/0)?"<<endl; 
        cin>>key0;
    }
}
 
void min_price(struct Print *P, int n) 
{
     int min = P[0].price;
     int k;
     for(int i=1;i<n;i++){
     	if(P[i].price<min){
     		min=P[i].price;
     		k=i;
		 }
	 }
	 cout<<"Print with the minimal price: " <<P[k].brand <<" " <<P[k].form <<" " <<P[k].speed <<" " <<min <<"\n";
}
 
void newFile(struct Print *P, int n)  
{
    char name[20]; 
    cout<<endl<<"Enter the name of new file: ";
    cin>> name;
    strcat(name, ".txt"); 
    ofstream save; 
    save.open(name); 
    for (int i=0; i<n; i++) 
    {
        save<<P[i].brand;
        save<<" "<<P[i].form;
        save<<" "<<P[i].speed;
        save<<" "<<P[i].price<<endl;
    }
    save.close(); 
}
Соседние файлы в папке Лабы