Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Курсовая Голиков Илья.docx
Скачиваний:
17
Добавлен:
19.01.2023
Размер:
4.11 Mб
Скачать

Приложение б – Исходный код реализации графического интерфейса с помощью библиотеки Windows Forms

Б1 – Исходный код функции main()

#include "Form1.h"

using namespace System;

using namespace System::Windows::Forms;

[STAThread]

int main(array<String^>^ arg) {

Application::EnableVisualStyles();

Application::SetCompatibleTextRenderingDefault(false);

Termpaperwinforms::Form1 form;

Application::Run(% form);

return 0;

}

Б2 – Содержимое заголовочного файла Functions.h

#pragma once

#include <string>

#include <iostream>

#include <fstream>

using namespace std;

struct ticket {

string surname;

string name;

string patr;

int day;

int month;

int year;

string from;

string to;

string luggage;

string company;

string fl_number;

int price;

int date_simp;

bool delete_param = false;

};

int count_of_lines();

void read_data(ticket* tickets, int count_of);

void qsort_date(ticket* tickets, int length);

void qsort_price(ticket* tickets, int length);

void bubble_sort_surn(ticket* tickets, int length);

void bubble_sort_comp(ticket* tickets, int length);

bool word_comparsion(string left, string right);

char MyToLower(char r);

Б3 – Исходный код функции add_item()

#include "Form1.h"

#include "Functions.h"

#include <msclr\marshal_cppstd.h>

System::Void Termpaperwinforms::Form1::sure_button_Click(System::Object^ sender, System::EventArgs^ e)

{

String^ t_line;

ofstream data;

data.open("data.txt", ios_base::app);

data << "####" << endl;

t_line = surname_abox->Text;

data << msclr::interop::marshal_as<string>(t_line) << endl;

t_line = name_abox->Text;

data << msclr::interop::marshal_as<string>(t_line) << endl;

t_line = patr_abox->Text;

data << msclr::interop::marshal_as<string>(t_line) << endl;

data << date_apicker->Value.Day << endl;

data << date_apicker->Value.Month << endl;

data << date_apicker->Value.Year << endl;

t_line = from_abox->Text;

data << msclr::interop::marshal_as<string>(t_line) << endl;

t_line = to_abox->Text;

data << msclr::interop::marshal_as<string>(t_line) << endl;

if (lugg_aradio->Checked) data << "Есть" << endl;

else data << "Нет" << endl;

t_line = company_abox->Text;

data << msclr::interop::marshal_as<string>(t_line) << endl;

t_line = fl_number_abox->Text;

data << msclr::interop::marshal_as<string>(t_line) << endl;

t_line = price_abox->Text;

data << msclr::interop::marshal_as<string>(t_line) << endl;

data.close();

surname_abox->Text = "";

name_abox->Text = "";

patr_abox->Text = "";

from_abox->Text = "";

to_abox->Text = "";

company_abox->Text = "";

fl_number_abox->Text = "";

price_abox->Text = "";

no_luggage_aradio->Checked = true;

this->sure_button->Visible = false;

this->cancel_button->Visible = false;

return System::Void();

}

Б4 – Исходный код функции count_of_lines()

#include "Functions.h"

int count_of_lines() {

string line; // For temp lines

int count_of = 0;

//Open the database in read mode

ifstream data;

data.open("data.txt");

//Count the number of elements

while (!data.eof()) {

getline(data, line);

if (line == "####") count_of++;

}

data.close();

return count_of;

}

Б5 – Исходный код функции db_output()

#include "Form1.h"

#include "Functions.h"

using namespace std;

System::Void Termpaperwinforms::Form1::button1_Click(System::Object^ sender, System::EventArgs^ e)

{

this->Dgv1->Visible = true;

int count_of = count_of_lines();

String^ line;

ticket* tickets = new ticket[count_of];

read_data(tickets, count_of);

if (radio_surname->Checked) bubble_sort_surn(tickets, count_of);

if (radio_date->Checked) qsort_date(tickets, count_of);

if (radio_price->Checked) qsort_price(tickets, count_of);

if (radio_company->Checked) bubble_sort_comp(tickets, count_of);

Dgv1->Rows->Clear();

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

Dgv1->Rows->Add();

string date = to_string(tickets[i].day) + "/" + to_string(tickets[i].month) + "/" + to_string(tickets[i].year);

Dgv1->Rows[i]->Cells[0]->Value = i+1;

line = gcnew String(tickets[i].surname.c_str());

Dgv1->Rows[i]->Cells[1]->Value = line;

line = gcnew String(tickets[i].name.c_str());

Dgv1->Rows[i]->Cells[2]->Value = line;

line = gcnew String(tickets[i].patr.c_str());

Dgv1->Rows[i]->Cells[3]->Value = line;

line = gcnew String(date.c_str());

Dgv1->Rows[i]->Cells[4]->Value = line;

line = gcnew String(tickets[i].from.c_str());

Dgv1->Rows[i]->Cells[5]->Value = line;

line = gcnew String(tickets[i].to.c_str());

Dgv1->Rows[i]->Cells[6]->Value = line;

line = gcnew String(tickets[i].luggage.c_str());

Dgv1->Rows[i]->Cells[7]->Value = line;

line = gcnew String(tickets[i].company.c_str());

Dgv1->Rows[i]->Cells[8]->Value = line;

line = gcnew String(tickets[i].fl_number.c_str());

Dgv1->Rows[i]->Cells[9]->Value = line;

Dgv1->Rows[i]->Cells[10]->Value = tickets[i].price;

}

delete[] tickets;

return System::Void();

}

Б6 – Исходный код функции delete()

#include "Form1.h"

#include "Functions.h"

System::Void Termpaperwinforms::Form1::yes_delete_Click(System::Object^ sender, System::EventArgs^ e)

{

int count_of = count_of_lines();

ticket* tickets = new ticket[count_of];

read_data(tickets, count_of);

for (int i = 0; i < count_of; i++)

{

if (Dgv3->Rows[i]->Cells[0]->Value) tickets[i].delete_param = true;

}

ofstream data;

data.open("data.txt");

for (int i = 0; i < count_of; i++)

{

if (tickets[i].delete_param) continue;

data << "####" << endl

<< tickets[i].surname << endl

<< tickets[i].name << endl

<< tickets[i].patr << endl

<< tickets[i].day << endl

<< tickets[i].month << endl

<< tickets[i].year << endl

<< tickets[i].from << endl

<< tickets[i].to << endl

<< tickets[i].luggage << endl

<< tickets[i].company << endl

<< tickets[i].fl_number << endl

<< tickets[i].price << endl;

}

data.close();

delete[] tickets;

this->button4->Visible = false;

this->yes_delete->Visible = false;

return System::Void();

}

System::Void Termpaperwinforms::Form1::button3_Click(System::Object^ sender, System::EventArgs^ e)

{

int count_of = count_of_lines();

String^ line;

ticket* tickets = new ticket[count_of];

read_data(tickets, count_of);

Dgv3->Rows->Clear();

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

Dgv3->Rows->Add();

string date = to_string(tickets[i].day) + "/" + to_string(tickets[i].month) + "/" + to_string(tickets[i].year);

line = gcnew String(tickets[i].surname.c_str());

Dgv3->Rows[i]->Cells[1]->Value = line;

line = gcnew String(tickets[i].name.c_str());

Dgv3->Rows[i]->Cells[2]->Value = line;

line = gcnew String(tickets[i].patr.c_str());

Dgv3->Rows[i]->Cells[3]->Value = line;

line = gcnew String(date.c_str());

Dgv3->Rows[i]->Cells[4]->Value = line;

line = gcnew String(tickets[i].from.c_str());

Dgv3->Rows[i]->Cells[5]->Value = line;

line = gcnew String(tickets[i].to.c_str());

Dgv3->Rows[i]->Cells[6]->Value = line;

line = gcnew String(tickets[i].luggage.c_str());

Dgv3->Rows[i]->Cells[7]->Value = line;

line = gcnew String(tickets[i].company.c_str());

Dgv3->Rows[i]->Cells[8]->Value = line;

line = gcnew String(tickets[i].fl_number.c_str());

Dgv3->Rows[i]->Cells[9]->Value = line;

Dgv3->Rows[i]->Cells[10]->Value = tickets[i].price;

}

delete[] tickets;

return System::Void();

}

Б7 – Исходный код функции read_data()

#include "Functions.h"

/// <summary>

/// The function reads data from a file data.txt and passes them to the tickets array

/// </summary>

/// <param name="tickets"> - array for storing read data</param>

/// <param name="count_of"> - length of tickets array</param>

void read_data(ticket * tickets, int count_of) {

string line; // For temp lines

int i = 0;

ifstream data;

data.open("data.txt");

//Read info about tickets and add it to array

while (i < count_of && ! data.eof()){

getline(data, line);

//Устранение ошибок чтения и позиции в файле. В крайнем случае пропускается строка, но не возникает проблем

while (line != "####") {

getline(data, line);

};

//Read data

getline(data, tickets[i].surname);

getline(data, tickets[i].name);

getline(data, tickets[i].patr);

//Read date and convert it to int

getline(data, line);

tickets[i].day = stoi(line);

getline(data, line);

tickets[i].month = stoi(line);

getline(data, line);

tickets[i].year = stoi(line);

//Read onother data

getline(data, tickets[i].from);

getline(data, tickets[i].to);

getline(data, tickets[i].luggage);

getline(data, tickets[i].company);

getline(data, tickets[i].fl_number);

getline(data, line);

tickets[i].price = stoi(line);

tickets[i].date_simp = fabs(tickets[i].year - 2000) * 365 + tickets[i].month * 30 + tickets[i].day;

i++;

}

data.close();

}

Б8 – Исходный код функции search()

#include "Form1.h"

#include "Functions.h"

#include <msclr\marshal_cppstd.h>

System::Void Termpaperwinforms::Form1::button2_Click(System::Object^ sender, System::EventArgs^ e)

{

this->Dgv2->Visible = true;

int count_of = count_of_lines();

ticket* tickets = new ticket[count_of];

read_data(tickets, count_of);

Dgv2->Rows->Clear();

if (radio_t2_surn->Checked) {

string skey, temp;

String^ line;

int count_of_rows = 0;

System::String^ search_key_S = skey_box->Text;

skey = msclr::interop::marshal_as<string>(search_key_S);

for (auto& i : skey) i = MyToLower(i);

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

//this->month_box->Visible = true;

temp = tickets[i].surname;

for (auto& i : temp) i = MyToLower(i);

if (temp.find(skey) != string::npos) {

Dgv2->Rows->Add();

string date = to_string(tickets[i].day) + "/" + to_string(tickets[i].month) + "/" + to_string(tickets[i].year);

Dgv2->Rows[count_of_rows]->Cells[0]->Value = count_of_rows + 1;

line = gcnew String(tickets[i].surname.c_str());

Dgv2->Rows[count_of_rows]->Cells[1]->Value = line;

line = gcnew String(tickets[i].name.c_str());

Dgv2->Rows[count_of_rows]->Cells[2]->Value = line;

line = gcnew String(tickets[i].patr.c_str());

Dgv2->Rows[count_of_rows]->Cells[3]->Value = line;

line = gcnew String(date.c_str());

Dgv2->Rows[count_of_rows]->Cells[4]->Value = line;

line = gcnew String(tickets[i].from.c_str());

Dgv2->Rows[count_of_rows]->Cells[5]->Value = line;

line = gcnew String(tickets[i].to.c_str());

Dgv2->Rows[count_of_rows]->Cells[6]->Value = line;

line = gcnew String(tickets[i].luggage.c_str());

Dgv2->Rows[count_of_rows]->Cells[7]->Value = line;

line = gcnew String(tickets[i].company.c_str());

Dgv2->Rows[count_of_rows]->Cells[8]->Value = line;

line = gcnew String(tickets[i].fl_number.c_str());

Dgv2->Rows[count_of_rows]->Cells[9]->Value = line;

Dgv2->Rows[count_of_rows]->Cells[10]->Value = tickets[i].price;

count_of_rows++;

}

}

}

if (radio_t2_date->Checked) {

int count_of_rows = 0;

int month, year;

INT::TryParse(month_box->Text, month);

INT::TryParse(year_box->Text, year);

String ^ line;

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

if (tickets[i].month == month && tickets[i].year == year) {

Dgv2->Rows->Add();

string date = to_string(tickets[i].day) + "/" + to_string(tickets[i].month) + "/" + to_string(tickets[i].year);

Dgv2->Rows[count_of_rows]->Cells[0]->Value = count_of_rows + 1;

line = gcnew String(tickets[i].surname.c_str());

Dgv2->Rows[count_of_rows]->Cells[1]->Value = line;

line = gcnew String(tickets[i].name.c_str());

Dgv2->Rows[count_of_rows]->Cells[2]->Value = line;

line = gcnew String(tickets[i].patr.c_str());

Dgv2->Rows[count_of_rows]->Cells[3]->Value = line;

line = gcnew String(date.c_str());

Dgv2->Rows[count_of_rows]->Cells[4]->Value = line;

line = gcnew String(tickets[i].from.c_str());

Dgv2->Rows[count_of_rows]->Cells[5]->Value = line;

line = gcnew String(tickets[i].to.c_str());

Dgv2->Rows[count_of_rows]->Cells[6]->Value = line;

line = gcnew String(tickets[i].luggage.c_str());

Dgv2->Rows[count_of_rows]->Cells[7]->Value = line;

line = gcnew String(tickets[i].company.c_str());

Dgv2->Rows[count_of_rows]->Cells[8]->Value = line;

line = gcnew String(tickets[i].fl_number.c_str());

Dgv2->Rows[count_of_rows]->Cells[9]->Value = line;

Dgv2->Rows[count_of_rows]->Cells[10]->Value = tickets[i].price;

count_of_rows++;

}

}

}

if (radio_t2_city->Checked) {

string skey, temp, temp1;

String^ line;

int count_of_rows = 0;

System::String^ search_key_S = skey_box->Text;

skey = msclr::interop::marshal_as<string>(search_key_S);

for (auto& i : skey) i = MyToLower(i);

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

temp = tickets[i].from;

temp1 = tickets[i].to;

for (auto& i : temp) i = MyToLower(i);

for (auto& i : temp1) i = MyToLower(i);

if (temp.find(skey) != string::npos || temp1.find(skey) != string::npos) {

Dgv2->Rows->Add();

string date = to_string(tickets[i].day) + "/" + to_string(tickets[i].month) + "/" + to_string(tickets[i].year);

Dgv2->Rows[count_of_rows]->Cells[0]->Value = count_of_rows + 1;

line = gcnew String(tickets[i].surname.c_str());

Dgv2->Rows[count_of_rows]->Cells[1]->Value = line;

line = gcnew String(tickets[i].name.c_str());

Dgv2->Rows[count_of_rows]->Cells[2]->Value = line;

line = gcnew String(tickets[i].patr.c_str());

Dgv2->Rows[count_of_rows]->Cells[3]->Value = line;

line = gcnew String(date.c_str());

Dgv2->Rows[count_of_rows]->Cells[4]->Value = line;

line = gcnew String(tickets[i].from.c_str());

Dgv2->Rows[count_of_rows]->Cells[5]->Value = line;

line = gcnew String(tickets[i].to.c_str());

Dgv2->Rows[count_of_rows]->Cells[6]->Value = line;

line = gcnew String(tickets[i].luggage.c_str());

Dgv2->Rows[count_of_rows]->Cells[7]->Value = line;

line = gcnew String(tickets[i].company.c_str());

Dgv2->Rows[count_of_rows]->Cells[8]->Value = line;

line = gcnew String(tickets[i].fl_number.c_str());

Dgv2->Rows[count_of_rows]->Cells[9]->Value = line;

Dgv2->Rows[count_of_rows]->Cells[10]->Value = tickets[i].price;

count_of_rows++;

}

}

}

if (radio_t2_company->Checked) {

string skey, temp;

String^ line;

int count_of_rows = 0;

System::String^ search_key_S = skey_box->Text;

skey = msclr::interop::marshal_as<string>(search_key_S);

for (auto& i : skey) i = MyToLower(i);

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

//this->month_box->Visible = true;

temp = tickets[i].company;

for (auto& i : temp) i = MyToLower(i);

if (temp.find(skey) != string::npos) {

Dgv2->Rows->Add();

string date = to_string(tickets[i].day) + "/" + to_string(tickets[i].month) + "/" + to_string(tickets[i].year);

Dgv2->Rows[count_of_rows]->Cells[0]->Value = count_of_rows + 1;

line = gcnew String(tickets[i].surname.c_str());

Dgv2->Rows[count_of_rows]->Cells[1]->Value = line;

line = gcnew String(tickets[i].name.c_str());

Dgv2->Rows[count_of_rows]->Cells[2]->Value = line;

line = gcnew String(tickets[i].patr.c_str());

Dgv2->Rows[count_of_rows]->Cells[3]->Value = line;

line = gcnew String(date.c_str());

Dgv2->Rows[count_of_rows]->Cells[4]->Value = line;

line = gcnew String(tickets[i].from.c_str());

Dgv2->Rows[count_of_rows]->Cells[5]->Value = line;

line = gcnew String(tickets[i].to.c_str());

Dgv2->Rows[count_of_rows]->Cells[6]->Value = line;

line = gcnew String(tickets[i].luggage.c_str());

Dgv2->Rows[count_of_rows]->Cells[7]->Value = line;

line = gcnew String(tickets[i].company.c_str());

Dgv2->Rows[count_of_rows]->Cells[8]->Value = line;

line = gcnew String(tickets[i].fl_number.c_str());

Dgv2->Rows[count_of_rows]->Cells[9]->Value = line;

Dgv2->Rows[count_of_rows]->Cells[10]->Value = tickets[i].price;

count_of_rows++;

}

}

}

delete[] tickets;

return System::Void();

}

Б9 – Исходный код функции sorting()

#include "Functions.h"

void qsort_date(ticket* tickets, int length) {

int i = 0;

int j = length - 1;

//Вычисление опорного элемента

int mid = tickets[length / 2].date_simp;

do {

//Проходим по массиву через i и j, пропуская не требующие перестановки элементы

while (tickets[i].date_simp < mid) {

i++;

}

while (tickets[j].date_simp > mid) {

j--;

}

//Меняем элементы местами, если i и j не пересеклись, и сразу меняем их ещё на единицу

if (i <= j) {

ticket tmp = tickets[i];

tickets[i] = tickets[j];

tickets[j] = tmp;

i++;

j--;

}

} while (i <= j);

//Если j не дошла до начала

if (j > 0) {

//Рекурсируем функцию по левому куску

qsort_date(tickets, j + 1);

}

if (i < length) {

qsort_date(&tickets[i], length - i);

}

}

void qsort_price(ticket* tickets, int length) {

int i = 0;

int j = length - 1;

//Вычисление опорного элемента

int mid = tickets[length / 2].price;

do {

//Проходим по массиву через i и j, пропуская не требующие перестановки элементы

while (tickets[i].price < mid) {

i++;

}

while (tickets[j].price > mid) {

j--;

}

//Меняем элементы местами, если i и j не пересеклись, и сразу меняем их ещё на единицу

if (i <= j) {

ticket tmp = tickets[i];

tickets[i] = tickets[j];

tickets[j] = tmp;

i++;

j--;

}

} while (i <= j);

//Если j не дошла до начала

if (j > 0) {

//Рекурсируем функцию по левому куску

qsort_price(tickets, j + 1);

}

if (i < length) {

qsort_price(&tickets[i], length - i);

}

}

void bubble_sort_surn(ticket* tickets, int length) {

bool next = true;

ticket temp;

length--;

while (next)

{

next = false;

for (int i = 0; i < length; i++)

{

if (!word_comparsion(tickets[i].surname, tickets[i + 1].surname))

{

temp = tickets[i];

tickets[i] = tickets[i + 1];

tickets[i + 1] = temp;

next = true;

}

}

}

}

void bubble_sort_comp(ticket* tickets, int length) {

bool next = true;

ticket temp;

length--;

while (next)

{

next = false;

for (int i = 0; i < length; i++)

{

if (!word_comparsion(tickets[i].company, tickets[i + 1].company))

{

temp = tickets[i];

tickets[i] = tickets[i + 1];

tickets[i + 1] = temp;

next = true;

}

}

}

}

bool word_comparsion(string left, string right) {

short i = 0;

for (i; i < 4; i++)

{

if (left[i] < right[i]) return true;

if (left[i] > right[i]) return false;

}

return true;

}

Б10 – Содержимое заголовочного файла Form1.h

Для краткости из данного блока удалён код, генерирующий пользовательский интерфейс.

private:

System::Void button1_Click(System::Object^ sender, System::EventArgs^ e);

private: System::Void radioButton2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {

this->skey_box->Visible = false;

this->month_box->Visible = true;

this->year_box->Visible = true;

this->label3->Visible = true;

this->label2->Visible = true;

}

private: System::Void radioButton1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {

this->skey_box->Visible = true;

this->month_box->Visible = false;

this->year_box->Visible = false;

this->label3->Visible = false;

this->label2->Visible = false;

}

private: System::Void radioButton3_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {

this->skey_box->Visible = true;

this->month_box->Visible = false;

this->year_box->Visible = false;

this->label3->Visible = false;

this->label2->Visible = false;

}

private: System::Void radioButton4_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {

this->skey_box->Visible = true;

this->month_box->Visible = false;

this->year_box->Visible = false;

this->label3->Visible = false;

this->label2->Visible = false;

}

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e);

private: System::Void add_button_Click(System::Object^ sender, System::EventArgs^ e) {

this->sure_button->Visible = true;

this->cancel_button->Visible = true;

}

private: System::Void sure_button_Click(System::Object^ sender, System::EventArgs^ e);

private: System::Void cancel_button_Click(System::Object^ sender, System::EventArgs^ e) {

surname_abox->Text = "";

name_abox->Text = "";

patr_abox->Text = "";

from_abox->Text = "";

to_abox->Text = "";

company_abox->Text = "";

fl_number_abox->Text = "";

price_abox->Text = "";

no_luggage_aradio->Checked = true;

this->sure_button->Visible = false;

this->cancel_button->Visible = false;

}

private: System::Void delete_button_Click(System::Object^ sender, System::EventArgs^ e) {

this->yes_delete->Visible = true;

this->button4->Visible = true;

}

private: System::Void yes_delete_Click(System::Object^ sender, System::EventArgs^ e);

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e);

private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {

this->Dgv3->Rows->Clear();

}

};

}