Advertisement
Guest User

reti

a guest
Nov 10th, 2009
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.36 KB | None | 0 0
  1. #include <fstream>
  2. #include "mystring.h"
  3. #include "myistring.h"
  4. //using namespace std;
  5.  
  6. class course {
  7.  
  8.     public:
  9.         course();
  10.         course(const char*, int = 0, int = 0, const char* = '\0', int = 0, const char* = '\0', const char* = '\0', int = 0, int = 0);
  11.         course(const course& );
  12.         ~course();
  13.  
  14.         bool setName(char *);
  15.         bool setSection(int);
  16.         bool setCall(int);
  17.         bool setTitle(char *);
  18.         bool setTime(int);
  19.         bool setDays(char *);
  20.         bool setInstruct(char *);
  21.         bool setCredits(int);
  22.         bool setFee(int);
  23.  
  24.         const mystring getName()const;
  25.         const myistring getSection()const;
  26.         const myistring getCall()const;
  27.         const mystring getTitle()const;
  28.         const myistring getTime()const;
  29.         const mystring getDays()const;
  30.         const mystring getInstruct()const;
  31.         int getCredits()const;
  32.         int getFee()const;
  33.  
  34.  
  35.         bool setNext(course * );
  36.         course* getNext()const;
  37.  
  38.  
  39.         //void next();
  40.         void print()const;
  41.         bool compare(const course& );
  42.         bool operator== (const course& );
  43.         course operator=(const course &);
  44.  
  45. //      friend std::ostream& operator<< (std::ostream &, const course&);
  46.         friend std::ostream& operator<< (std::ostream &, const course &);
  47.  
  48.     private:
  49.         mystring name;
  50.         myistring sect;
  51.         myistring call;
  52.         mystring title;
  53.         myistring time;
  54.         mystring days;
  55.         mystring instruc;
  56.         int credits;
  57.         int fee;   
  58.         course * next;
  59.  
  60.  
  61. };
  62. #include <iostream>
  63.  
  64. class myistring{
  65.  
  66.     public:
  67.         myistring();
  68.         myistring(int);
  69.         myistring(const myistring&);
  70.         ~myistring();
  71.         bool copy(const int *);
  72.         int len();
  73.  
  74.         bool operator== (const int *)const;
  75.         bool operator== (const myistring& )const;
  76.         bool operator!= (const int *)const;
  77.         bool operator!= (const myistring&)const;
  78.         void operator= (const int *);
  79.         void operator= (int);
  80.         int& operator[] (int);
  81.  
  82.         friend std::ostream& operator<< (std::ostream &, const myistring & );
  83.  
  84.  
  85.     private:
  86.         int * istring;
  87.         void intToArr(int source);
  88. };
  89. #include <iostream>
  90.  
  91. class mystring{
  92.     public:
  93.         mystring();
  94.         mystring(const char *);
  95.         mystring(const mystring& );
  96.         mystring(char);
  97.         ~mystring();
  98.         bool copy(const char *);
  99.         bool copy(const mystring & );
  100.         int len()const;
  101.         bool cat(const char *);
  102.         bool cat(const mystring &);
  103.         const char* get()const;
  104.         bool sort();
  105.         //bool swap(const mystring&);
  106.         //int cmp(const char *, const char *);
  107.         bool operator== (const mystring &)const;
  108.         bool operator!= (const mystring &)const;
  109.         bool operator== (const char *)const;
  110.         bool operator!= (const char * rhs)const;
  111.         void operator= (const mystring &);
  112.         void operator= (const char *);
  113.         void operator= (char);
  114.         char operator[] (int);
  115.         bool operator+= (const mystring&);
  116.         bool operator+= (const char* );
  117.         bool operator+= (char);
  118.    
  119.  
  120.         friend std::ostream& operator<< (std::ostream &, const mystring&);
  121.         friend std::istream& operator>> (std::istream &, const mystring&);
  122.         friend std::ostream& operator<< (std::ostream &, const course&);
  123.  
  124.  
  125.     private:
  126.         char * cstring;
  127.         int strlen(const char *)const;
  128.  
  129.  
  130.  
  131. };
  132. #include <iostream>
  133. #include "course.h"
  134.  
  135. using namespace std;
  136.  
  137. course::course(){
  138.  
  139.     //mystring and myistring have the constructors in their class already
  140.    
  141.     credits = 0;
  142.     fee = 0;
  143.     next = NULL;
  144.  
  145. }
  146.  
  147. course::course(const char* rname, int rsect, int rcall, const char* rtitle, int rtime, const char* rdays, const char* rinstruc, int rcredits, int rfee){
  148.  
  149.     name = rname;
  150.     sect = rsect;
  151.     call = rcall;
  152.     title = rtitle;
  153.     time = rtime;
  154.     days = rdays;
  155.     instruc = rinstruc;
  156.     if(rcredits >= 0 && rcredits <= 5)
  157.         credits = rcredits;
  158.     if(rfee >= 0)
  159.         fee = rfee;
  160.     next = NULL;
  161. }
  162.  
  163. course::course(const course& rhs){
  164.  
  165.     name = rhs.name;  
  166.     sect = rhs.sect;
  167.     call = rhs.call;
  168.     title = rhs.title;
  169.     time = rhs.time;
  170.     days = rhs.days;
  171.     instruc = rhs.instruc;
  172.     credits = rhs.credits;
  173.     fee = rhs.fee;
  174. }
  175.  
  176. course::~course(){
  177.  
  178.     //mystring and myistring already have the destructor included
  179.     fee = 0;
  180.     credits = 0;
  181.     next = NULL;
  182. }
  183.  
  184. bool course::setName(char * n){
  185.  
  186.     name = n;
  187.     return true;
  188. }
  189.  
  190. bool course::setSection(int s){
  191.  
  192.     sect = s;
  193.  
  194.     return true;
  195. }
  196.  
  197. bool course::setCall(int c){
  198.  
  199.  
  200.     call = c;
  201.  
  202.     return true;
  203. }
  204.  
  205. bool course::setTitle(char * t){
  206.  
  207.     title = t;
  208.  
  209.     return true;
  210. }
  211.  
  212. bool course::setTime(int t){
  213.  
  214.     time = t;
  215.  
  216.     return true;
  217. }
  218.  
  219. bool course::setDays(char * d){
  220.  
  221.     days = d;
  222.  
  223.     return true;
  224. }
  225.  
  226. bool course::setInstruct(char * n){
  227.  
  228.     instruc = n;
  229.  
  230.     return true;
  231. }
  232.  
  233. bool course::setCredits(int c){
  234.  
  235.     if (c <= 5 && c >= 0){
  236.         credits = c;
  237.         return true;
  238.     } else{
  239.         credits = 0;
  240.         return false;
  241.     }
  242. }
  243.  
  244. bool course::setFee(int f){
  245.  
  246.     if (f <= 250 && f >= 0){
  247.         fee = f;
  248.         return true;
  249.     } else{
  250.         fee = 0;
  251.         return false;
  252.     }
  253. }
  254.  
  255. const mystring  course::getName()const{
  256.     return name;
  257. }
  258.  
  259. const myistring course::getSection()const{
  260.     return sect;
  261. }
  262.  
  263. const myistring course::getCall()const{
  264.     return call;
  265. }
  266.  
  267. const mystring course::getTitle()const{
  268.     return title;
  269. }
  270.  
  271. const myistring course::getTime()const{
  272.     return time;
  273. }
  274.  
  275. const mystring course::getDays()const{
  276.     return days;
  277. }
  278.  
  279. const mystring course::getInstruct()const{
  280.     return instruc;
  281. }
  282.  
  283. int course::getCredits()const{
  284.     return credits;
  285. }
  286.  
  287. int course::getFee()const{
  288.     return fee;
  289. }
  290.  
  291. void course::print()const{
  292.  
  293.     int i = 0;
  294.     cout << "Name: " << name << endl;
  295.     ////////////
  296.     cout << "Section: ";
  297.     cout << sect;
  298.     cout << endl;
  299.     //////////////
  300.     cout << "Call Number: ";
  301.     cout << call;
  302.     cout << endl;
  303.     //////////////
  304.     cout << "Title: " << title << endl;
  305.     //////////////
  306.     cout << "Time: ";
  307.     cout << time << endl;
  308.     //////////////
  309.     cout << "Days: " << days << endl;
  310.     cout << "Instructor: " << instruc << endl;
  311.     cout << "Credits: " << credits << endl;
  312.     cout << "Fee: $" << fee << endl;
  313.     cout << "#####################################" << endl;
  314. }
  315.  
  316. bool course::compare(const course& rhs){
  317.  
  318.     bool same = true;
  319.  
  320.     if(this == &rhs)
  321.         return true;
  322.  
  323.     if(name != rhs.name)
  324.         same = false;
  325.     if(sect != rhs.sect)
  326.         same = false;
  327.     if(call != rhs.call)
  328.         same = false;
  329.     if(title != rhs.title)
  330.         same = false;
  331.     if(time != rhs.time)
  332.         same = false;
  333.     if(days != rhs.days)
  334.         same = false;
  335.     if(instruc != rhs.instruc)
  336.         same = false;
  337.     if(credits != rhs.credits)
  338.         same = false;
  339.     if(fee != rhs.fee)
  340.         same = false;
  341.  
  342.     return same;
  343. }
  344.  
  345. bool course::operator== (const course& rhs){
  346.     return compare(rhs);
  347. }
  348.  
  349. bool course::setNext(course * tmp){
  350.  
  351.     next = tmp;
  352.     return true;
  353. }
  354.  
  355. course* course::getNext()const{
  356.     return next;
  357. }
  358.  
  359. course course::operator=(const course & rhs){
  360.  
  361.     if(this != &rhs){
  362.         name = rhs.name;
  363.         sect = rhs.sect;
  364.         call = rhs.call;
  365.         title = rhs.title;
  366.         days = rhs.days;
  367.         instruc = rhs.instruc;
  368.         credits = rhs.credits;
  369.         fee = rhs.fee;
  370.         return *this;
  371.     }
  372.     else return *this;
  373. }
  374.  
  375.  
  376. //std::ostream& operator<< (std::ostream &out, const course &rhs){
  377. ostream& operator<< (ostream &out, const course & rhs) const
  378. {
  379.  
  380.     out << "Name: " << rhs.name <<endl;
  381.     /////////////
  382.     out << "Section: ";
  383.     out << rhs.sect;
  384.     out <<endl;
  385.     /////////////
  386.     out << "Call Number: ";
  387.     out << rhs.call;
  388.     out <<endl;
  389.     /////////////
  390.     out << "Title: " << rhs.title <<endl;
  391.     /////////////
  392.     out << "Time: ";
  393.     out << rhs.time <<endl;
  394.     /////////////
  395.     out << "Days: " << rhs.days <<endl;
  396.     out << "Instructor: " << rhs.instruc <<endl;
  397.  
  398.     out << "Credits: " << rhs.credits <<endl;
  399.     out << "Fee: $" << rhs.fee <<endl;
  400.  
  401.     out << "#####################################" <<endl;
  402.     return out;
  403. }
  404. #include "course.h"
  405. #include <iostream>
  406. #include <fstream>
  407. #include <stdlib.h>
  408. using namespace std;
  409.  
  410. const char FILENAME[] = "courses.txt";
  411.  
  412. void readInfo(ifstream & , course* & );
  413.  
  414. void newNode(course * &);
  415.  
  416. void deleteList(course *);
  417.  
  418. void deleteNode(course* & head);
  419.  
  420. int main(){
  421.  
  422.  
  423.     course * head =  NULL;
  424.     course * tmp = NULL;
  425.     ifstream fin;
  426.     bool cont = true;
  427.     char * ctmp = new char[100];
  428.     int itmp;
  429.     char selection;
  430.     course * test;
  431.  
  432.     test = new course;
  433.     course test1;
  434.  
  435.     cout << test1;
  436.  
  437. //  cout << test1 << endl;
  438.  
  439.  
  440.  
  441.     fin.open(FILENAME);
  442. /*
  443.     //create a new head
  444.     head = new course;
  445.     tmp = head;
  446.  
  447.     readInfo(fin, head);
  448.  
  449.  
  450.     //read in the data while creating new nodes as needed
  451.     while(fin.good()){
  452.         fin.peek();
  453.         if(fin.good()){
  454.             newNode(tmp);
  455.             tmp = tmp->getNext();
  456.             readInfo(fin, tmp);
  457.             //tmp->print();
  458.  
  459.         }
  460.     }
  461.  
  462.  
  463.     system("clear");
  464.     //menu
  465.     do{
  466.         tmp = head;
  467.         cout << "1: Add a course" << endl;
  468.         cout << "2: Delete a course" << endl;      
  469.         cout << "3: Print the courses" << endl;
  470.         cout << "4: Quit" << endl;
  471.         cin >> selection;
  472.         switch(selection){
  473.  
  474.             case '1' :
  475.             case 'a' :
  476.                 //get info for course
  477.                 tmp = head;
  478.                 while(tmp->getNext() != NULL){
  479.                     tmp = tmp->getNext();
  480.                 }
  481.                 newNode(tmp);
  482.  
  483.                 cout << endl << "Whats the new name?" ;
  484.                 cin.get();
  485.                 cin.getline(ctmp, 100);
  486.                 tmp->setName(ctmp);
  487.  
  488.                 cout << endl << "Whats the new section number?" ;
  489.                 cin >> itmp;
  490.                 tmp->setSection(itmp);
  491.  
  492.                 cout << endl << "Whats the new call number?" ;
  493.                 cin >> itmp;
  494.                 tmp->setCall(itmp);
  495.  
  496.                 cout << endl << "Whats the new course title?";
  497.                 cin.get();
  498.                 cin.getline(ctmp, 100);
  499.                 tmp->setTitle(ctmp);
  500.  
  501.                 cout << endl << "Whats the new time?";
  502.                 cin >> itmp;
  503.                 tmp->setTime(itmp);
  504.  
  505.                 cout << endl << "What are the new days?";
  506.                 cin.get();
  507.                 cin.getline(ctmp, 100);
  508.                 tmp->setDays(ctmp);
  509.  
  510.                 cout << endl << "Whats the new instructor?";
  511.                 cin.get();
  512.                 cin.getline(ctmp, 100);
  513.                 tmp->setInstruct(ctmp);
  514.  
  515.                 cout << endl << "How many credits is it?";
  516.                 cin >> itmp;
  517.                 tmp->setCredits(itmp);
  518.  
  519.                 cout << endl <<"How much is the fee?";
  520.                 cin >> itmp;
  521.                 tmp->setFee(itmp);
  522.  
  523.                 break;
  524.             case '2' :
  525.             case 'd' :
  526.                 //delete course
  527.                 deleteNode(head);
  528.                 break;
  529.             case '3' :
  530.             case 'p' : 
  531.                 tmp = head;
  532.                 //print 'em out
  533.                 while(tmp->getNext() != NULL){
  534.                     //tmp->print();
  535.                     cout << *tmp << endl;;
  536.                     tmp = tmp->getNext();
  537.                 }
  538.                 break;
  539.             case '4' :
  540.             case 'q' : 
  541.                 cont = false;
  542.                 break;
  543.  
  544.  
  545.  
  546.  
  547.         }
  548.     }while(cont == true);
  549.  
  550.     tmp = head;
  551.  
  552.     //delete the list
  553.     deleteList(head);
  554.     delete [] ctmp;
  555. */
  556.     return 0;
  557.  
  558. }
  559.  
  560. void readInfo(ifstream & fin, course* & node){
  561.  
  562.     int itmp = 0;
  563.     char * ctmp = new char [100];
  564.     fin.peek();
  565.  
  566.  
  567.     if(fin.good()){
  568.         if(fin.peek() == '\n')
  569.             fin.get();
  570.         fin.getline(ctmp, 99, '&');
  571.         node->setName(ctmp);
  572.  
  573.         fin >> itmp;
  574.         node->setSection(itmp);
  575.  
  576.         fin >> itmp;
  577.         node->setCall(itmp);
  578.  
  579.         fin.getline(ctmp, 99, '&');
  580.         node->setTitle(ctmp);
  581.  
  582.         fin >> itmp;
  583.         node->setTime(itmp);
  584.  
  585.         fin.getline(ctmp, 99, '&');
  586.         node->setDays(ctmp);
  587.  
  588.         fin.getline(ctmp, 99, '&');
  589.         node->setInstruct(ctmp);
  590.  
  591.         fin >> itmp;
  592.         node->setCredits(itmp);
  593.  
  594.         fin >> itmp;
  595.         node->setFee(itmp);
  596.  
  597.     }
  598. }
  599.  
  600. void newNode(course * &oldNode){
  601.  
  602.     course * tmp;
  603.  
  604.     tmp = new course;
  605.  
  606.     oldNode->setNext(tmp);
  607.  
  608.     tmp->setNext(NULL);
  609. }
  610.  
  611. void deleteList(course * head){
  612.  
  613.     course * tmp = head;
  614.  
  615.     while(head->getNext() != NULL){
  616.         tmp = tmp->getNext();
  617.         delete head;
  618.         head = tmp;
  619.     }
  620.     delete head;
  621. }
  622.  
  623. void deleteNode(course* & head){
  624.  
  625.     course * tmp = head;
  626.     course * tmp1 = NULL;
  627.     int i = 1, num = 0;
  628.  
  629.     cout << "Which node would you like to delete? (0 for none)" << endl;
  630.  
  631.     while(tmp->getNext() != NULL){
  632.         cout << i << ". " << tmp->getName() << endl;
  633.         tmp = tmp->getNext();
  634.         i++;
  635.     }
  636.     cin >> num;
  637.  
  638.     //exception for first node
  639.     tmp = head;
  640.     if(num == 1){
  641.         head = head->getNext();
  642.         delete tmp;
  643.     }
  644.  
  645.     //goes down the list until it reaches the before node, links the prev to the next, and deletes the correct one
  646.     else{
  647.         if(num > 0){
  648.             for(i = 0; i < num - 2; i++){
  649.                 tmp = tmp->getNext();
  650.             }
  651.             tmp1 = tmp->getNext();
  652.             tmp->setNext(tmp1->getNext());
  653.             delete tmp1;
  654.         }
  655.  
  656.     }
  657. }
  658. #include "myistring.h"
  659. #include <cstdio>
  660.  
  661. myistring::myistring(){
  662.     istring = NULL;
  663. }
  664.  
  665. myistring::myistring(int a){
  666.     istring = NULL;
  667.     intToArr(a);
  668. }
  669.  
  670. //copy constructor
  671. myistring::myistring(const myistring& rhs){
  672.    
  673.     if(istring != NULL)
  674.         istring = new int[rhs.istring[0] + 1];
  675.     copy(rhs.istring);
  676.  
  677. }
  678.  
  679.     myistring::~myistring(){
  680.         if(istring != NULL)
  681.             delete [] istring;
  682.         istring = NULL;
  683.     }
  684.  
  685. bool myistring::copy(const int * source){
  686.  
  687.     int i = 0;
  688.     if(istring != NULL){
  689.         delete [] istring;
  690.         istring = NULL;
  691.     }
  692.     istring = new int[source[0] + 1];
  693.  
  694.  
  695.     for(i = 0; i <= source[0]; i++){
  696.         istring[i] = source[i];
  697.     }
  698.  
  699.     return true;
  700. }
  701.  
  702.     int myistring::len(){
  703.         if(istring != NULL)
  704.             return istring[0];
  705.         else return 0;
  706.     }
  707.  
  708. bool myistring::operator== (const int * rhs)const{
  709.  
  710.     if(istring == NULL)
  711.         return false;
  712.  
  713.     if(rhs[0] != istring[0])
  714.         return false;
  715.  
  716.     for(int i = 1; i <= rhs[0]; i++)
  717.         if(rhs[i] != istring[i])
  718.             return false;
  719.     return true;
  720. }
  721.  
  722. bool myistring::operator== (const myistring& rhs)const{
  723.     return operator== (rhs.istring);
  724. }
  725.  
  726. bool myistring::operator!= (const int * rhs)const{
  727.  
  728.     if(istring == NULL)
  729.         return true;
  730.     if(rhs[0] != istring[0])
  731.         return true;
  732.  
  733.     for(int i = 1; i <= rhs[0]; i++)
  734.         if(rhs[i] != istring[i])
  735.             return true;
  736.     return false;
  737.  
  738.  
  739. }
  740.  
  741. bool myistring::operator!= (const myistring& rhs) const{
  742.     return operator!= (rhs.istring);
  743. }
  744.  
  745. void myistring::operator= (const int * rhs){
  746.  
  747.     copy(rhs);
  748.  
  749. }
  750.  
  751. void myistring::operator= (int a){
  752.  
  753.     intToArr(a);
  754. }
  755.  
  756. int& myistring::operator[] (int a){
  757.  
  758.  
  759.     if(a >= 0 && a >= istring[0])
  760.         return istring[a];
  761. }
  762.  
  763. void myistring::intToArr(int source){
  764.  
  765.     int i = 0, length = 0, stmp = source;
  766.     char * tmp = NULL   ;
  767.  
  768.     //gets the length of the source
  769.     while(stmp > 0){
  770.         stmp /= 10;
  771.         length++;
  772.     }
  773.  
  774.     tmp = new char[length +1];
  775.  
  776.     //converts the source into a char array
  777.     sprintf(tmp, "%d", source);
  778.  
  779.     if(istring != NULL){
  780.         delete [] istring;
  781.     }
  782.     istring = new int[length + 1];
  783.  
  784.     //converts the char array to the int array
  785.     for(i = 0; i < length; i++){
  786.         istring[ i+ 1] = tmp[i] - '0';
  787.     }
  788.  
  789.     istring[0] = length;
  790.     delete [] tmp;
  791. }
  792.  
  793. std::ostream& operator<< (std::ostream &out, const myistring & str){
  794.  
  795.     if(str.istring != NULL)
  796.         for(int i = 1; i <= str.istring[0]; i++){
  797.             out << str.istring[i];
  798.         }
  799.     return out;
  800. }
  801.  
  802. std::istream& operator>> (std::istream &in, const myistring & str){
  803.     /*
  804.        int num;
  805.        in >> num;
  806.        myistring::str.intToArr(num);
  807.        return num;
  808.        */
  809. }
  810. #include "mystring.h"
  811.  
  812. //int const NULL = 0;
  813.  
  814. mystring::mystring(){
  815.     cstring = new char[1];
  816.     cstring[0] = '\0';
  817. }
  818.  
  819. //this one lets you construc a string when you declare it
  820. mystring::mystring(const char * word){
  821.    
  822.     cstring = new char[strlen(word) + 1];
  823.     copy(word);
  824. }
  825.  
  826. //copy constructor
  827. mystring::mystring(const mystring& word){
  828.    
  829.     cstring = new char[word.len() + 1];
  830.     copy(word.cstring);
  831. }
  832.  
  833. mystring::mystring(char c){
  834.    
  835.     cstring = new char[2];
  836.     cstring[0] = c;
  837.     cstring[1] = '\0';
  838. }
  839.  
  840. mystring::~mystring(){
  841.     if(cstring != NULL){
  842.         delete [] cstring;
  843.         cstring = NULL;
  844.     }
  845. }
  846.  
  847. //copies a c-string into the class' string
  848. bool mystring::copy(const char * source){
  849.     int i = 0;
  850.  
  851.     if(cstring != NULL){
  852.         while(source[i] != '\0'){
  853.             cstring[i] = source[i];
  854.             i++;
  855.         }
  856.         cstring[i] = '\0';
  857.         return true;
  858.     }
  859. }
  860.  
  861. bool mystring::copy(const mystring & source){
  862.  
  863.     return copy(source.cstring);
  864. }
  865.  
  866. //returns the length of the class c-string, excludes the NULL
  867. int mystring::len()const{
  868.     int i = 0;
  869.  
  870.     if(cstring != NULL){
  871.         while(cstring[i] != '\0')
  872.             i++;
  873.         return i;
  874.     }
  875.     else return 0;
  876. }
  877.  
  878. //takes the cstring and adds the source to the end of it
  879. bool mystring::cat(const char * source){
  880.  
  881.     int i = 0, j = -1;
  882.  
  883.  
  884.     if(cstring != NULL){
  885.         char * tmp = new char[len() + strlen(source) + 1];
  886.  
  887.         while(cstring[i] != '\0'){
  888.             tmp[i] = cstring[i];
  889.             i++;
  890.         }
  891.  
  892.         do{
  893.             j++;
  894.             tmp[i+j] = source[j];
  895.         } while(source[j] != '\0');
  896.         delete [] cstring;
  897.         cstring = tmp;
  898.  
  899.         return true;
  900.     }
  901. }
  902.  
  903. bool mystring::cat(const mystring & source){
  904.  
  905.     return cat(source.cstring);
  906.  
  907. }
  908.  
  909. //returns the cstring pointer
  910. const char* mystring::get()const{
  911.     return cstring;
  912. }
  913.  
  914. bool mystring::sort(){
  915.  
  916.     int length = len();
  917.     bool swapped = false;
  918.  
  919.     do{
  920.         swapped = false;
  921.         for(int i = 0; i < length - 1; i++){
  922.             if(cstring[i] > cstring[i+1]){
  923.                 //swap
  924.                 cstring[i] = cstring[i] ^ cstring[i+1];
  925.                 cstring[i+1] = cstring[i] ^ cstring[i+1];
  926.                 cstring[i] = cstring[i] ^ cstring[i+1];
  927.  
  928.                 swapped = true;
  929.             }
  930.         }
  931.         length -= 1;
  932.     }while(swapped);
  933.     return true;
  934.  
  935. }
  936.  
  937. //checks if the two cstrings are equal
  938. bool mystring::operator== (const mystring & rhs)const{
  939.     return operator== (rhs.cstring);
  940. }
  941.  
  942. //checks if the two cstrings are not equal
  943. bool mystring::operator!= (const mystring & rhs)const{
  944.     return operator!= (rhs.cstring);
  945. }
  946.  
  947. bool mystring::operator== (const char * rhs)const{
  948.  
  949.     int len1 = 0;
  950.     if(cstring != NULL){
  951.         len1 = len();
  952.  
  953.     }
  954.     else return false;
  955.  
  956.     int len2 = strlen(rhs);
  957.  
  958.     if (len1 != len2)
  959.         return false;
  960.     else if(len1 == len2)
  961.         for(int i = 0; i < len1; i++){
  962.             if(cstring[i] != rhs[i])
  963.                 return false;
  964.         }
  965.     else return true;
  966. }
  967. bool mystring::operator!= (const char * rhs)const{
  968.  
  969.     int len1 = 0;
  970.  
  971.     len1 =len();
  972.  
  973.     int len2 = strlen(rhs);
  974.  
  975.     if(len1 != len2)
  976.         return true;
  977.  
  978.     for(int i = 0; i < len1; i++)
  979.         if(cstring[i] != rhs[i])
  980.             return true;
  981.  
  982.     return false;
  983.  
  984. }
  985. //copies the righ hand side into the class string replacing what's inside
  986. void  mystring::operator= (const mystring & rhs){
  987.     if(cstring != NULL)
  988.         delete [] cstring;
  989.     if(this != &rhs){
  990.         cstring = new char[rhs.len() + 1];
  991.         copy(rhs.cstring);
  992.     }
  993. }
  994.  
  995. void mystring::operator= (const char * str){
  996.     if(cstring != NULL)
  997.         delete [] cstring;
  998.     cstring = new char[strlen(str) + 1];
  999.     copy(str);
  1000. }
  1001.  
  1002. void mystring::operator= (char c){
  1003.     if(cstring != NULL)
  1004.         delete [] cstring;
  1005.     cstring = new char[2];
  1006.     cstring[0] = c;
  1007.     cstring[1] = '\0';
  1008. }
  1009.  
  1010. /*bool swap(const char * str){
  1011.  
  1012.   char * tmp = new char[strlen(str) + 1];
  1013.  
  1014.   copy();
  1015.   }*/
  1016.  
  1017. //internal function for strlength
  1018. int mystring::strlen(const char * str)const{
  1019.     int i = 0;
  1020.  
  1021.     if(cstring != NULL){
  1022.         while(str[i] != '\0')
  1023.             i++;
  1024.         return i;
  1025.     }
  1026.     else return 0;
  1027.  
  1028. }
  1029.  
  1030. //lets me cout the class object
  1031. std::ostream& operator<< (std::ostream &out, const mystring & str){
  1032.     if(str.cstring != NULL)
  1033.         out << str.cstring;
  1034.     return out;
  1035. }
  1036.  
  1037. //lets me cin a cstring into the class object
  1038. std::istream& operator>> (std::istream &in, const mystring &str){
  1039.     in >> str.cstring;
  1040.     return in;
  1041. }
  1042.  
  1043. //lets you access the individual char of the cstring
  1044. char mystring::operator[] (int a){
  1045.  
  1046.     if(cstring != NULL){
  1047.         if(a >= 0 && a < len())
  1048.             return cstring[a];
  1049.     }
  1050.     else return '\0';
  1051. }
  1052.  
  1053. //same as cat
  1054. bool mystring::operator+= (const char*  add){
  1055.     return cat(add);
  1056. }
  1057.  
  1058. bool mystring::operator+= (const mystring& add){
  1059.     return cat(add);
  1060. }
  1061.  
  1062. bool mystring::operator+= (char add){
  1063.  
  1064.     int i = 0;
  1065.     if(cstring != NULL){
  1066.         char * tmp = new char[len() + 2];
  1067.  
  1068.         for(i = 0; i < len(); i++){
  1069.             tmp[i] = cstring[i];
  1070.         }
  1071.         tmp[i] = add;
  1072.         i++;
  1073.         tmp[i] = '\0';
  1074.         delete [] cstring;
  1075.         cstring = tmp;
  1076.         return true;
  1077.     }
  1078. }
  1079.  
  1080. #include "mystring.h"
  1081. #include <iostream>
  1082.  
  1083. using namespace std;
  1084.  
  1085. class test{
  1086.  
  1087.     public:
  1088.         test();
  1089.         friend ostream& operator<< (ostream &out, const test & rhs);
  1090.  
  1091.     private:
  1092.         mystring name;
  1093. };
  1094.  
  1095. ostream& operator<< (ostream &out, const test & rhs){
  1096.         out << rhs.name;
  1097.         return out;
  1098. }
  1099. test::test(){
  1100.     name = "bob";
  1101. }
  1102.  
  1103. int main(){
  1104.     test * sample;
  1105.  
  1106.     sample = new test;
  1107.  
  1108.     cout << *sample;
  1109.  
  1110.     return 0;
  1111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement