Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.60 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. namespace str {
  7.  
  8.  
  9.     class my_str {
  10.  
  11.     public:
  12.         //O(n) =
  13.         my_str(const char *s = " "){
  14.             head = createNode(*s++);
  15.             list_node *temp = head;
  16.             while(*s != '\0'){
  17.                 temp->next = createNode(*s++);
  18.                 temp = temp->next;
  19.             }
  20.         }
  21.  
  22.         //O(n) =
  23.         my_str(const my_str &s) {
  24.             list_node *tempL1;
  25.             list_node *tempL2 = s.head;
  26.  
  27.             head = new list_node;
  28.             tempL1 = head;
  29.  
  30.             while(tempL2 != nullptr)
  31.             {
  32.                 tempL1->info = tempL2->info;
  33.                 tempL1->next = new list_node;
  34.                 tempL1 = tempL1->next;
  35.                 tempL2 = tempL2->next;
  36.             }
  37.             delete tempL1;
  38.         }
  39.  
  40.         //O(n) =
  41.         my_str &operator=(const my_str &s) {
  42.             destroyList();
  43.             list_node *tempL1;
  44.             list_node *tempL2 = s.head;
  45.             head = new list_node;
  46.             tempL1 = head;
  47.  
  48.             while(tempL2 != nullptr)
  49.             {
  50.                 tempL1->info = tempL2->info;
  51.                 tempL1->next = new list_node;
  52.                 tempL1 = tempL1->next;
  53.                 tempL2 = tempL2->next;
  54.             }
  55.             return *this;
  56.         }
  57.  
  58.         //O(n) =
  59.         char &operator[](const int index) {
  60.             list_node *temp = head;
  61.             int searchKey = 0;
  62.             while(searchKey !=index && temp != nullptr){
  63.                 searchKey++;
  64.                 temp = temp->next;
  65.             }
  66.             return temp->info;
  67.         }
  68.  
  69.  
  70.  
  71.         //
  72.         int length()const {
  73.             return strlen(head);
  74.         }
  75.         //O(n) =
  76.         int indexOf(char c)const {
  77.             list_node *temp = head;
  78.             int index = 0;
  79.             while (temp->info != c && temp != nullptr){
  80.                 index++;
  81.                 temp = temp->next;
  82.             }
  83.             return index;
  84.         }
  85.  
  86.         //O(n) =
  87.         int indexOf(const my_str &pat)const {
  88.             list_node *temp = head;
  89.  
  90.         }
  91.  
  92.         //O(n) =
  93.         bool operator ==(const my_str &s)const {
  94.             list_node *string1 = head;
  95.             list_node *string2 = s.head;
  96.             if(length() == s.length()){
  97.                 while(string1 != nullptr && string2 != nullptr){
  98.                     if(string1->info == string2->info )
  99.                     {
  100.                         return true;
  101.                     }
  102.                     string1 = string1->next;
  103.                     string2 = string2->next;
  104.                 }
  105.             }
  106.             return false;
  107.         }
  108.  
  109.         //O(n) =
  110.         my_str operator+(const my_str &s)const {
  111.             my_str obj(*this);
  112.  
  113.              list_node *currChar = s.head;
  114.              list_node *newChar;
  115.              list_node *objTail = obj.getTail();
  116.  
  117.              while(currChar != nullptr){
  118.                  newChar = new list_node;
  119.                  newChar ->info = currChar->info;
  120.                  newChar ->next = nullptr;
  121.                  objTail ->next = newChar;
  122.                  objTail = objTail ->next;
  123.                  currChar = currChar->next;
  124.              }
  125.             return obj;
  126.         }
  127.  
  128.         //O(n) =
  129.         my_str &operator+=(const my_str &s) {
  130.  
  131.         }
  132.  
  133.         //O(n) =
  134.         my_str reverse()const{
  135.         my_str obj;
  136.         list_node *tail;
  137.         obj.head = new list_node;
  138.         obj.head ->next = nullptr;
  139.         obj.head->info = (*this)[length()-1];
  140.         tail = obj.head;
  141.         for(int i = length()-2; i >= 0; i--){
  142.             char c = (*this)[i];
  143.             list_node *temp = head;
  144.             tail = new list_node;
  145.             tail->next = nullptr;
  146.  
  147.             tail ->info = c;
  148.             tail ->next = temp;
  149.             tail = tail->next;
  150.  
  151.         }
  152.         return obj;
  153.         }
  154.  
  155.         //
  156.         void print(std::ostream &out)const {
  157.             list_node *temp = head;
  158.             while(temp != nullptr)
  159.             {
  160.                 out << temp->info;
  161.                 temp = temp->next;
  162.             }
  163.         }
  164.  
  165.         //O(n) =
  166.         void read(std::istream &in) {
  167.             destroyList();
  168.             std::cout << "im here";
  169.             list_node *temp = head;
  170.             list_node *tail;
  171.             head = new list_node;
  172.             head->next = nullptr;
  173.             std::cout << "im here 2";
  174.             head ->info = in.get();
  175.             std::cout << "im here 3";
  176.             tail = head;
  177.             char c = in.get();
  178.             std::cout << "im here 4";
  179.             while(!in.eof() && c != '\n' && c != '\0'){
  180.                 temp = new list_node;
  181.                 temp->next = nullptr;
  182.                 temp->info = c;
  183.                 c = in.get();
  184.                 tail->next = temp;
  185.                 tail = tail -> next;
  186.             }
  187.             std::cout << "im here 44";
  188.         }
  189.         //O(n) =
  190.         ~my_str() {
  191.             destroyList();
  192.         }
  193.  
  194.     private:
  195.         struct list_node
  196.         {
  197.             char info = '\0';
  198.             list_node *next = nullptr;
  199.         };
  200.  
  201.         list_node *createNode(char value)
  202.         {
  203.             return new list_node{value};
  204.         }
  205.  
  206.         char &operator[](const int index)const {
  207.             list_node *temp = head;
  208.             int searchKey = 0;
  209.             while(searchKey !=index && temp != nullptr){
  210.                 searchKey++;
  211.                 temp = temp->next;
  212.             }
  213.             return temp->info;
  214.         }
  215.  
  216.         //returns the last index in the list
  217.         list_node *getTail(){
  218.             list_node *temp  = head;
  219.             while(temp->next != nullptr){
  220.                 temp = temp->next;
  221.             }
  222.             return temp;
  223.         }
  224.         void destroyList() {
  225.  
  226.             list_node *temp = head;
  227.             list_node *ptr = temp;
  228.             while(ptr != nullptr)
  229.             {
  230.                 temp = temp->next;
  231.                 delete ptr;
  232.                 ptr = temp;
  233.             }
  234.         }
  235.  
  236.         static int strlen(list_node* ptr) {
  237.  
  238.             list_node *temp = ptr;
  239.             int count = 0;
  240.             while(temp != nullptr){
  241.                 count++;
  242.                 temp = temp->next;
  243.             }
  244.             return count;
  245.         }
  246.  
  247.         list_node *head = nullptr;
  248.  
  249.     };
  250.  
  251.     inline std::ostream &operator <<(std::ostream &out, const my_str &str) {
  252.         str.print(out);
  253.         return out;
  254.     }
  255.  
  256.     inline std::istream &operator >>(std::istream &in, my_str &str) {
  257.         //str.read(in);
  258.         return in;
  259.     }
  260. /**********************************************************/
  261. //Testing Functions
  262.     my_str copyConstructorTest(const my_str &l) {
  263.  
  264.         return l;
  265.     }
  266.  
  267.     void testAssignment() {
  268.         my_str a = "joji";
  269.         my_str b;
  270.         std::cout << "Before Assignment: " << a << std::endl;
  271.         std::cout << "After Assignment: " << (b = a) << std::endl;
  272.     }
  273.  
  274.     void testReverse() {
  275.         std::ifstream ifs;
  276.         ifs.open("input.txt");
  277.         my_str l;
  278.         l.read(ifs);
  279.         std::cout << copyConstructorTest(l) << " " << l.length() << " " << l.reverse() << std::endl;
  280.         ifs.close();
  281.     }
  282.  
  283.     void testPrint() {
  284.         my_str a = "hello";
  285.         a.print(std::cout);
  286.     }
  287.  
  288.     void testIndexOf() {
  289.         my_str a = "jojithomas";
  290.         std::cout << a.indexOf('a') << std::endl;
  291.     }
  292.  
  293.     void testLength() {
  294.         my_str j = "jojithomas";
  295.         std::cout << j.length() << std::endl;
  296.     }
  297.  
  298. //test read
  299.     void testRead() {
  300.         my_str ar;
  301.         ar.read(std::cin);
  302.         std::cout << ar << std::endl;
  303.     }
  304.  
  305.     void testAddEqual() {
  306.         my_str a = "driver";
  307.         my_str b = "racecar";
  308.         std::cout << (b += a);
  309.     }
  310.  
  311.     void testStrAdd() {
  312.         my_str a = "joji";
  313.         my_str b = "thomas";
  314.         (a + b).print(std::cout);
  315.     }
  316.  
  317.     void testEqual() {
  318.         my_str a = "joji";
  319.         my_str b = "joi";
  320.         my_str c = "joji";
  321.         std::cout << "A is = to B: ";
  322.         std::cout << (a == b);
  323.         std::cout << "\nA is = to C: ";
  324.         std::cout << (a == c);
  325.  
  326.     }
  327.  
  328.     void testIndexOfMyStr() {
  329.         my_str a = "joji";
  330.         my_str b = "ojac";
  331.         my_str c = "ji";
  332.  
  333.         std::cout << "a = " << a << std::endl;
  334.         std::cout << "b = " << b << std::endl;
  335.         std::cout << "c = " << c << std::endl;
  336.         std::cout << "a.indexOf(b) = " << a.indexOf(b) << std::endl;
  337.         std::cout << "a.indexOf(c) = " << a.indexOf(c) << std::endl;
  338.     }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement