Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. //CS202 Program #1 Ryan Larson
  2. //Header file including classes
  3. #include<iostream>
  4. using namespace std;
  5. #include<cstring>
  6. using namespace std;
  7.  
  8. const int TITLE = 100;
  9. const int BODY = 600;
  10. const int SIZE = 20;
  11.  
  12. struct thread
  13. {
  14.        char * post_title;
  15.        char * post_body;
  16. };
  17.  
  18. struct temp_thread
  19. {
  20.        char title[TITLE];
  21.        char post_body[BODY];
  22. };
  23.  
  24. struct node
  25. {
  26.        thread data;
  27.        node * next;
  28. };
  29.  
  30. class forum
  31. {
  32.       public:
  33.              forum();  //constructor
  34.              ~forum(); //destructor
  35.              int display_all(); //displays all threads
  36.       protected:
  37.               node *topic[SIZE];
  38. };
  39.  
  40. class new_thread : public forum //derived class of class forum
  41. {
  42.  public:
  43.             new_thread();
  44.             int newthread(temp_thread & temp);
  45. };
  46.  
  47. class new_post : public forum //derived class of class forum
  48. {
  49.       public:
  50.              new_post();
  51.              int search(temp_thread & temp);
  52. };
  53.  
  54. //CS202 Program 1 Ryan Larson
  55. //Class implementation
  56.  
  57. forum::forum()//constructor with Int argument for the size
  58. {
  59.  
  60.         for(int i = 0; i < SIZE; ++i)
  61.         {
  62.                 topic[i] = NULL;
  63.     }
  64. }
  65. forum::~forum()
  66. {
  67.         node * current = NULL;
  68.         int i = 0;
  69.         if(topic[i])
  70.         {
  71.         for(i = 0; i < SIZE; ++i)
  72.         {
  73.         current = topic[i];
  74.         topic[i] = topic[i]->next; //the tail is now the head
  75.         if(topic[i])
  76.         {
  77.         do{
  78.         delete [] topic[i]->data.post_body;
  79.         delete [] topic[i]->data.post_title;
  80.         delete topic[i];
  81.     topic[i] = topic[i]->next;
  82.         }while (current != topic[i]);
  83.         }
  84.         }
  85.     }
  86. }
  87.  
  88. int forum::display_all()
  89. {
  90.     node * current;
  91.  
  92.     for (int i = 0; i<SIZE; ++i)
  93.     {
  94.         current = topic[i]->next;
  95.         while(current) //while list is not NULL
  96.         {
  97.          cout<<"New Thread: " <<endl <<endl;
  98.          cout<<"Post title: " <<current->data.post_title <<endl;
  99.          cout<<"Post body:  " <<current->data.post_body <<endl <<endl;
  100.         }
  101.     }
  102. }
  103.  
  104. new_thread::new_thread()
  105. {}
  106.  
  107. int new_thread::newthread(temp_thread & temp)
  108. {
  109.     int i = 0;
  110.  
  111.     if(!topic[i]){
  112.     topic[i] = new node;
  113.     topic[i]->data.post_title = new char[strlen(temp.title) +1];
  114.     strcpy(topic[i]->data.post_title, temp.title);
  115.     topic[i]->data.post_body = new char[strlen(temp.post_body) +1];
  116.     strcpy(topic[i]->data.post_body, temp.post_body);
  117.     topic[i]->next = topic[i];
  118.     }
  119.     else{
  120.  
  121.     while(topic[i])//loop to traverse and find the next NULL pointer in the array
  122.     {
  123.       ++i;
  124.     }
  125.     topic[i] = new node;
  126.     topic[i]->data.post_title = new char[strlen(temp.title) +1];
  127.     strcpy(topic[i]->data.post_title, temp.title);
  128.     topic[i]->data.post_body = new char[strlen(temp.post_body) +1];
  129.     strcpy(topic[i]->data.post_body, temp.post_body);
  130.     topic[i]->next = topic[i];
  131.     }
  132. }
  133.  
  134. new_post::new_post()
  135. {}
  136.  
  137. int new_post::search(temp_thread & temp)
  138. {
  139.     int i = 0;
  140.     for(i = 0; i < SIZE; ++i)//loop to find the thread being searched for
  141.     {
  142.             if(strcmp(temp.title, topic[i]->next->data.post_title) == 0)
  143.             cout<<"Post Found!" <<endl <<endl;
  144.     }
  145.     node * current = topic[i];
  146.     node * temp1 = topic[i]->next;
  147.     node * temp2 = NULL;
  148.     do{  //loop to display all posts in a given thread
  149.          cout<<"Post Title: " <<topic[i]->data.post_title <<endl;
  150.          cout<<"BODY: " <<topic[i]->data.post_body <<endl <<endl;
  151.          temp2 = topic[i];//finds the last node in the CLL
  152.          topic[i] = topic[i]->next;
  153.     }while(topic[i] != current);
  154.  
  155.     temp1 = NULL;
  156.     temp1 = new node;
  157.  
  158.     cout<<"Please enter the title for your reply post: ";
  159.  cin.get(temp1->data.post_title, 100); cin.ignore(100, '\n');
  160.     cout<<"Please enter the body for your reply post: ";
  161.     cin.get(temp1->data.post_body, 600); cin.ignore(100, '\n');
  162.     temp1->next = topic[i]->next;
  163.     topic[i]->next = temp1;
  164. }
  165. //CS202 Ryan Larson Program #1
  166. //Client Program
  167.  
  168.  
  169. int main()
  170. {
  171.     temp_thread temp_data; //object of struct temp_thread
  172.     char decision; //character variable for the menu decision
  173.     forum discussion; //object of the forum class
  174.     new_thread newthread; //object of the new_thread class
  175.     new_post newpost; //object of the new_post class
  176.     temp_thread tempdata; //object of the temp_thread class
  177.  
  178.  
  179.     cout<< "Welcome to the discussion forum" <<endl <<endl; //Welcome the user
  180.     cout<< "What would you like to do?" <<endl <<endl;  //Menu
  181.     cout<< "V to view all discussion threads" <<endl;
  182.     cout<< "A to add a new discussion thread" <<endl;
  183.     cout<< "S to search for and add to existing thread" <<endl;
  184.  
  185.     cin>> decision; cin.ignore(100, '\n'); //read in the decision
  186.     //if conditions for menu decision
  187.     if(decision == 'v' || decision == 'V')
  188.     {
  189.          discussion.display_all();
  190.     }
  191.     else if(decision == 'A' || decision == 'a')
  192.     {
  193.          cout<<"Create a new thread! " <<endl;
  194.          cout<<"Please enter the thread title: " <<endl;
  195.          cin.get(tempdata.title, 100); cin.ignore(100, '\n');
  196.          cout<<"Please enter the body of the first post " <<endl;
  197.          cin.get(tempdata.post_body, 600); cin.ignore(100, '\n');
  198.          newthread.newthread(tempdata);
  199.     }
  200.     else if(decision == 'S' || decision == 's')
  201.     {
  202.          cout<<"Find a thread and post to it! " <<endl;
  203.          cout<<"Please enter the TITLE of the thread you would like to find: ";
  204.          cin.get(tempdata.title, 100); cin.ignore(100, '\n');
  205.  
  206.          newpost.search(tempdata);
  207.     }
  208.     else
  209.  
  210.  
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement