Advertisement
kubpica

Asercje c++

May 6th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <fstream>
  7. #include <cassert>
  8.  
  9. using namespace std;
  10.  
  11. struct el {
  12.     int v;
  13.     el* next = NULL;
  14.     el* prev = NULL;
  15.     el(int v);
  16.     virtual ~el();
  17. };
  18.  
  19. el::el(int _v) {
  20.     this->v = _v;
  21.     //cout << "el(" << _v << ")" << endl;
  22. }
  23.  
  24. el::~el() {
  25.     el* a = this->prev;
  26.     el* b = this->next;
  27.     if (a != NULL)
  28.         a->next = b;
  29.     if (b != NULL)
  30.         b->prev = a;
  31. }
  32.  
  33. class List {
  34.     el* head = NULL;
  35.     el* tail = NULL;
  36.  
  37. public:
  38.     virtual ~List();
  39.     el* add(int a);
  40.     void showRightToLeft();
  41.     void showLeftToRight();
  42. };
  43.  
  44.  
  45.  
  46. List::~List() {
  47.     el* e = this->head;
  48.     while (e != NULL) {
  49.         el* temp = e;
  50.         e = e->next;
  51.         delete temp;
  52.     }
  53. }
  54.  
  55. el* List::add(int _a) {
  56.     el* current = new el(_a);
  57.     assert(current != NULL);
  58.  
  59.     if (this->head == NULL) {
  60.         //dodanie pierwszego elementu
  61.         this->head = current;
  62.         this->tail = current;
  63.         return current;
  64.     }
  65.  
  66.     if (current->v < (this->head)->v) {
  67.         //dodanie z przodu
  68.         el* e = this->head;
  69.         current->next = e;
  70.         e->prev = current;
  71.         this->head = current;
  72.         return current;
  73.     }
  74.  
  75.     el* next = this->head->next;
  76.     while (next != NULL && current->v > next->v) {
  77.         next = next->next;
  78.     }
  79.     if (next == NULL) {
  80.         //dodanie na końcu
  81.         el* e = this->tail;
  82.         e->next = current;
  83.         current->prev = e;
  84.         this->tail = current;
  85.         return current;
  86.     }
  87.  
  88.     //dodanie w środku
  89.     el* a = next->prev;
  90.     el* b = next;
  91.     a->next = current;
  92.     b->prev = current;
  93.     current->prev = a;
  94.     current->next = b;
  95.     return current;
  96. }
  97.  
  98. void List::showLeftToRight() {
  99.     el* e = this->head;
  100.     while (e != NULL) {
  101.         cout << e->v << " ";
  102.         e = e->next;
  103.     }
  104.     cout << endl;
  105. }
  106.  
  107. void List::showRightToLeft() {
  108.     el* e = this->tail;
  109.     while (e != NULL) {
  110.         cout << e->v << " ";
  111.         e = e->prev;
  112.     }
  113.     cout << endl;
  114. }
  115.  
  116. void checkRange(int var, int min, int max) {
  117.     assert(var>=min && var<=max);
  118.     cout << var << " znajduje sie w ustalonym zakresie " << min << ":" << max << "." << endl;
  119. }
  120.  
  121. bool try_stoi(int & i, const string & s) {
  122.     try {
  123.         size_t pos;
  124.         i = stoi(s, &pos);
  125.         return pos == s.size();
  126.     }
  127.     catch (const std::invalid_argument&) {
  128.         return false;
  129.     }
  130. }
  131.  
  132. int main(int argc, char* argv[])
  133. {
  134.     assert(argc==4);
  135.     string fileName = argv[1];
  136.     int min = atoi(argv[2]);
  137.     int max = atoi(argv[3]);
  138.     cout << "Plik: " << fileName << " Zakres: " << min << ":" << max << endl;
  139.  
  140.     ifstream file;
  141.     file.open(fileName, std::fstream::in);
  142.     assert(file.good());
  143.     cout << "Plik zostal poprawnie otworzony." << endl;
  144.          
  145.     List* list = new List;
  146.     assert(list != NULL);
  147.     string wiersz;
  148.     while (getline(file, wiersz)) {
  149.         int i;
  150.         bool ok = try_stoi(i, wiersz);
  151.         assert(ok);
  152.         //dodawanie do listy tylko liczb z podanego zakresu
  153.         checkRange(i, min, max);
  154.         list->add(i);
  155.         wiersz.clear();
  156.     }
  157.     file.close();
  158.  
  159.     cout << "Lista od lewej do prawej: " << endl;
  160.     list->showLeftToRight();
  161.     cout << "Lista od prawej do lewej: " << endl;
  162.     list->showRightToLeft();
  163.  
  164.     delete list;
  165.     cin >> min;
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement