Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CS202 Program #1 Ryan Larson
- //Header file including classes
- #include<iostream>
- using namespace std;
- #include<cstring>
- using namespace std;
- const int TITLE = 100;
- const int BODY = 600;
- const int SIZE = 20;
- struct thread
- {
- char * post_title;
- char * post_body;
- };
- struct temp_thread
- {
- char title[TITLE];
- char post_body[BODY];
- };
- struct node
- {
- thread data;
- node * next;
- };
- class forum
- {
- public:
- forum(); //constructor
- ~forum(); //destructor
- int display_all(); //displays all threads
- protected:
- node *topic[SIZE];
- };
- class new_thread : public forum //derived class of class forum
- {
- public:
- new_thread();
- int newthread(temp_thread & temp);
- };
- class new_post : public forum //derived class of class forum
- {
- public:
- new_post();
- int search(temp_thread & temp);
- };
- //CS202 Program 1 Ryan Larson
- //Class implementation
- forum::forum()//constructor with Int argument for the size
- {
- for(int i = 0; i < SIZE; ++i)
- {
- topic[i] = NULL;
- }
- }
- forum::~forum()
- {
- node * current = NULL;
- int i = 0;
- if(topic[i])
- {
- for(i = 0; i < SIZE; ++i)
- {
- current = topic[i];
- topic[i] = topic[i]->next; //the tail is now the head
- if(topic[i])
- {
- do{
- delete [] topic[i]->data.post_body;
- delete [] topic[i]->data.post_title;
- delete topic[i];
- topic[i] = topic[i]->next;
- }while (current != topic[i]);
- }
- }
- }
- }
- int forum::display_all()
- {
- node * current;
- for (int i = 0; i<SIZE; ++i)
- {
- current = topic[i]->next;
- while(current) //while list is not NULL
- {
- cout<<"New Thread: " <<endl <<endl;
- cout<<"Post title: " <<current->data.post_title <<endl;
- cout<<"Post body: " <<current->data.post_body <<endl <<endl;
- }
- }
- }
- new_thread::new_thread()
- {}
- int new_thread::newthread(temp_thread & temp)
- {
- int i = 0;
- if(!topic[i]){
- topic[i] = new node;
- topic[i]->data.post_title = new char[strlen(temp.title) +1];
- strcpy(topic[i]->data.post_title, temp.title);
- topic[i]->data.post_body = new char[strlen(temp.post_body) +1];
- strcpy(topic[i]->data.post_body, temp.post_body);
- topic[i]->next = topic[i];
- }
- else{
- while(topic[i])//loop to traverse and find the next NULL pointer in the array
- {
- ++i;
- }
- topic[i] = new node;
- topic[i]->data.post_title = new char[strlen(temp.title) +1];
- strcpy(topic[i]->data.post_title, temp.title);
- topic[i]->data.post_body = new char[strlen(temp.post_body) +1];
- strcpy(topic[i]->data.post_body, temp.post_body);
- topic[i]->next = topic[i];
- }
- }
- new_post::new_post()
- {}
- int new_post::search(temp_thread & temp)
- {
- int i = 0;
- for(i = 0; i < SIZE; ++i)//loop to find the thread being searched for
- {
- if(strcmp(temp.title, topic[i]->next->data.post_title) == 0)
- cout<<"Post Found!" <<endl <<endl;
- }
- node * current = topic[i];
- node * temp1 = topic[i]->next;
- node * temp2 = NULL;
- do{ //loop to display all posts in a given thread
- cout<<"Post Title: " <<topic[i]->data.post_title <<endl;
- cout<<"BODY: " <<topic[i]->data.post_body <<endl <<endl;
- temp2 = topic[i];//finds the last node in the CLL
- topic[i] = topic[i]->next;
- }while(topic[i] != current);
- temp1 = NULL;
- temp1 = new node;
- cout<<"Please enter the title for your reply post: ";
- cin.get(temp1->data.post_title, 100); cin.ignore(100, '\n');
- cout<<"Please enter the body for your reply post: ";
- cin.get(temp1->data.post_body, 600); cin.ignore(100, '\n');
- temp1->next = topic[i]->next;
- topic[i]->next = temp1;
- }
- //CS202 Ryan Larson Program #1
- //Client Program
- int main()
- {
- temp_thread temp_data; //object of struct temp_thread
- char decision; //character variable for the menu decision
- forum discussion; //object of the forum class
- new_thread newthread; //object of the new_thread class
- new_post newpost; //object of the new_post class
- temp_thread tempdata; //object of the temp_thread class
- cout<< "Welcome to the discussion forum" <<endl <<endl; //Welcome the user
- cout<< "What would you like to do?" <<endl <<endl; //Menu
- cout<< "V to view all discussion threads" <<endl;
- cout<< "A to add a new discussion thread" <<endl;
- cout<< "S to search for and add to existing thread" <<endl;
- cin>> decision; cin.ignore(100, '\n'); //read in the decision
- //if conditions for menu decision
- if(decision == 'v' || decision == 'V')
- {
- discussion.display_all();
- }
- else if(decision == 'A' || decision == 'a')
- {
- cout<<"Create a new thread! " <<endl;
- cout<<"Please enter the thread title: " <<endl;
- cin.get(tempdata.title, 100); cin.ignore(100, '\n');
- cout<<"Please enter the body of the first post " <<endl;
- cin.get(tempdata.post_body, 600); cin.ignore(100, '\n');
- newthread.newthread(tempdata);
- }
- else if(decision == 'S' || decision == 's')
- {
- cout<<"Find a thread and post to it! " <<endl;
- cout<<"Please enter the TITLE of the thread you would like to find: ";
- cin.get(tempdata.title, 100); cin.ignore(100, '\n');
- newpost.search(tempdata);
- }
- else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement