Лабораторные работы / отчёт 12
.docxФЕДЕРАЛЬНОЕ ГОСУДАРСТВЕННОЕ АВТОНОМНОЕ
ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ
“ОМСКИЙ ГОСУДАРСТВЕННЫЙ ТЕХНИЧЕСКИЙ УНИВЕРСИТЕТ”
Кафедра “Информатика и вычислительная техника”
ОТЧЕТ
по лабораторной работе №12
“СТРУКТУРЫ”
Выполнил:
Студент группы ИВТ-234 Бектимиров А.Е.
_____________________________________________________
(дата, подпись)
Проверил:
ст. пр. доцент Дорошенко М.С.
_____________________________________________________
(дата, подпись)
Омск 2024
Лабораторная работа №12
Тема: Структуры
Вариант 2
Задание 1
Условие задачи:
Дан список студентов и оценка каждого на экзамене (оценки на N экзаменах). Подсчитать количество удовлетворительных оценок, хороших, отличных и средний балл в группе. Напечатать фамилии неуспевающих студентов.
Схема алгоритма:
Текст программы:
#include <iostream>
#include <vector>
#include <string>
#include <locale.h>
using namespace std;
struct Student {
string name;
vector<int> grades;
};
int main() {
setlocale(LC_ALL, "RUS");
vector<Student> students = {
{"Иванов", {4, 5, 3}},
{"Петров", {5, 5, 4}},
{"Сидоров", {2, 3, 2}},
{"Козлов", {5, 4, 4}}
};
int satisfactory = 0;
int good = 0;
int excellent = 0;
int totalAverage = 0;
int studentCount = students.size();
for (int i = 0; i < studentCount; i++) {
int sum = 0;
for (int grade : students[i].grades) {
sum += grade;
if (grade >= 1 && grade <= 2) {
satisfactory++;
}
else if (grade >= 3 && grade <= 4) {
good++;
}
else if (grade == 5) {
excellent++;
}
}
totalAverage += sum / students[i].grades.size();
if (sum / students[i].grades.size() < 3) {
cout << students[i].name << " не справился" << endl;
}
}
cout << "Удовлетворительных оценок: " << satisfactory << endl;
cout << "Хороших оценок: " << good << endl;
cout << "Отличных оценок: " << excellent << endl;
cout << "Средний балл в группе: " << totalAverage / studentCount << endl;
return 0;
}
Результаты выполнения программы, выводы:
Задание 2
Условие задачи:
Дополнить программу 12_1 заполнением и обработкой файлов. Имя файла вводить с клавиатуры в процессе работы программы.
Схема алгоритма:
Текст программы:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <locale.h>
#include <windows.h>
using namespace std;
struct Student {
string name;
vector<int> grades;
};
int main() {
setlocale(LC_ALL, "RUS");
SetConsoleCP(1251);
string filename;
cout << "Введите имя файла: ";
cin >> filename;
ifstream file(filename);
if (!file.good()) {
ofstream createFile(filename);
createFile.close();
file.open(filename);
}
vector<Student> students;
string line;
while (getline(file, line)) {
Student student;
student.name = line;
getline(file, line);
stringstream ss(line);
string grade;
while (ss >> grade) {
student.grades.push_back(stoi(grade));
}
students.push_back(student);
}
file.close();
int satisfactory = 0;
int good = 0;
int excellent = 0;
int totalAverage = 0;
int studentCount = students.size();
for (int i = 0; i < studentCount; i++) {
int sum = 0;
for (int grade : students[i].grades) {
sum += grade;
if (grade >= 1 && grade <= 2) {
satisfactory++;
}
else if (grade >= 3 && grade <= 4) {
good++;
}
else if (grade == 5) {
excellent++;
}
}
totalAverage += sum / students[i].grades.size();
if (sum / students[i].grades.size() < 3) {
cout << students[i].name << " не справился" << endl;
}
}
cout << "Удовлетворительных оценок: " << satisfactory << endl;
cout << "Хороших оценок: " << good << endl;
cout << "Отличных оценок: " << excellent << endl;
cout << "Средний балл в группе: " << totalAverage / studentCount << endl;
return 0;
}
Результаты выполнения программы, выводы:
Изначально не было файла. При первом запуске программы он создался:
Я добавил список студентов с их оценками из программы 12_1:
Задание 3
Условие задачи:
Реализовать программу 9_2 через структуры.
С хема алгоритма:
Текст программы:
#include <iostream>
#include <iomanip>
#include <random>
struct Array {
int* data;
int size;
};
int func(Array& target, int& answer_two) {
int answer_one = target.data[target.size - 2];
answer_two = target.data[target.size - 1];
return answer_one;
}
void print(Array& target) {
for (int i = 0; i < target.size; i++) {
std::cout << std::setw(3) << target.data[i] << " ";
}
}
void fill(Array& target) {
for (int i = 0; i < target.size; i++) {
target.data[i] = rand() % 21 - 10;
}
}
int main() {
setlocale(LC_ALL, "rus");
Array A = { new int[5], 5 };
Array B = { new int[6], 6 };
fill(A);
std::cout << "Массив А: ";
print(A);
fill(B);
std::cout << "\nМассив В: ";
print(B);
int answer_one, answer_two;
std::cout << "\n\nВыпавшие значения первого массива: ";
answer_one = func(A, answer_two);
std::cout << answer_one << " " << answer_two;
std::cout << "\nВыпавшие значения второго массива: ";
answer_one = func(B, answer_two);
std::cout << answer_one << " " << answer_two;
delete[] A.data;
delete[] B.data;
return 0;
}
Результаты выполнения программы, выводы: