Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <windows.h>
- #include <string>
- #include <cstdlib>
- #include <fstream>
- #include <cassert>
- using namespace std;
- struct el {
- int v;
- el* next = NULL;
- el* prev = NULL;
- el(int v);
- virtual ~el();
- };
- el::el(int _v) {
- this->v = _v;
- //cout << "el(" << _v << ")" << endl;
- }
- el::~el() {
- el* a = this->prev;
- el* b = this->next;
- if (a != NULL)
- a->next = b;
- if (b != NULL)
- b->prev = a;
- }
- class List {
- el* head = NULL;
- el* tail = NULL;
- public:
- virtual ~List();
- el* add(int a);
- void showRightToLeft();
- void showLeftToRight();
- };
- List::~List() {
- el* e = this->head;
- while (e != NULL) {
- el* temp = e;
- e = e->next;
- delete temp;
- }
- }
- el* List::add(int _a) {
- el* current = new el(_a);
- assert(current != NULL);
- if (this->head == NULL) {
- //dodanie pierwszego elementu
- this->head = current;
- this->tail = current;
- return current;
- }
- if (current->v < (this->head)->v) {
- //dodanie z przodu
- el* e = this->head;
- current->next = e;
- e->prev = current;
- this->head = current;
- return current;
- }
- el* next = this->head->next;
- while (next != NULL && current->v > next->v) {
- next = next->next;
- }
- if (next == NULL) {
- //dodanie na końcu
- el* e = this->tail;
- e->next = current;
- current->prev = e;
- this->tail = current;
- return current;
- }
- //dodanie w środku
- el* a = next->prev;
- el* b = next;
- a->next = current;
- b->prev = current;
- current->prev = a;
- current->next = b;
- return current;
- }
- void List::showLeftToRight() {
- el* e = this->head;
- while (e != NULL) {
- cout << e->v << " ";
- e = e->next;
- }
- cout << endl;
- }
- void List::showRightToLeft() {
- el* e = this->tail;
- while (e != NULL) {
- cout << e->v << " ";
- e = e->prev;
- }
- cout << endl;
- }
- void checkRange(int var, int min, int max) {
- assert(var>=min && var<=max);
- cout << var << " znajduje sie w ustalonym zakresie " << min << ":" << max << "." << endl;
- }
- bool try_stoi(int & i, const string & s) {
- try {
- size_t pos;
- i = stoi(s, &pos);
- return pos == s.size();
- }
- catch (const std::invalid_argument&) {
- return false;
- }
- }
- int main(int argc, char* argv[])
- {
- assert(argc==4);
- string fileName = argv[1];
- int min = atoi(argv[2]);
- int max = atoi(argv[3]);
- cout << "Plik: " << fileName << " Zakres: " << min << ":" << max << endl;
- ifstream file;
- file.open(fileName, std::fstream::in);
- assert(file.good());
- cout << "Plik zostal poprawnie otworzony." << endl;
- List* list = new List;
- assert(list != NULL);
- string wiersz;
- while (getline(file, wiersz)) {
- int i;
- bool ok = try_stoi(i, wiersz);
- assert(ok);
- //dodawanie do listy tylko liczb z podanego zakresu
- checkRange(i, min, max);
- list->add(i);
- wiersz.clear();
- }
- file.close();
- cout << "Lista od lewej do prawej: " << endl;
- list->showLeftToRight();
- cout << "Lista od prawej do lewej: " << endl;
- list->showRightToLeft();
- delete list;
- cin >> min;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement