Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <iterator>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <iomanip>
- using namespace std;
- class Cell
- {
- public:
- string Data;
- Cell *Nxt;
- Cell()
- {
- Data = "";
- Nxt = NULL;
- }
- };
- class Row
- {
- public:
- Cell* Head;
- Cell* Tail;
- Row* Nxt;
- Row()
- {
- Head = NULL;
- Tail = NULL;
- for (int i = 0; i < 6; i++)
- {
- Cell *Tmp = new Cell;
- if (this->Head == NULL)
- this->Head = this->Tail = Tmp;
- else
- {
- this->Tail->Nxt = Tmp;
- this->Tail = Tmp;
- }
- }
- }
- ~Row()
- {
- while (Head != NULL)
- {
- Cell *Tmp = Head->Nxt;
- delete Head;
- Head = Tmp;
- }
- Tail = NULL;
- }
- };
- class TTable
- {
- public:
- int RowNum;
- Row* HeadPoint;
- Row* TailPoint;
- TTable()
- {
- HeadPoint = NULL;
- }
- ~TTable()
- {
- while (HeadPoint != NULL)
- {
- Row *Tmp = HeadPoint->Nxt;
- delete HeadPoint;
- HeadPoint = Tmp;
- }
- TailPoint = NULL;
- }
- bool FileGet(string Path)
- {
- ifstream File_in(Path);
- if (File_in.is_open())
- {
- Row *TmpRow = new Row;
- string Line;
- while (getline(File_in, Line))
- {
- stringstream StrStream;
- StrStream << Line;
- if (IsCorrect(Line))
- {
- if (HeadPoint == NULL)
- HeadPoint = TailPoint = TmpRow;
- else
- {
- TailPoint->Nxt = TmpRow;
- TailPoint = TmpRow;
- }
- string str;
- for (int i = 0; i < 6; i++)
- {
- StrStream >> str;
- Add(TmpRow, str);
- }
- TmpRow->Nxt = new Row;
- TmpRow = TmpRow->Nxt;
- RowNum++;
- }
- else
- cout << "Ошибка чтения из файла\n";
- }
- File_in.close();
- return true;
- }
- else
- {
- cout << "Файл не найден.";
- return false;
- }
- }
- void FileSet(string Path)
- {
- ofstream File_out(Path);
- if (File_out.is_open())
- {
- string Line;
- int i = 0;
- for (Row *TmpRow = HeadPoint; TmpRow, TmpRow->Nxt; TmpRow = TmpRow->Nxt, i++)
- {
- string Line = RowStr(TmpRow);
- File_out << Line;
- if (i != RowNum - 1)
- File_out << '\n';
- }
- File_out.close();
- }
- else
- {
- cout << "Файл не найден.";
- }
- }
- void Add(Row* row, string str)
- {
- Cell* Tmp = new Cell;
- if (Tmp == NULL)
- return;
- Tmp->Data = str;
- if (row->Head->Data == "")
- {
- row->Head = Tmp;
- row->Tail = Tmp;
- }
- else
- {
- row->Tail->Nxt = Tmp;
- row->Tail = Tmp;
- }
- }
- void AddRow()
- {
- cout << "Введите информацию о студенте:\n";
- string row;
- cin.ignore();
- getline(cin, row);
- if (IsCorrect(row))
- {
- stringstream StrStream;
- StrStream << row;
- Row *TmpRow = new Row;
- if (HeadPoint == NULL)
- HeadPoint = TailPoint = TmpRow;
- else
- {
- TailPoint->Nxt = TmpRow;
- TailPoint = TmpRow;
- }
- string str;
- for (int i = 0; i < 6; i++)
- {
- StrStream >> str;
- Add(TmpRow, str);
- }
- RowNum++;
- }
- }
- void DeleteRow()
- {
- int Val = 0;
- cout << "Введите индекс столбца: "; cin >> Val;
- if (Val < 1 || Val > 6)
- {
- cout << "Неверный индекс\n";
- return;
- }
- string Data;
- cout << "Введите значение: "; cin >> Data;
- int Position;
- if (CellCheck(Data, Val))
- Position = FindRow(Val, Data);
- else
- Position = -1;
- if (Position != -1)
- {
- Row* TmpRow = HeadPoint;
- if (Position == 0)
- {
- HeadPoint = TmpRow->Nxt;
- free(TmpRow);
- return;
- }
- for (int i = 0; TmpRow != NULL && i < Position - 1; i++)
- TmpRow = TmpRow->Nxt;
- if (TmpRow == NULL || TmpRow->Nxt == NULL)
- return;
- Row* Nxt = TmpRow->Nxt->Nxt;
- free(TmpRow->Nxt);
- TmpRow->Nxt = Nxt;
- RowNum--;
- }
- }
- void ChangeRow()
- {
- int Val = 0;
- cout << "Введите индекс столбца: "; cin >> Val;
- if (Val < 1 || Val > 6)
- {
- cout << "Неверный индекс\n";
- return;
- }
- string Data;
- cout << "Введите значение: "; cin >> Data;
- int Position;
- if (CellCheck(Data, Val))
- Position = FindRow(Val, Data);
- else
- Position = -1;
- if (Position != -1)
- {
- Row* TmpRow = HeadPoint;
- Row* Cur = HeadPoint;
- for (int i = 0; i < Position; i++)
- {
- TmpRow = Cur;
- Cur = Cur->Nxt;
- }
- if (Cur != NULL)
- {
- cout << "Найденная строка:\n";
- PrntRow(Cur);
- cout << "Введите новую строку: \n";
- string Line;
- cin.ignore();
- while (true)
- {
- getline(cin, Line);
- if (IsCorrect(Line))
- {
- Cell *TmpCell = Cur->Head;
- stringstream StrStream;
- StrStream << Line;
- string str;
- for (int i = 0; i < 6; i++)
- {
- StrStream >> str;
- TmpCell->Data = str;
- TmpCell = TmpCell->Nxt;
- }
- Cur = Cur->Nxt;
- break;
- }
- else
- {
- Line.clear();
- cout << "Введите строку еще раз: ";
- }
- }
- }
- else
- cout << "N/A";
- }
- }
- void Sort()
- {
- int Num;
- cout << "Введите номер столбца: "; cin >> Num;
- if (Num >= 1 && Num <= 6)
- {
- Row *rowHead = HeadPoint;
- Row *FirstNode, *SecondNode;
- for (FirstNode = rowHead; FirstNode, FirstNode->Nxt; FirstNode = FirstNode->Nxt)
- {
- for (SecondNode = rowHead; SecondNode, SecondNode->Nxt; SecondNode = SecondNode->Nxt)
- {
- Cell *TmpCellFirst = FirstNode->Head;
- Cell *TmpCellSecond = SecondNode->Head;
- for (int i = 0; i < Num - 1; i++)
- {
- TmpCellFirst = TmpCellFirst->Nxt;
- TmpCellSecond = TmpCellSecond->Nxt;
- }
- if (strcmp(TmpCellFirst->Data.c_str(), TmpCellSecond->Data.c_str()) < 0)
- swap(FirstNode->Head, SecondNode->Head);
- }
- }
- }
- else
- {
- cout << "Неверный номер элемента\n";
- return;
- }
- }
- void Search()
- {
- int Value = 0, Colon = 0;
- cout << "Введите номер столбца: ";
- cin >> Colon;
- cout << "\n";
- cout << "По какому критерию будет проводиться поиск?\n";
- cout << "Чтобы найти самый большой элемент, введите 1\n";
- cout << "Чтобы найти самый маленький элемент, введите 2\n";
- cout << "Чтобы найти первый четный элемент, введите 3\n";
- cout << "Чтобы найти первый нечетный элемент, введите 4\n";
- cout << "Чтобы найти первый положительный элемент, введите 5\n";
- cout << "Чтобы найти первый отрицательный элемент, введите 6\n";
- cout << "Чтобы найти все позиции указанного элемента, введите 7\n"; cin >> Value;
- if (Colon >= 1 && Colon <= 6)
- {
- switch (Value)
- {
- case 1:
- {
- int max = 0;
- int Count = 0;
- Row *TmpRow = HeadPoint;
- int size = HeadPoint->Head->Data.size();
- for (; TmpRow, TmpRow->Nxt; TmpRow = TmpRow->Nxt, Count++)
- {
- Cell *TmpCell = TmpRow->Nxt->Head;
- for (int i = 0; i < Colon - 1; i++)
- TmpCell = TmpCell->Nxt;
- if (TmpCell->Data.size() > size && TmpCell->Data.size() != 0)
- {
- size = TmpCell->Data.size();
- max = Count + 1;
- }
- }
- cout << "Самую большую строку содержит: " << max + 1 << "-й столбец\n";
- break;
- }
- case 2:
- {
- int min = 0;
- int Count = 0;
- Row *TmpRow = HeadPoint;
- int size = HeadPoint->Head->Data.size();
- for (; TmpRow, TmpRow->Nxt; TmpRow = TmpRow->Nxt, Count++)
- {
- Cell *TmpCell = TmpRow->Nxt->Head;
- for (int i = 0; i < Colon - 1; i++)
- TmpCell = TmpCell->Nxt;
- if (TmpCell->Data.size() < size && TmpCell->Data.size() != 0)
- {
- size = TmpCell->Data.size();
- min = Count + 1;
- }
- }
- cout << "Самую маленькую строку содержит: " << min + 1 << "-й столбец\n";
- break;
- }
- case 3:
- {
- system("cls");
- int Even = 0;
- int Count = 0;
- Row *TmpRow = HeadPoint;
- for (; TmpRow, TmpRow->Nxt; TmpRow = TmpRow->Nxt, Count++)
- {
- Cell *TmpCell = TmpRow->Head;
- for (int i = 0; i < Colon - 1; i++)
- TmpCell = TmpCell->Nxt;
- if (TmpCell->Data.size() % 2 == 0 && TmpCell->Data.size() != 0)
- {
- Even = Count + 1;
- break;
- }
- }
- if (Even != 0)
- cout << "Первую четную строку содержит " << Even << "-й столбец\n";
- else
- cout << "Четные элементы отсутствуют" << "\n\n";
- break;
- }
- case 4:
- {
- system("cls");
- int Uneven = 0;
- int Count = 0;
- Row *TmpRow = HeadPoint;
- for (; TmpRow, TmpRow->Nxt; TmpRow = TmpRow->Nxt, Count++)
- {
- Cell *TmpCell = TmpRow->Head;
- for (int i = 0; i < Colon - 1; i++)
- TmpCell = TmpCell->Nxt;
- if (TmpCell->Data.size() % 2 != 0 && TmpCell->Data.size() != 0)
- {
- Uneven = Count + 1;
- break;
- }
- }
- if (Uneven != 0)
- cout << "Первую нечетную строку содержит " << Uneven << "-й столбец\n";
- else
- cout << "Нечетные элементы отсутствуют" << "\n\n";
- break;
- }
- case 5:
- {
- system("cls");
- int Plus = 0;
- int Count = 0;
- Row *TmpRow = HeadPoint;
- for (; TmpRow, TmpRow->Nxt; TmpRow = TmpRow->Nxt, Count++)
- {
- Cell *TmpCell = TmpRow->Head;
- for (int i = 0; i < Colon - 1; i++)
- TmpCell = TmpCell->Nxt;
- if (TmpCell->Data.size() > 0 && TmpCell->Data.size() != 0)
- {
- Plus = Count + 1;
- break;
- }
- }
- if (Plus != 0)
- cout << "Первую положительную строку содержит " << Plus << "-й столбец\n";
- else
- cout << "Положительные элементы отсутствуют" << "\n\n";
- break;
- }
- case 6:
- {
- system("cls");
- int Minus = 0;
- int Count = 0;
- Row *TmpRow = HeadPoint;
- for (; TmpRow, TmpRow->Nxt; TmpRow = TmpRow->Nxt, Count++)
- {
- Cell *TmpCell = TmpRow->Head;
- for (int i = 0; i < Colon - 1; i++)
- TmpCell = TmpCell->Nxt;
- if (TmpCell->Data.size() < 0 && TmpCell->Data.size() != 0)
- {
- Minus = Count + 1;
- break;
- }
- }
- if (Minus != 0)
- cout << "Первую отрицательную строку содержит " << Minus << "-й столбец\n";
- else
- cout << "Отрицательные элементы отсутствуют" << "\n\n";
- break;
- }
- case 7:
- {
- system("cls");
- string str = "";
- cout << "Введите строку: "; cin >> str;
- cout << "Все позиции узазанной строки: ";
- int Count = 0;
- Row *TmpRow = HeadPoint;
- for (; TmpRow, TmpRow->Nxt; TmpRow = TmpRow->Nxt, Count++)
- {
- Cell *TmpCell = TmpRow->Head;
- for (int i = 0; i < Colon - 1; i++)
- TmpCell = TmpCell->Nxt;
- if (strcmp(TmpCell->Data.c_str(), str.c_str()) == 0 && TmpCell->Data.size() != 0)
- cout << Count + 1 << ", ";
- }
- cout << "\n\n";
- break;
- }
- default: cout << "\n...Не то число...\n\n";
- }
- }
- else
- {
- cout << "Неверный номер столбца";
- }
- }
- void Print()
- {
- if (HeadPoint == NULL)
- return;
- Row *TmpRow = HeadPoint;
- cout << "\n\n " << setw(19) << "1.Фамилия" << setw(19) << "2.Имя" << setw(19) << "3.Группа" << setw(19) << "4.Специальность" << setw(19) << "5.Год рождения" << setw(19) << "6.Телефон";
- cout << '\n';
- int i = 0;
- while (TmpRow && TmpRow->Nxt)
- {
- cout << i + 1 << '.';
- PrntRow(TmpRow);
- TmpRow = TmpRow->Nxt;
- i++;
- }
- cout << "\n";
- }
- void PrntRow(Row* row)
- {
- if (row == NULL)
- return;
- Cell *TmpCell = row->Head;
- while (TmpCell)
- {
- cout << setw(19) << TmpCell->Data;
- TmpCell = TmpCell->Nxt;
- }
- cout << '\n';
- }
- bool CellCheck(string Data, int Position)
- {
- switch (Position)
- {
- case 1:
- {
- if (!IsSurname(Data))
- return false;
- else
- return true;
- }
- case 2:
- {
- if (!IsName(Data))
- return false;
- else
- return true;
- }
- case 3:
- {
- if (!IsGroup(Data))
- return false;
- else
- return true;
- }
- case 4:
- {
- if (!IsSpecialty(Data))
- return false;
- else
- return true;
- }
- case 5:
- {
- if (!IsBirthYear(Data))
- return false;
- else
- return true;
- }
- case 6:
- {
- if (!IsPhoneNumber(Data))
- return false;
- else
- return true;
- }
- default: cout << "\nНеверный индекс\n";
- }
- }
- int FindRow(int ColonNum, string Data)
- {
- if (HeadPoint == NULL)
- return -1;
- Row* TmpRow = HeadPoint;
- int Position = 0;
- while (TmpRow)
- {
- Cell* TmpCell = TmpRow->Head;
- for (int i = 0; i < ColonNum; i++)
- {
- if (strcmp(TmpCell->Data.c_str(), Data.c_str()) == 0)
- return Position;
- TmpCell = TmpCell->Nxt;
- }
- TmpRow = TmpRow->Nxt;
- Position++;
- }
- cout << "\nЗначение не найдено\n";
- return -1;
- }
- string CellDataSearch(Row* row, int Position)
- {
- Cell* TmpCell = row->Head;
- for (int i = 0; i < Position - 1; i++)
- {
- TmpCell = TmpCell->Nxt;
- }
- return TmpCell->Data;
- }
- bool IsCorrect(string Line)
- {
- stringstream StrStream; StrStream << Line;
- string str;
- StrStream >> str;
- if (!IsSurname(str))
- return false;
- StrStream >> str;
- if (!IsName(str))
- return false;
- StrStream >> str;
- if (!IsGroup(str))
- return false;
- StrStream >> str;
- if (!IsSpecialty(str))
- return false;
- StrStream >> str;
- if (!IsBirthYear(str))
- return false;
- StrStream >> str;
- if (!IsPhoneNumber(str))
- return false;
- return true;
- }
- string RowStr(Row* row)
- {
- string str;
- Cell* TmpCell = row->Head;
- for (int i = 0; i < 6; TmpCell = TmpCell->Nxt, i++)
- {
- if (i != 5)
- str += TmpCell->Data + ' ';
- else
- {
- str += TmpCell->Data;
- }
- }
- return str;
- }
- bool IsSurname(string Surname)
- {
- if (Surname.length() > 20)
- {
- cout << "Слишком длинная фамилия\n";
- return false;
- }
- for (int i = 0; i < Surname.length(); i++)
- {
- if (((Surname[i] < 65 || Surname[i] > 122) || (Surname[i] > 90 && Surname[i] < 97)) && Surname[i] != '-')
- {
- cout << "Некорректный символ: " << Surname[i] << '\n';
- return false;
- }
- }
- return true;
- }
- bool IsName(string Name)
- {
- if (Name.length() > 10)
- {
- cout << "Слишком длинное имя\n";
- return false;
- }
- for (int i = 0; i < Name.length(); i++)
- {
- if ((Name[i] < 65 || Name[i] > 122) || (Name[i] > 90 && Name[i] < 97))
- {
- cout << "Некорректный символ: " << Name[i] << '\n';
- return false;
- }
- }
- return true;
- }
- bool IsGroup(string Group)
- {
- if (Group.length() > 10)
- {
- cout << "Слишком длинное название группы\n";
- return false;
- }
- for (int i = 0; i < Group.length(); i++)
- {
- if (((Group[i] < 48 || Group[i] > 122) || (Group[i] > 57 && Group[i] < 65) || (Group[i] > 90 && Group[i] < 97)) && Group[i] != '-')
- {
- cout << "Некорректный символ: " << Group[i] << '\n';
- return false;
- }
- }
- return true;
- }
- bool IsSpecialty(string Speciality)
- {
- if (Speciality.length() > 30)
- {
- cout << "Слишком длинное название специальности\n";
- return false;
- }
- for (int i = 0; i < Speciality.length(); i++)
- {
- if ((Speciality[i] < 65 || Speciality[i] > 122) || (Speciality[i] > 90 && Speciality[i] < 97))
- {
- cout << "Некорректный символ: " << Speciality[i] << '\n';
- return false;
- }
- }
- return true;
- }
- bool IsBirthYear(string BirthYear)
- {
- if (BirthYear.length() != 4)
- {
- cout << "Некорректный год\n";
- return false;
- }
- for (int i = 0; i < BirthYear.length(); i++)
- {
- if (BirthYear[i] < 48 || BirthYear[i] > 57)
- {
- cout << "Некорректный символ: " << BirthYear[i] << '\n';
- return false;
- }
- }
- return true;
- }
- bool IsPhoneNumber(string PhoneNumber)
- {
- if (PhoneNumber.length() != 10)
- {
- cout << "Некорректный номер\n";
- return false;
- }
- for (int i = 0; i < PhoneNumber.length(); i++)
- {
- if (PhoneNumber[i] < 48 || PhoneNumber[i] > 57)
- {
- cout << "Некорректный символ: " << PhoneNumber[i] << '\n';
- return false;
- }
- }
- return true;
- }
- void Actions(string Path)
- {
- int Value = 0;
- do
- {
- cout << "Выбирайте действие:\n";
- cout << "Чтобы добавить элемент в массив, введите 1\n";
- cout << "Чтобы удалить элемент из массива, введите 2\n";
- cout << "Чтобы заменить элемент в массиве, введите 3\n";
- cout << "Чтобы сортировать массив, введите 4\n";
- cout << "Чтобы искать элемент в массиве, введите 5\n";
- cout << "Чтобы вывести таблицу на экран, введите 6\n";
- cout << "Чтобы покинуть программу, введите любой другой символ\n";
- cout << "Ваше действие: "; cin >> Value;
- switch (Value)
- {
- case 1:
- {
- system("cls");
- AddRow();
- FileSet(Path);
- Print();
- break;
- }
- case 2:
- {
- system("cls");
- DeleteRow();
- FileSet(Path);
- Print();
- break;
- }
- case 3:
- {
- system("cls");
- ChangeRow();
- FileSet(Path);
- Print();
- break;
- }
- case 4:
- {
- system("cls");
- Sort();
- FileSet(Path);
- Print();
- break;
- }
- case 5:
- {
- system("cls");
- Search();
- Print();
- break;
- }
- case 6:
- {
- system("cls");
- Print();
- break;
- }
- default: cout << "Всего доброго!\n";
- }
- } while (Value > 0 && Value < 7);
- }
- };
- int main()
- {
- setlocale(LC_ALL, "Russian");
- string Path = "D:\\C#\\DB_Lab-4\\DB_Lab-4\\Students.txt";
- TTable NewTable;
- if (NewTable.FileGet(Path))
- NewTable.Actions(Path);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement