Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
лаб_6_метод_указания.doc
Скачиваний:
0
Добавлен:
01.04.2025
Размер:
334.34 Кб
Скачать

1. Описать структуру с именем student, содержащую следующие поля:

  • NAME – фамилия и инициалы;

  • номер группы;

  • успеваемость (массив из пяти элементов).

2. Написать программу, выполняющую следующие действия:

  • вывод десяти структур данных типа STUDENT с клавиатуры в файл;

  • чтение из файла и вывод на дисплей фамилий и номеров групп для всех студентов, имеющих хотя бы одну оценку 3;

  • если таких студентов нет, вывести соответствующее сообщение.

////////////////////////////////////////////////////////////////

//head.h константы, определение структурного типа, прототипы функций

const int KOL_ST = 10;

const int KOL_OC = 5;

const int BAL = 3;

struct student {

unsigned short gruppa;

char fio[20];

unsigned short ysp[KOL_OC];

};

int vivod_file_txt (char [],const int, const int);

int vvod_file_txt (char[],const int, const int);

int otsenka (unsigned short [],const int,const int);

////////////////////////////////////////////////////////////////

// stdafx.h

#pragma once

#define WIN32_LEAN_AND_MEAN// Exclude rarely-used stuff from Windows headers

#include <stdio.h>

#include <tchar.h>

// TODO: reference additional headers your program requires here

#include <iostream>

#include <fstream>

#include <iomanip>

#include <conio.h>

#include "head.h"

using namespace std;

////////////////////////////////////////////////////////////////

//func.cpp

#include "stdafx.h"

int vivod_file_txt (char name_file[],const int kol_st, const int kol_oc)

{

ofstream outfile(name_file);

if (!outfile) {

cout <<"error ofstream"<<endl;

_getch();

return 1;

}

student stud; //определение структуры

for (int i=0; i<kol_st; i++) { //заполнение полей структуры

cout <<"vvedite fio studenta #"<<i+1<<" ";

cin >>stud.fio; //чтение поля структуры с клавиатуры

outfile << stud.fio <<'\n'; //запись поля структуры в файл

cout << "vvedite gryppy studenta #"<<i+1<<" ";

cin >> stud.gruppa;

outfile << stud.gruppa <<'\n';

cout <<"vvedite otsenki studenta #"<<i+1<<endl;

for (int j=0; j<kol_oc; j++)

{cin >>stud.ysp[j];

outfile << stud.ysp[j] <<'\n';

}

}

cout << "file is made \n" << endl;

outfile.close();

return 0;

}

//предполагаем , что если из файла считывается строка с фамилией // студента, то считается и последующая информация о нем.

int vvod_file_txt (char name_file[],const int kol_oc, const int bal) {

bool prov = false;

ifstream infile(name_file);

if(!infile) {

cout<<"error ifstream"<< endl;

_getch();

return 1;

}

student stud;

while (true) {

infile >> stud.fio;

if (infile.eof()) break; //проверка по stud.fio на конец файла

infile >> stud. gruppa;

for (int j=0; j<kol_oc; j++)

infile >>stud.ysp[j];

if (otsenka(stud.ysp,kol_oc,bal)) {

prov = true;

cout << "student " << setw(15) << stud.fio

<< " iz " << stud.gruppa

<< "-y gruppi imeet hotya bi odny troiky" << endl;

}

}

if (!prov) cout << "net studentov, imeyuschih troiki" << endl;

infile.close ();

return 0;

}

int otsenka (unsigned short ysp[],const int kol_oc, const int bal) {

for (int i=0;i<kol_oc;i++){

if (ysp[i]==bal) return 1;

}

return 0;

}

////////////////////////////////////////////////////////////////

//main.cpp

#include "stdafx.h"

int main () {

char name_file[20]; //имя файла

cout <<"vvedite imya faila: ";

cin.getline (name_file,20);

if ( vivod_file_txt (name_file,KOL_ST,KOL_OC) )

{cout <<"error vivod_file_txt() "<<endl;

_getch();

return 1;

}

else if ( vvod_file_txt (name_file,KOL_OC, BAL) )

{ cout <<"error vvod_file_txt() "<<endl;

_getch();

return 2;

}

else

_getch();

return 0;

}

////////////////////////////////////////////////////////////////