Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include "mystring.h"
- #include "myistring.h"
- //using namespace std;
- class course {
- public:
- course();
- course(const char*, int = 0, int = 0, const char* = '\0', int = 0, const char* = '\0', const char* = '\0', int = 0, int = 0);
- course(const course& );
- ~course();
- bool setName(char *);
- bool setSection(int);
- bool setCall(int);
- bool setTitle(char *);
- bool setTime(int);
- bool setDays(char *);
- bool setInstruct(char *);
- bool setCredits(int);
- bool setFee(int);
- const mystring getName()const;
- const myistring getSection()const;
- const myistring getCall()const;
- const mystring getTitle()const;
- const myistring getTime()const;
- const mystring getDays()const;
- const mystring getInstruct()const;
- int getCredits()const;
- int getFee()const;
- bool setNext(course * );
- course* getNext()const;
- //void next();
- void print()const;
- bool compare(const course& );
- bool operator== (const course& );
- course operator=(const course &);
- // friend std::ostream& operator<< (std::ostream &, const course&);
- friend std::ostream& operator<< (std::ostream &, const course &);
- private:
- mystring name;
- myistring sect;
- myistring call;
- mystring title;
- myistring time;
- mystring days;
- mystring instruc;
- int credits;
- int fee;
- course * next;
- };
- #include <iostream>
- class myistring{
- public:
- myistring();
- myistring(int);
- myistring(const myistring&);
- ~myistring();
- bool copy(const int *);
- int len();
- bool operator== (const int *)const;
- bool operator== (const myistring& )const;
- bool operator!= (const int *)const;
- bool operator!= (const myistring&)const;
- void operator= (const int *);
- void operator= (int);
- int& operator[] (int);
- friend std::ostream& operator<< (std::ostream &, const myistring & );
- private:
- int * istring;
- void intToArr(int source);
- };
- #include <iostream>
- class mystring{
- public:
- mystring();
- mystring(const char *);
- mystring(const mystring& );
- mystring(char);
- ~mystring();
- bool copy(const char *);
- bool copy(const mystring & );
- int len()const;
- bool cat(const char *);
- bool cat(const mystring &);
- const char* get()const;
- bool sort();
- //bool swap(const mystring&);
- //int cmp(const char *, const char *);
- bool operator== (const mystring &)const;
- bool operator!= (const mystring &)const;
- bool operator== (const char *)const;
- bool operator!= (const char * rhs)const;
- void operator= (const mystring &);
- void operator= (const char *);
- void operator= (char);
- char operator[] (int);
- bool operator+= (const mystring&);
- bool operator+= (const char* );
- bool operator+= (char);
- friend std::ostream& operator<< (std::ostream &, const mystring&);
- friend std::istream& operator>> (std::istream &, const mystring&);
- friend std::ostream& operator<< (std::ostream &, const course&);
- private:
- char * cstring;
- int strlen(const char *)const;
- };
- #include <iostream>
- #include "course.h"
- using namespace std;
- course::course(){
- //mystring and myistring have the constructors in their class already
- credits = 0;
- fee = 0;
- next = NULL;
- }
- course::course(const char* rname, int rsect, int rcall, const char* rtitle, int rtime, const char* rdays, const char* rinstruc, int rcredits, int rfee){
- name = rname;
- sect = rsect;
- call = rcall;
- title = rtitle;
- time = rtime;
- days = rdays;
- instruc = rinstruc;
- if(rcredits >= 0 && rcredits <= 5)
- credits = rcredits;
- if(rfee >= 0)
- fee = rfee;
- next = NULL;
- }
- course::course(const course& rhs){
- name = rhs.name;
- sect = rhs.sect;
- call = rhs.call;
- title = rhs.title;
- time = rhs.time;
- days = rhs.days;
- instruc = rhs.instruc;
- credits = rhs.credits;
- fee = rhs.fee;
- }
- course::~course(){
- //mystring and myistring already have the destructor included
- fee = 0;
- credits = 0;
- next = NULL;
- }
- bool course::setName(char * n){
- name = n;
- return true;
- }
- bool course::setSection(int s){
- sect = s;
- return true;
- }
- bool course::setCall(int c){
- call = c;
- return true;
- }
- bool course::setTitle(char * t){
- title = t;
- return true;
- }
- bool course::setTime(int t){
- time = t;
- return true;
- }
- bool course::setDays(char * d){
- days = d;
- return true;
- }
- bool course::setInstruct(char * n){
- instruc = n;
- return true;
- }
- bool course::setCredits(int c){
- if (c <= 5 && c >= 0){
- credits = c;
- return true;
- } else{
- credits = 0;
- return false;
- }
- }
- bool course::setFee(int f){
- if (f <= 250 && f >= 0){
- fee = f;
- return true;
- } else{
- fee = 0;
- return false;
- }
- }
- const mystring course::getName()const{
- return name;
- }
- const myistring course::getSection()const{
- return sect;
- }
- const myistring course::getCall()const{
- return call;
- }
- const mystring course::getTitle()const{
- return title;
- }
- const myistring course::getTime()const{
- return time;
- }
- const mystring course::getDays()const{
- return days;
- }
- const mystring course::getInstruct()const{
- return instruc;
- }
- int course::getCredits()const{
- return credits;
- }
- int course::getFee()const{
- return fee;
- }
- void course::print()const{
- int i = 0;
- cout << "Name: " << name << endl;
- ////////////
- cout << "Section: ";
- cout << sect;
- cout << endl;
- //////////////
- cout << "Call Number: ";
- cout << call;
- cout << endl;
- //////////////
- cout << "Title: " << title << endl;
- //////////////
- cout << "Time: ";
- cout << time << endl;
- //////////////
- cout << "Days: " << days << endl;
- cout << "Instructor: " << instruc << endl;
- cout << "Credits: " << credits << endl;
- cout << "Fee: $" << fee << endl;
- cout << "#####################################" << endl;
- }
- bool course::compare(const course& rhs){
- bool same = true;
- if(this == &rhs)
- return true;
- if(name != rhs.name)
- same = false;
- if(sect != rhs.sect)
- same = false;
- if(call != rhs.call)
- same = false;
- if(title != rhs.title)
- same = false;
- if(time != rhs.time)
- same = false;
- if(days != rhs.days)
- same = false;
- if(instruc != rhs.instruc)
- same = false;
- if(credits != rhs.credits)
- same = false;
- if(fee != rhs.fee)
- same = false;
- return same;
- }
- bool course::operator== (const course& rhs){
- return compare(rhs);
- }
- bool course::setNext(course * tmp){
- next = tmp;
- return true;
- }
- course* course::getNext()const{
- return next;
- }
- course course::operator=(const course & rhs){
- if(this != &rhs){
- name = rhs.name;
- sect = rhs.sect;
- call = rhs.call;
- title = rhs.title;
- days = rhs.days;
- instruc = rhs.instruc;
- credits = rhs.credits;
- fee = rhs.fee;
- return *this;
- }
- else return *this;
- }
- //std::ostream& operator<< (std::ostream &out, const course &rhs){
- ostream& operator<< (ostream &out, const course & rhs) const
- {
- out << "Name: " << rhs.name <<endl;
- /////////////
- out << "Section: ";
- out << rhs.sect;
- out <<endl;
- /////////////
- out << "Call Number: ";
- out << rhs.call;
- out <<endl;
- /////////////
- out << "Title: " << rhs.title <<endl;
- /////////////
- out << "Time: ";
- out << rhs.time <<endl;
- /////////////
- out << "Days: " << rhs.days <<endl;
- out << "Instructor: " << rhs.instruc <<endl;
- out << "Credits: " << rhs.credits <<endl;
- out << "Fee: $" << rhs.fee <<endl;
- out << "#####################################" <<endl;
- return out;
- }
- #include "course.h"
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- using namespace std;
- const char FILENAME[] = "courses.txt";
- void readInfo(ifstream & , course* & );
- void newNode(course * &);
- void deleteList(course *);
- void deleteNode(course* & head);
- int main(){
- course * head = NULL;
- course * tmp = NULL;
- ifstream fin;
- bool cont = true;
- char * ctmp = new char[100];
- int itmp;
- char selection;
- course * test;
- test = new course;
- course test1;
- cout << test1;
- // cout << test1 << endl;
- fin.open(FILENAME);
- /*
- //create a new head
- head = new course;
- tmp = head;
- readInfo(fin, head);
- //read in the data while creating new nodes as needed
- while(fin.good()){
- fin.peek();
- if(fin.good()){
- newNode(tmp);
- tmp = tmp->getNext();
- readInfo(fin, tmp);
- //tmp->print();
- }
- }
- system("clear");
- //menu
- do{
- tmp = head;
- cout << "1: Add a course" << endl;
- cout << "2: Delete a course" << endl;
- cout << "3: Print the courses" << endl;
- cout << "4: Quit" << endl;
- cin >> selection;
- switch(selection){
- case '1' :
- case 'a' :
- //get info for course
- tmp = head;
- while(tmp->getNext() != NULL){
- tmp = tmp->getNext();
- }
- newNode(tmp);
- cout << endl << "Whats the new name?" ;
- cin.get();
- cin.getline(ctmp, 100);
- tmp->setName(ctmp);
- cout << endl << "Whats the new section number?" ;
- cin >> itmp;
- tmp->setSection(itmp);
- cout << endl << "Whats the new call number?" ;
- cin >> itmp;
- tmp->setCall(itmp);
- cout << endl << "Whats the new course title?";
- cin.get();
- cin.getline(ctmp, 100);
- tmp->setTitle(ctmp);
- cout << endl << "Whats the new time?";
- cin >> itmp;
- tmp->setTime(itmp);
- cout << endl << "What are the new days?";
- cin.get();
- cin.getline(ctmp, 100);
- tmp->setDays(ctmp);
- cout << endl << "Whats the new instructor?";
- cin.get();
- cin.getline(ctmp, 100);
- tmp->setInstruct(ctmp);
- cout << endl << "How many credits is it?";
- cin >> itmp;
- tmp->setCredits(itmp);
- cout << endl <<"How much is the fee?";
- cin >> itmp;
- tmp->setFee(itmp);
- break;
- case '2' :
- case 'd' :
- //delete course
- deleteNode(head);
- break;
- case '3' :
- case 'p' :
- tmp = head;
- //print 'em out
- while(tmp->getNext() != NULL){
- //tmp->print();
- cout << *tmp << endl;;
- tmp = tmp->getNext();
- }
- break;
- case '4' :
- case 'q' :
- cont = false;
- break;
- }
- }while(cont == true);
- tmp = head;
- //delete the list
- deleteList(head);
- delete [] ctmp;
- */
- return 0;
- }
- void readInfo(ifstream & fin, course* & node){
- int itmp = 0;
- char * ctmp = new char [100];
- fin.peek();
- if(fin.good()){
- if(fin.peek() == '\n')
- fin.get();
- fin.getline(ctmp, 99, '&');
- node->setName(ctmp);
- fin >> itmp;
- node->setSection(itmp);
- fin >> itmp;
- node->setCall(itmp);
- fin.getline(ctmp, 99, '&');
- node->setTitle(ctmp);
- fin >> itmp;
- node->setTime(itmp);
- fin.getline(ctmp, 99, '&');
- node->setDays(ctmp);
- fin.getline(ctmp, 99, '&');
- node->setInstruct(ctmp);
- fin >> itmp;
- node->setCredits(itmp);
- fin >> itmp;
- node->setFee(itmp);
- }
- }
- void newNode(course * &oldNode){
- course * tmp;
- tmp = new course;
- oldNode->setNext(tmp);
- tmp->setNext(NULL);
- }
- void deleteList(course * head){
- course * tmp = head;
- while(head->getNext() != NULL){
- tmp = tmp->getNext();
- delete head;
- head = tmp;
- }
- delete head;
- }
- void deleteNode(course* & head){
- course * tmp = head;
- course * tmp1 = NULL;
- int i = 1, num = 0;
- cout << "Which node would you like to delete? (0 for none)" << endl;
- while(tmp->getNext() != NULL){
- cout << i << ". " << tmp->getName() << endl;
- tmp = tmp->getNext();
- i++;
- }
- cin >> num;
- //exception for first node
- tmp = head;
- if(num == 1){
- head = head->getNext();
- delete tmp;
- }
- //goes down the list until it reaches the before node, links the prev to the next, and deletes the correct one
- else{
- if(num > 0){
- for(i = 0; i < num - 2; i++){
- tmp = tmp->getNext();
- }
- tmp1 = tmp->getNext();
- tmp->setNext(tmp1->getNext());
- delete tmp1;
- }
- }
- }
- #include "myistring.h"
- #include <cstdio>
- myistring::myistring(){
- istring = NULL;
- }
- myistring::myistring(int a){
- istring = NULL;
- intToArr(a);
- }
- //copy constructor
- myistring::myistring(const myistring& rhs){
- if(istring != NULL)
- istring = new int[rhs.istring[0] + 1];
- copy(rhs.istring);
- }
- myistring::~myistring(){
- if(istring != NULL)
- delete [] istring;
- istring = NULL;
- }
- bool myistring::copy(const int * source){
- int i = 0;
- if(istring != NULL){
- delete [] istring;
- istring = NULL;
- }
- istring = new int[source[0] + 1];
- for(i = 0; i <= source[0]; i++){
- istring[i] = source[i];
- }
- return true;
- }
- int myistring::len(){
- if(istring != NULL)
- return istring[0];
- else return 0;
- }
- bool myistring::operator== (const int * rhs)const{
- if(istring == NULL)
- return false;
- if(rhs[0] != istring[0])
- return false;
- for(int i = 1; i <= rhs[0]; i++)
- if(rhs[i] != istring[i])
- return false;
- return true;
- }
- bool myistring::operator== (const myistring& rhs)const{
- return operator== (rhs.istring);
- }
- bool myistring::operator!= (const int * rhs)const{
- if(istring == NULL)
- return true;
- if(rhs[0] != istring[0])
- return true;
- for(int i = 1; i <= rhs[0]; i++)
- if(rhs[i] != istring[i])
- return true;
- return false;
- }
- bool myistring::operator!= (const myistring& rhs) const{
- return operator!= (rhs.istring);
- }
- void myistring::operator= (const int * rhs){
- copy(rhs);
- }
- void myistring::operator= (int a){
- intToArr(a);
- }
- int& myistring::operator[] (int a){
- if(a >= 0 && a >= istring[0])
- return istring[a];
- }
- void myistring::intToArr(int source){
- int i = 0, length = 0, stmp = source;
- char * tmp = NULL ;
- //gets the length of the source
- while(stmp > 0){
- stmp /= 10;
- length++;
- }
- tmp = new char[length +1];
- //converts the source into a char array
- sprintf(tmp, "%d", source);
- if(istring != NULL){
- delete [] istring;
- }
- istring = new int[length + 1];
- //converts the char array to the int array
- for(i = 0; i < length; i++){
- istring[ i+ 1] = tmp[i] - '0';
- }
- istring[0] = length;
- delete [] tmp;
- }
- std::ostream& operator<< (std::ostream &out, const myistring & str){
- if(str.istring != NULL)
- for(int i = 1; i <= str.istring[0]; i++){
- out << str.istring[i];
- }
- return out;
- }
- std::istream& operator>> (std::istream &in, const myistring & str){
- /*
- int num;
- in >> num;
- myistring::str.intToArr(num);
- return num;
- */
- }
- #include "mystring.h"
- //int const NULL = 0;
- mystring::mystring(){
- cstring = new char[1];
- cstring[0] = '\0';
- }
- //this one lets you construc a string when you declare it
- mystring::mystring(const char * word){
- cstring = new char[strlen(word) + 1];
- copy(word);
- }
- //copy constructor
- mystring::mystring(const mystring& word){
- cstring = new char[word.len() + 1];
- copy(word.cstring);
- }
- mystring::mystring(char c){
- cstring = new char[2];
- cstring[0] = c;
- cstring[1] = '\0';
- }
- mystring::~mystring(){
- if(cstring != NULL){
- delete [] cstring;
- cstring = NULL;
- }
- }
- //copies a c-string into the class' string
- bool mystring::copy(const char * source){
- int i = 0;
- if(cstring != NULL){
- while(source[i] != '\0'){
- cstring[i] = source[i];
- i++;
- }
- cstring[i] = '\0';
- return true;
- }
- }
- bool mystring::copy(const mystring & source){
- return copy(source.cstring);
- }
- //returns the length of the class c-string, excludes the NULL
- int mystring::len()const{
- int i = 0;
- if(cstring != NULL){
- while(cstring[i] != '\0')
- i++;
- return i;
- }
- else return 0;
- }
- //takes the cstring and adds the source to the end of it
- bool mystring::cat(const char * source){
- int i = 0, j = -1;
- if(cstring != NULL){
- char * tmp = new char[len() + strlen(source) + 1];
- while(cstring[i] != '\0'){
- tmp[i] = cstring[i];
- i++;
- }
- do{
- j++;
- tmp[i+j] = source[j];
- } while(source[j] != '\0');
- delete [] cstring;
- cstring = tmp;
- return true;
- }
- }
- bool mystring::cat(const mystring & source){
- return cat(source.cstring);
- }
- //returns the cstring pointer
- const char* mystring::get()const{
- return cstring;
- }
- bool mystring::sort(){
- int length = len();
- bool swapped = false;
- do{
- swapped = false;
- for(int i = 0; i < length - 1; i++){
- if(cstring[i] > cstring[i+1]){
- //swap
- cstring[i] = cstring[i] ^ cstring[i+1];
- cstring[i+1] = cstring[i] ^ cstring[i+1];
- cstring[i] = cstring[i] ^ cstring[i+1];
- swapped = true;
- }
- }
- length -= 1;
- }while(swapped);
- return true;
- }
- //checks if the two cstrings are equal
- bool mystring::operator== (const mystring & rhs)const{
- return operator== (rhs.cstring);
- }
- //checks if the two cstrings are not equal
- bool mystring::operator!= (const mystring & rhs)const{
- return operator!= (rhs.cstring);
- }
- bool mystring::operator== (const char * rhs)const{
- int len1 = 0;
- if(cstring != NULL){
- len1 = len();
- }
- else return false;
- int len2 = strlen(rhs);
- if (len1 != len2)
- return false;
- else if(len1 == len2)
- for(int i = 0; i < len1; i++){
- if(cstring[i] != rhs[i])
- return false;
- }
- else return true;
- }
- bool mystring::operator!= (const char * rhs)const{
- int len1 = 0;
- len1 =len();
- int len2 = strlen(rhs);
- if(len1 != len2)
- return true;
- for(int i = 0; i < len1; i++)
- if(cstring[i] != rhs[i])
- return true;
- return false;
- }
- //copies the righ hand side into the class string replacing what's inside
- void mystring::operator= (const mystring & rhs){
- if(cstring != NULL)
- delete [] cstring;
- if(this != &rhs){
- cstring = new char[rhs.len() + 1];
- copy(rhs.cstring);
- }
- }
- void mystring::operator= (const char * str){
- if(cstring != NULL)
- delete [] cstring;
- cstring = new char[strlen(str) + 1];
- copy(str);
- }
- void mystring::operator= (char c){
- if(cstring != NULL)
- delete [] cstring;
- cstring = new char[2];
- cstring[0] = c;
- cstring[1] = '\0';
- }
- /*bool swap(const char * str){
- char * tmp = new char[strlen(str) + 1];
- copy();
- }*/
- //internal function for strlength
- int mystring::strlen(const char * str)const{
- int i = 0;
- if(cstring != NULL){
- while(str[i] != '\0')
- i++;
- return i;
- }
- else return 0;
- }
- //lets me cout the class object
- std::ostream& operator<< (std::ostream &out, const mystring & str){
- if(str.cstring != NULL)
- out << str.cstring;
- return out;
- }
- //lets me cin a cstring into the class object
- std::istream& operator>> (std::istream &in, const mystring &str){
- in >> str.cstring;
- return in;
- }
- //lets you access the individual char of the cstring
- char mystring::operator[] (int a){
- if(cstring != NULL){
- if(a >= 0 && a < len())
- return cstring[a];
- }
- else return '\0';
- }
- //same as cat
- bool mystring::operator+= (const char* add){
- return cat(add);
- }
- bool mystring::operator+= (const mystring& add){
- return cat(add);
- }
- bool mystring::operator+= (char add){
- int i = 0;
- if(cstring != NULL){
- char * tmp = new char[len() + 2];
- for(i = 0; i < len(); i++){
- tmp[i] = cstring[i];
- }
- tmp[i] = add;
- i++;
- tmp[i] = '\0';
- delete [] cstring;
- cstring = tmp;
- return true;
- }
- }
- #include "mystring.h"
- #include <iostream>
- using namespace std;
- class test{
- public:
- test();
- friend ostream& operator<< (ostream &out, const test & rhs);
- private:
- mystring name;
- };
- ostream& operator<< (ostream &out, const test & rhs){
- out << rhs.name;
- return out;
- }
- test::test(){
- name = "bob";
- }
- int main(){
- test * sample;
- sample = new test;
- cout << *sample;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement