Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iomanip>
- #include "ArgumentManager.h"
- using namespace std;
- struct book {
- book *prev;
- int book_id;
- string book_name;
- string book_author;
- book *next;
- };
- class book_lib {
- private:
- book *head;
- book *tail;
- int size;
- public:
- book_lib() {
- head = 0;
- tail = 0;
- }
- int getSize()
- {
- return size;
- }
- bool isempty() {
- return (head == 0 ? 1 : 0);
- };
- void insertbegin(int real_book_id, string real_book_name, string real_book_author) {
- if (book_search("book_id", to_string(real_book_id)) != -1)
- return;
- book *new_book = new book;
- new_book->prev = nullptr;
- new_book->book_id = real_book_id;
- new_book->book_name = real_book_name;
- new_book->book_author = real_book_author;
- new_book->next = head;
- if (head != nullptr)
- head->prev = new_book;
- head = new_book;
- if (tail == nullptr) {
- tail = new_book;
- }
- };
- void insertend(int real_book_id, string real_book_name, string real_book_author) {
- if (book_search("book_id", to_string(real_book_id)) != -1)
- return;
- book *new_book = new book;
- new_book->prev = tail;
- new_book->book_id = real_book_id;
- new_book->book_name = real_book_name;
- new_book->book_author = real_book_author;
- new_book->next = nullptr;
- if (tail != nullptr) {
- tail->next = new_book;
- }
- tail = new_book;
- if (head == nullptr) {
- head = new_book;
- }
- size++;
- };
- void insert(int pos, int real_book_id, string real_book_name, string real_book_author) {
- if (book_search("book_id", to_string(real_book_id)) != -1)
- return;
- book *new_book = new book;
- new_book = head;
- int ncounter = 0;
- if (pos >= 0 && head == NULL)
- {
- insertend(real_book_id, real_book_name, real_book_author);
- size++;
- return;
- }
- if (pos == 0) {
- insertbegin(real_book_id, real_book_name, real_book_author);
- return;
- }
- else {
- while (ncounter != pos && new_book->next != nullptr) {
- new_book = new_book->next;
- ncounter++;
- };
- if (new_book->next == nullptr) {
- insertend(real_book_id, real_book_name, real_book_author);
- size++;
- return;
- }
- else {
- book *copy_book = new book;
- copy_book->prev = new_book->prev;
- copy_book->next = new_book;
- new_book->prev->next = copy_book;
- new_book->prev = copy_book;
- copy_book->book_id = real_book_id;
- copy_book->book_name = real_book_name;
- copy_book->book_author = real_book_author;
- };
- };
- size++;
- };
- void remove(int pos) {
- if (pos >= size)
- {
- return;
- }
- book *book_to_remove = head;
- int ncounter2 = 0;
- while (ncounter2 != pos && book_to_remove != nullptr) {
- book_to_remove = book_to_remove->next;
- ncounter2++;
- };
- if (book_to_remove != nullptr) {
- if (book_to_remove->prev != nullptr) {
- book_to_remove->prev->next = book_to_remove->next;
- }
- else {
- head = book_to_remove->next;
- };
- if (book_to_remove->next != nullptr) {
- book_to_remove->next->prev = book_to_remove->prev;
- }
- else {
- tail = book_to_remove->prev;
- };
- delete book_to_remove;
- };
- size--;
- };
- void sort(string key) {
- int swap_val;
- book *book_to_swap;
- if (isempty())
- return;
- do
- {
- swap_val = 0;
- book_to_swap = head;
- while (book_to_swap->next != nullptr)
- {
- if (key == "book_id") {
- if (book_to_swap->book_id > book_to_swap->next->book_id) {
- swap(book_to_swap->book_id, book_to_swap->next->book_id);
- swap(book_to_swap->book_name, book_to_swap->next->book_name);
- swap(book_to_swap->book_author, book_to_swap->next->book_author);
- swap_val = 1;
- }
- }
- else if (key == "book_name") {
- if (book_to_swap->book_name > book_to_swap->next->book_name) {
- swap(book_to_swap->book_id, book_to_swap->next->book_id);
- swap(book_to_swap->book_name, book_to_swap->next->book_name);
- swap(book_to_swap->book_author, book_to_swap->next->book_author);
- swap_val = 1;
- }
- }
- else if (key == "book_author") {
- if (book_to_swap->book_author > book_to_swap->next->book_author) {
- swap(book_to_swap->book_id, book_to_swap->next->book_id);
- swap(book_to_swap->book_name, book_to_swap->next->book_name);
- swap(book_to_swap->book_author, book_to_swap->next->book_author);
- swap_val = 1;
- }
- }
- book_to_swap = book_to_swap->next;
- }
- } while (swap_val);
- }
- int book_search(string key, string value) {
- int ncounter3 = 0;
- book *book_to_find = head;
- if (key == "pos") {
- return stoi(value);
- }
- while (book_to_find != nullptr) {
- if (key == "book_id") {
- if (book_to_find->book_id == stoi(value)) {
- return ncounter3;
- }
- }
- else if (key == "book_name") {
- if (book_to_find->book_name == value) {
- return ncounter3;
- }
- }
- else if (key == "book_author") {
- if (book_to_find->book_author == value) {
- return ncounter3;
- }
- };
- ncounter3++;
- book_to_find = book_to_find->next;
- };
- return -1;
- }
- void print(bool flag = false, string file = "") {
- book *book_to_print = new book;
- book_to_print = head;
- ofstream ofile;
- if (flag == true)
- ofile.open(file);
- while (book_to_print != nullptr) {
- if (flag == false) {
- cout << "book_id:" << book_to_print->book_id
- << ", book_name:" << book_to_print->book_name
- << ", book_author:" << book_to_print->book_author << endl;
- }
- else {
- ofile << "book_id:" << book_to_print->book_id
- << ", book_name:" << book_to_print->book_name
- << ", book_author:" << book_to_print->book_author << endl;
- }
- book_to_print = book_to_print->next;
- };
- if (ofile.is_open())
- ofile.close();
- };
- };
- int main(int argc, char** argv) {
- if (argc < 2) {
- std::cout <<
- "Usage: ListOperation input=input1.txt command=command1.txt output=output1.txt"
- << std::endl;
- return -1;
- }
- ArgumentManager am(argc, argv);
- const string A = am.get("input");
- const string B = am.get("command");
- const string C = am.get("output");
- int n = 0;
- string line = "";
- book_lib list;
- ifstream ifileone(A);
- if (ifileone.is_open())
- {
- while (getline(ifileone, line))
- {
- if (line.length() != 0) {
- int book_id = stoi(line.substr(line.find("book_id:") + 8, 5));
- string book_name = line.substr(line.find("book_name:") + 10, line.find(", book_author") - 25);
- string book_author = line.substr(line.find("book_author:") + 12);
- list.insertend(book_id, book_name, book_author);
- n++;
- }
- }
- ifileone.close();
- }
- ifstream ifiletwo(B);
- if (ifiletwo.is_open())
- {
- while (getline(ifiletwo, line))
- {
- if (line.length() != 0) {
- if (line.substr(0, line.find(" ")) == "add") {
- //int book_id = stoi(line.substr(line.find("book_id:") + 8, 5));
- //string book_name = line.substr(line.find("book_name:") + 10, line.find(", book_author") - 35);
- //string book_author = line.substr(line.find("book_author:") + 12);
- //int name_pos = stoi(line.substr(line.find("pos:") + 4, line.find("book_name:") + 10));
- string a = line.substr(line.find("book_author"));
- string book_author = a.substr(12);
- string n = line.substr(line.find("book_name"));
- string book_name = n.substr(10, n.length() - a.length() - 12);
- string c = line.substr(line.find("book_id"));
- string e = c.substr(8, c.length() - n.length() - 10);
- int book_id = stoi(e);
- string d = line.substr(line.find("pos:"));
- int name_pos = stoi(d.substr(4, d.length() - c.length() - 5));
- list.insert(name_pos, book_id, book_name, book_author);
- }
- else if (line.substr(0, line.find(" ")) == "sort") {
- list.sort(line.substr(line.find(" ") + 1));
- }
- else if (line.substr(0, line.find(" ")) == "remove") {
- int temp = 0;
- while (list.book_search(line.substr(line.find(" ") + 1, line.find(":") - 7),
- line.substr(line.find(":") + 1)) != -1 && temp != list.getSize() && list.getSize() != 0) {
- list.remove(list.book_search(line.substr(line.find(" ") + 1, line.find(":") - 7),
- line.substr(line.find(":") + 1)));
- temp++;
- }
- }
- n++;
- }
- }
- ifiletwo.close();
- }
- list.print(true, C);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement