Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
прога экзамен.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
29.59 Кб
Скачать

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

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

#include <iostream>

using namespace std;

struct num

{

int n;

num *next;

};

int main()

{

num *start, *curr;

/*

где-то здесь идёт задавание списка, но оно не требуется в задании

*/

int max = 0, sr = 0;

if (start)

{

max = start -> n;

}

curr = start;

int i = 0; // отчет ведётся с нуля

while (curr -> next != nullptr)

{

if (curr -> next -> n > max)

max = curr -> next -> n;

if (i % 2 == 0)

sr += curr -> next -> n;

curr = curr -> next;

i++;

}

curr = start;

while (curr != nullptr)

{

if (curr -> n = max)

curr -> n = sr;

curr = curr -> next;

}

return 0;

}

//22. Данные об N студентах (N>5) хранятся в виде односвязного списка (ФИО, средний балл за сессию).

//Написать функции записи данных о студентах в текстовый файл и чтения из него.

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

struct stud

{

string f;

string i;

string o;

float mark;

stud *next;

};

void readtxt(stud **head, stud **last, stud **current, stud **newList)

{

ifstream fin("text.txt");

float tmp = 0.;

(*newList) = new stud;

//

fin >> (*newList) -> f;

fin >> (*newList) -> i;

fin >> (*newList) -> o;

fin >> tmp;

if (tmp > 0 && tmp <= 5)

(*newList) -> mark = tmp;

else

(*newList) -> mark = 0;

//

(*head) = (*newList);

(*newList) -> next = NULL;

(*current) = (*newList);

(*last) = (*head);

while(!fin.eof())

{

(*newList) = new stud;

//

fin >> (*newList) -> f;

fin >> (*newList) -> i;

fin >> (*newList) -> o;

fin >> tmp;

if (tmp > 0 && tmp <= 5)

(*newList) -> mark = tmp;

else

(*newList) -> mark = 0;

//

(*newList) -> next = nullptr;

(*current) -> next = (*newList);

(*last) = (*newList);

(*current) = (*newList);

}

fin.close();

}

void savetxt(stud *head)

{

ofstream fout("text.txt");

struct stud *curr;

curr = head;

while (curr)

{

fout << curr -> f << endl;

fout << curr -> i << endl;

fout << curr -> o << endl;

fout << curr -> mark << endl;

curr = curr -> next;

}

fout.close();

}

Int main()

{

stud *head = nullptr, *last = nullptr, *current = nullptr, *newList = nullptr;

/*

задавание структуры

*/

readtxt(&head, &last, &current, &newList);

savetxt(head);

return 0;

}

// 23. Написать фрагмент программы, удаляющий из односвязного кольца (список, последний элемент которого указывает на первый)

// целых чисел нечетные числа. Удаление элемента оформить в виде функции.

#include <iostream>

using namespace std;

struct num

{

int a;

num *next;

};

void delete_element(num **head, num **newList, num **last)

{

(*newList)=(*head);

num *tmp;

do

{

if((*newList)->next->a % 2 == 1)

{

tmp = (*newList)->next;

(*newList)->next = (*newList)->next->next;

delete tmp;

}

(*newList)=(*newList)->next;

} while((*newList)!=(*head));

}

Int main()

{

num *head = nullptr, *last = nullptr, *newList = nullptr;

/*

задавание кольца

*/

return 0;

}

//27. Данные об авиарейсах хранятся в односвязном списке (пункт вылета, пункт назначения, время в пути).

//Написать функции записи данных об авиарейсах в бинарный файл и чтения из него.

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

struct race

{

string v;

string n;

string time; // для формата ЧЧ:ММ:СС, но есть много способов хранить время

race *next;

};

void readbin(race **head, race **last, race **current, race **newList)

{

ifstream fin("text.dat", ios::binary|ios::in);

(*newList) = new race;

//

fin.read((char*)&(*newList) -> v, sizeof string);

fin.read((char*)&(*newList) -> n, sizeof string);

fin.read((char*)&(*newList) -> time, sizeof string);

//

(*head) = (*newList);

(*newList) -> next = NULL;

(*current) = (*newList);

(*last) = (*head);

while(!fin.eof())

{

(*newList) = new race;

//

fin.read((char*)&(*newList) -> v, sizeof string);

fin.read((char*)&(*newList) -> n, sizeof string);

fin.read((char*)&(*newList) -> time, sizeof string);

//

(*newList) -> next = nullptr;

(*current) -> next = (*newList);

(*last) = (*newList);

(*current) = (*newList);

}

fin.close();

}

void savebin(race *head)

{

ofstream fout("text.dat", ios::binary|ios::out);

struct race *curr;

curr = head;

while (curr)

{

fout.write((char*)&curr -> v, sizeof string);

fout.write((char*)&curr -> n, sizeof string);

fout.write((char*)&curr -> time, sizeof string);

curr = curr -> next;

}

fout.close();

}

Int main()

{

race *head = nullptr, *last = nullptr, *current = nullptr, *newList = nullptr;

/*

задавание структуры

*/

readbin(&head, &last, &current, &newList);

savebin(head);

return 0;

}

// 14. Написать фрагмент программы, удаляющий i-й элемент из двусвязного списка целых чисел размера N, i <= N.

// Фрагмент оформить в виде функции.

#include <iostream>

using namespace std;

struct num

{

int a;

num *next, *prev;

};

void delete_element(int &n, num **head, num **newList, num **last, num **current)

{

int choice = 0;

cout << "Enter number of element to delete" << endl;

cin >> choice;

choice--;

if (choice > n || n < 0) //если номер неккоректный

{

cout << "wrong number" << endl;

return ;

}

(*newList)=(*head);

for (int i = 0; i < choice; i++) //добегаем до этого элемента

{

(*newList)=(*newList)->next;

}

if((*newList)==(*head)) //если первый

{

(*head)=(*head)->next;

if((*head))

(*head)->prev=NULL;

else

(*last)=NULL;

if((*current)==(*newList))

(*current)=(*head);

delete (*newList);

} else

if ((*newList)==(*last)) //если последний

{

(*last)=(*last)->prev;

if((*last))

(*last)->next=NULL;

if((*current)==(*newList))

(*current)=(*last);

delete (*newList);

} else //если где-то посередине

{

(*newList)->prev->next=(*newList)->next;

(*newList)->next->prev=(*newList)->prev;

(*current)=(*newList)->prev;

delete (*newList);

(*newList)=(*newList)->next;

}

n--;

}