Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <cstdlib>
- #include <vector>
- using namespace std;
- struct data {
- int id;
- string namePL;
- string nameLA;
- string date;
- string collector;
- string comment;
- string family;
- string place;
- string status;
- };
- struct node {
- struct data data;
- struct node* next;
- struct node* prev;
- };
- struct node* head = NULL;
- struct node* last = NULL;
- void append(struct data newKarta)
- {
- struct node* new_node = (struct node*)malloc(sizeof(struct node));
- last = head;
- new_node->data = newKarta;
- new_node->next = NULL;
- if (head == NULL) {
- new_node->prev = NULL;
- head = new_node;
- return;
- }
- while (last->next != NULL)
- last = last->next;
- last->next = new_node;
- new_node->prev = last;
- return;
- } ;
- struct node* usun(int id) {
- struct node* current = head;
- struct node* previous = NULL;
- if (head == NULL) {
- return NULL;
- }
- while ((current->data).id != id) {
- if (current->next == NULL) {
- return NULL;
- }
- else {
- previous = current;
- current = current->next;
- }
- }
- if (current == head) {
- head = head->next;
- }
- else {
- current->prev->next = current->next;
- }
- if (current == last) {
- last = current->prev;
- }
- else {
- current->next->prev = current->prev;
- }
- return current;
- };
- string getNamePL() {
- string namePL;
- cout << "Wprowadz polska nazwe zebranego osobnika: " << endl;
- cin >> namePL;
- return namePL;
- };
- string getNameLA() {
- string nameLA;
- cout << "wprowadz lacinska nazwe zebranego osobinka: " << endl;
- cin >> nameLA;
- return nameLA;
- };
- string getDate() {
- string date;
- cout << "Data zebrania (format rrrr-mm-dd): " << endl;
- cin >> date;
- return date;
- };
- string getComment() {
- string comment;
- cout << "Komentarz: " << endl;
- cin >> comment;
- return comment;
- };
- string getFamily() {
- string family;
- cout << "Rodzina (lac.) do ktorej nalezy osobnik: " << endl;
- cin >> family;
- return family;
- };
- string getCollector() {
- string collector;
- cout << "Imie i nazwisko osoby ktora zebrala osobnika: " << endl;
- cin >> collector;
- return collector;
- };
- string getPlace() {
- string place;
- cout << "Miejsce zebrania: " << endl;
- cin >> place;
- return place;
- };
- string getStatus() {
- string status;
- cout << "Wprowadz status gatunku osobnika (zagrozony, rzadki, powszechny, brak danych); " << endl;
- cin >> status;
- return status;
- };
- /*void printData(){
- struct node *ptr = head;
- while (ptr != NULL) {
- cout << "\n[ ";
- cout << "id: ";
- cout << ptr->data.id << endl;
- cout << "Nazwa polska: ";
- cout << ptr->data.namePL << endl;
- cout << "Nazwa lacinska: ";
- cout << ptr->data.nameLA << endl;
- cout << "Data zebrania: ";
- cout << ptr->data.date << endl;
- cout << "Osoba ktora zebrala osobnika: ";
- cout << ptr->data.collector << endl;
- cout << "Rodzina: ";
- cout << ptr->data.family << endl;
- cout << "Miejsce zebrania: ";
- cout << ptr->data.place << endl;
- cout << "Status wystepowania: ";
- cout << ptr->data.status << endl;
- cout << "Komentarz: ";
- cout << ptr->data.comment << endl;
- cout << " ]" << endl;
- }
- */
- void displayListForward(){
- struct node *ptr = head;
- while (ptr != NULL) {
- cout << "\n[ ";
- cout << "id: ";
- cout << ptr->data.id << endl;
- cout << "Nazwa polska: ";
- cout << ptr->data.namePL << endl;
- cout << "Nazwa lacinska: ";
- cout << ptr->data.nameLA << endl;
- cout << "Data zebrania: ";
- cout << ptr->data.date << endl;
- cout << "Osoba ktora zebrala osobnika: ";
- cout << ptr->data.collector << endl;
- cout << "Rodzina: ";
- cout << ptr->data.family << endl;
- cout << "Miejsce zebrania: ";
- cout << ptr->data.place << endl;
- cout << "Status wystepowania: ";
- cout << ptr->data.status << endl;
- cout << "Komentarz: ";
- cout << ptr->data.comment << endl;
- cout << " ]" << endl;
- }
- };
- /*bool searchId( int x)
- {
- struct node* current = head;
- while (current != NULL)
- {
- if ((current->data).id == x)
- printData(current->data);
- return true;
- current = current->next;
- }
- return false;
- }
- */
- /*
- bool searchNamePL(string x)
- {
- struct node* current = head;
- while (current != NULL)
- {
- if ((current->data).namePL == x)
- printData(current->data);
- return true;
- current = current->next;
- }
- return false;
- }
- bool searchNameLA(string x)
- {
- struct node* current = head;
- while (current != NULL)
- {
- if ((current->data).nameLA == x) {
- printData(current->data);
- return true;
- }
- current = current->next;
- }
- return false;
- }
- bool searchDate(string x)
- {
- struct node* current = head; // Initialize current
- while (current != NULL)
- {
- if ((current->data).date == x)
- printData(current->data);
- return true;
- current = current->next;
- }
- return false;
- }
- bool searchFamily(string x)
- {
- struct node* current = head; // Initialize current
- while (current != NULL)
- {
- if ((current->data).family == x)
- printData(current->data);
- return true;
- current = current->next;
- }
- return false;
- }
- bool searchCollector(string x)
- {
- struct node* current = head; // Initialize current
- while (current != NULL)
- {
- if ((current->data).collector == x)
- printData(current->data);
- return true;
- current = current->next;
- }
- return false;
- }
- bool searchPlace(string x)
- {
- struct node* current = head; // Initialize current
- while (current != NULL)
- {
- if ((current->data).place == x)
- printData(current->data);
- return true;
- current = current->next;
- }
- return false;
- }
- bool searchStatus(string x)
- {
- struct node* current = head; // Initialize current
- while (current != NULL)
- {
- if ((current->data).status == x)
- printData(current->data);
- return true;
- current = current->next;
- }
- return false;
- }
- */
- /*
- void szukajPo() {
- int choice;
- do
- {
- cout << endl
- << " Szukaj rosliny po: \n"
- << " 1 - id\n"
- << " 2 - Nazwie polskiej\n"
- << " 3 - Nazwie lacinskiej.\n"
- << " 4 - Dacie.\n"
- << " 5 - Rodzinie\n"
- << " 6 - Osobie która zebrała osobnika.\n"
- << " 7 - Miejscu zebrania.\n"
- << " 8 - Statusie zagrożenia.\n"
- << " Enter your choice and press return: ";
- cin >> choice;
- int temp;
- string temps;
- switch (choice)
- {
- case 1:
- cout << "Wprowadz id: ";
- cin >> temp;
- searchId(temp);
- break;
- case 2:
- cout << "Wprowadz nazwę polską: ";
- cin >> temps;
- searchNamePL(temps);
- break;
- case 3:
- cout << "Wprowadz nazwę lacinska: ";
- cin >> temps;
- searchNameLA(temps);
- break;
- case 4:
- cout << "Wprowadz date: ";
- cin >> temps;
- searchDate(temps);
- break;
- case 5:
- cout << "Wprowadz family: ";
- cin >> temps;
- searchFamily(temps);
- case 6:
- cout << "Wprowadz osobę ktora zebrala: ";
- cin >> temps;
- searchCollector(temps);
- break;
- case 7:
- cout << "Wprowadz miejsce zebrania: ";
- cin >> temps;
- searchPlace(temps);
- break;
- case 8:
- cout << "Wprowadz status (zagrozony, rzadki, powszechny, brak danych): ";
- cin >> temps;
- searchStatus(temps);
- break;
- default:
- cout << "Wybierz ponownie. \n";
- break;
- }
- } while (choice != 9);
- }
- */
- struct data getDataFromUser() {
- struct data temp;
- temp.namePL = getNamePL();
- temp.nameLA = getNameLA();
- temp.family = getFamily();
- temp.status = getStatus();
- temp.date = getDate();
- temp.place = getPlace();
- temp.collector = getCollector();
- temp.comment = getComment();
- return temp;
- };
- void importList();
- void exportList();
- int main() {
- int choice;
- do
- {
- cout << endl
- << " 1 - Wprowadz nowa roslinie\n"
- << " 2 - Usun rosline z albumu\n"
- << " 3 - Edytuj dane o roslinie\n"
- << " 4 - Wyszukaj element po id, "
- << " 5 - Wyswietl cala baze\n"
- << " 6 - Wyswietl rosliny o zadanym statusie\n"
- << " 7 - Zapisz do pliku\n"
- << " 8 - Wczytaj z pliku\n"
- << " 9 - Zakoncz program\n"
- << " Enter your choice and press return: ";
- cin >> choice;
- switch (choice)
- {
- case 1:
- append(getDataFromUser());
- break;
- case 2:
- int toDelete;
- cout << "podaj pozycje która chcesz usunać: " << endl;
- cin >> toDelete;
- usun(toDelete);
- break;
- case 3:
- break;
- case 4:
- break;
- case 5:
- displayListForward();
- break;
- case 6:
- break;
- case 7:
- exportList();
- break;
- case 8:
- break;
- case 9:
- break;
- default:
- cout << "Niepoprawny wybor. \n"
- << "Wybierz ponownie.\n";
- break;
- }
- } while (choice != 9);
- return 0;
- }
- void exportList() {
- struct node *ptr = head;
- ofstream dataFile;
- dataFile.open("zielnik.txt", ofstream::out | ofstream::trunc);
- if (!dataFile.is_open() || dataFile.fail())
- {
- dataFile.close();
- printf("\nBląd z plikiem zielnik.txt");
- };
- while (ptr != NULL) {
- dataFile << (ptr->data).id << ","
- << (ptr->data).namePL << ","
- << (ptr->data).nameLA << ","
- << (ptr->data).date << ","
- << (ptr->data).collector << ","
- << (ptr->data).comment << ","
- << (ptr->data).family << ","
- << (ptr->data).place << ","
- << (ptr->data).status << endl;
- ptr = ptr->next;
- }
- dataFile.close();
- };
- void importList() {
- node *head = NULL;
- node *last = NULL;
- ifstream dataFile;
- dataFile.open("zielnik.txt", ifstream::in);
- if (!dataFile.is_open() || dataFile.fail())
- {
- dataFile.close();
- printf("\nBląd z plikiem zielnik.txt");
- };
- vector<string> matrix;
- string line;
- while (getline(dataFile, line, '\n'))
- {
- istringstream iss(line);
- string data;
- while (getline(iss, data, ','))
- {
- matrix.push_back(data);
- }
- }
- dataFile.close();
- //tworzenie tymczas struktury i id
- struct data tmp_karta;
- int tmp_id;
- tmp_karta;
- int i = 0;
- while (i < matrix.size()) {
- tmp_karta.id = sstream::stoi(matrix[i]);
- tmp_karta.namePL = matrix[i + 1];
- tmp_karta.nameLA = matrix[i + 2];
- tmp_karta.date = matrix[i + 3];
- tmp_karta.collector = matrix[i + 4];
- tmp_karta.comment = matrix[i + 5];
- tmp_karta.family = matrix[i + 6];
- tmp_karta.place = matrix[i + 7];
- tmp_karta.status = matrix[i + 8];
- i += 9; // rekord ma 9pol
- append(tmp_karta);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement