Курсач - програмування на С

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Національний університет «Одеська політехніка»

Інститут комп'ютерних систем


Кафедра інформаційних систем

КУРСОВА РОБОТА

з дисципліни «Алгоритмізація та програмування»

Тема «Програмування динамічної структури даних – черга»

Студента (ки) __1__ курсу АІ-226 групи


Спеціальності 122 – «Комп’ютерні
науки»
_______Каракушан К.Г._________
(прізвище та ініціали)

Керівник ст. викладач . PhD


Іванов Олексій Володимирович
(посада, вчене звання, науковий ступінь, прізвище та ініціали)

Національна шкала ______________________


Кількість балів: __________
Оцінка: ECTS _____

Члени комісії ________________ ___________________________


(підпис) (прізвище та ініціали)
________________ ___________________________
(підпис) (прізвище та ініціали)
________________ ___________________________
(підпис) (прізвище та ініціали)

м. Одеса – 2023 рік


2
Національний університет «Одеська політехніка»
Інститут комп'ютерних систем
Кафедра інформаційних систем

ЗАВДАННЯ

НА КУРСОВУ РОБОТУ

студенту Каракушан Катерині Геннадіїівні група АІ-226

1. Тема роботи
«Програмування динамічної структури даних – черга»

2. Термін здачі студентом закінченої роботи 16.06.2023

3. Початкові дані до проекту (роботи)


Варіант 12
Предметна область – вивезення відходів з підприємства. Реалізувати
динамічну структуру даних (черга), що містить наступну інформацію: код
підприємства, найменування, адреса, телефон, код відходу, найменування,
агрегатний стан, дата вивезення, кількість, вартість послуги вивезення.
Програма повинна забезпечувати:  додавання елемента;  видалення
елемента;  можливість коригування даних;  виведення всіх даних; 
формування списку підприємств, які вивозили певний вид відходів в задану
дату;  розрахунок вартості наданих послуг з вивезення певного виду
відходів з заданого підприємства;  пошук всіх підприємств, розташованих
на заданій вулиці;  розрахунок загальної кількості вивезених відходів з
підприємства за заданий інтервал часу;  сортування по полю кількість,
вартість послуги
4. Зміст розрахунково-пояснювальної записки (перелік питань, які належить
розробити)
Вступ. Теоретичні відомості про чергу. Програмна реалізація черги.
Інструкція користувача. Висновки.

5. Перелік графічного матеріалу (з точним зазначенням обов’язкових


креслень)
Блок-схема алгоритму – 1 аркуш формату А1.
3
Завдання видано 20.03.23 ______________
(підпис викладача)

Завдання прийнято до виконання 20.03.23 ______________


(підпис студента)
4
КОД ПРОГРАМИ

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct waste {


int company_code;
char company_name[50];
char address[100];
char phone[20];
int waste_code;
char waste_name[50];
char aggregate_state[20];
char disposal_date[20];
int quantity;
float disposal_cost;
struct waste *next;
} Waste;

Waste *head = NULL;


Waste *tail = NULL;

void enqueue(Waste data) {


Waste *new_node = (Waste *)malloc(sizeof(Waste));
*new_node = data;
new_node->next = NULL;

if (head == NULL) {
head = new_node;
tail = new_node;
} else {
tail->next = new_node;
tail = new_node;
}
}

void dequeue() {
if (head == NULL) {
printf("Queue is empty.\n");
return;
}

Waste *temp = head;


head = head->next;

if (head == NULL) {
tail = NULL;
}

free(temp);
5
}

void update_data(int company_code, Waste new_data) {


Waste *current = head;

while (current != NULL) {


if (current->company_code == company_code) {
*current = new_data;
return;
}
current = current->next;
}

printf("Company code not found.\n");


}

void display_all() {
Waste *current = head;

while (current != NULL) {


printf("Company code: %d\n", current->company_code);
printf("Company name: %s\n", current->company_name);
printf("Address: %s\n", current->address);
printf("Phone: %s\n", current->phone);
printf("Waste code: %d\n", current->waste_code);
printf("Waste name: %s\n", current->waste_name);
printf("Aggregate state: %s\n", current->aggregate_state);
printf("Disposal date: %s\n", current->disposal_date);
printf("Quantity: %d\n", current->quantity);
printf("Disposal cost: %.2f\n", current->disposal_cost);
printf("\n");

current = current->next;
}
}

void display_companies_by_waste_and_date(int waste_code, char *date) {


Waste *current = head;

while (current != NULL) {


if (current->waste_code == waste_code && strcmp(current->disposal_date, date) == 0) {
printf("Company code: %d\n", current->company_code);
printf("Company name: %s\n", current->company_name);
printf("\n");
}
current = current->next;
}
}

float calculate_disposal_cost(int company_code, int waste_code) {


float total_cost = 0.0;
6
Waste *current = head;

while (current != NULL) {


if (current->company_code == company_code && current->waste_code == waste_code) {
total_cost += current->disposal_cost;
}
current = current->next;
}

return total_cost;
}

void display_companies_by_street(char *street) {


Waste *current = head;

while (current != NULL) {


if (strstr(current->address, street) != NULL) {
printf("Company code: %d\n", current->company_code);
printf("Company name: %s\n", current->company_name);
printf("\n");
}
current = current->next;
}
}

int calculate_total_waste_quantity(int company_code, char *start_date, char *end_date) {


int total_quantity = 0;
Waste *current = head;

while (current != NULL) {


if (current->company_code == company_code && strcmp(current->disposal_date,
start_date) >= 0 && strcmp(current->disposal_date, end_date) <= 0) {
total_quantity += current->quantity;
}
current = current->next;
}

return total_quantity;
}

void sort_by_quantity() {
if (head == NULL) {
return;
}

int swapped;
Waste *current;
Waste *last = NULL;

do {
swapped = 0;
current = head;
7

while (current->next != last) {


if (current->quantity > current->next->quantity) {
Waste temp = *current;
*current = *(current->next);
*(current->next) = temp;
swapped = 1;
}
current = current->next;
}
last = current;
} while (swapped);
}

void sort_by_cost() {
if (head == NULL) {
return;
}

int swapped;
Waste *current;
Waste *last = NULL;

do {
swapped = 0;
current = head;

while (current->next != last) {


if (current->disposal_cost > current->next->disposal_cost) {
Waste temp = *current;
*current = *(current->next);
*(current->next) = temp;
swapped = 1;
}
current = current->next;
}
last = current;
} while (swapped);
}

int main() {
int choice;

do {
printf("1. Add data\n");
printf("2. Remove data\n");
printf("3. Update data\n");
printf("4. Display all data\n");
printf("5. Display companies by waste and date\n");
printf("6. Calculate disposal cost\n");
printf("7. Display companies by street\n");
printf("8. Calculate total waste quantity\n");
8
printf("9. Sort by quantity\n");
printf("10. Sort by cost\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1: {
Waste data;

printf("Enter company code: ");


scanf("%d", &data.company_code);

printf("Enter company name: ");


scanf("%s", data.company_name);

printf("Enter address: ");


scanf("%s", data.address);

printf("Enter phone: ");


scanf("%s", data.phone);

printf("Enter waste code: ");


scanf("%d", &data.waste_code);

printf("Enter waste name: ");


scanf("%s", data.waste_name);

printf("Enter aggregate state: ");


scanf("%s", data.aggregate_state);

printf("Enter disposal date: ");


scanf("%s", data.disposal_date);

printf("Enter quantity: ");


scanf("%d", &data.quantity);

printf("Enter disposal cost: ");


scanf("%f", &data.disposal_cost);

enqueue(data);
break;
}
case 2: {
dequeue();
break;
}
case 3: {
int company_code;
Waste new_data;

printf("Enter company code to update: ");


9
scanf("%d", &company_code);

printf("Enter new company code: ");


scanf("%d", &new_data.company_code);

printf("Enter new company name: ");


scanf("%s", new_data.company_name);

printf("Enter new address: ");


scanf("%s", new_data.address);

printf("Enter new phone: ");


scanf("%s", new_data.phone);

printf("Enter new waste code: ");


scanf("%d", &new_data.waste_code);

printf("Enter new waste name: ");


scanf("%s", new_data.waste_name);

printf("Enter new aggregate state: ");


scanf("%s", new_data.aggregate_state);

printf("Enter new disposal date: ");


scanf("%s", new_data.disposal_date);

printf("Enter new quantity: ");


scanf("%d", &new_data.quantity);

printf("Enter new disposal cost: ");


scanf("%f", &new_data.disposal_cost);

update_data(company_code, new_data);
break;
}
case 4:
display_all();
break;
case 5: {
int waste_code;
char date[20];

printf("Enter waste code: ");


scanf("%d", &waste_code);

printf("Enter date: ");


scanf("%s", date);

display_companies_by_waste_and_date(waste_code, date);
break;
}
case 6: {
10
int company_code, waste_code;

printf("Enter company code: ");


scanf("%d", &company_code);

printf("Enter waste code: ");


scanf("%d", &waste_code);

float total_cost = calculate_disposal_cost(company_code, waste_code);


printf("Total disposal cost: %.2f\n", total_cost);
break;
}
case 7: {
char street[50];

printf("Enter street: ");


scanf("%s", street);

display_companies_by_street(street);
break;
}
case 8: {
int company_code;
char start_date[20], end_date[20];

printf("Enter company code: ");


scanf("%d", &company_code);

printf("Enter start date: ");


scanf("%s", start_date);

printf("Enter end date: ");


scanf("%s", end_date);

int total_quantity = calculate_total_waste_quantity(company_code, start_date,


end_date);
printf("Total waste quantity: %d\n", total_quantity);
break;
}
case 9: {
sort_by_quantity();
break;
}
case 10: {
sort_by_cost();
break;
}
}
} while (choice != 0);

return 0;
11
}

СКРИНШОТИ РЕЗУЛЬТАТІВ РОБОТИ


12
13

You might also like