Guest User

Untitled

a guest
May 13th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. class MyBag
  2. {
  3. private:
  4.     int nLines;
  5.     std::string *lineArray;
  6.     void ResizeArray(int newLength);
  7. public:
  8.     MyBag();
  9.     int size();
  10.     void add (std::string line);
  11.     void lineNum (int index);
  12.     void lineSearch (std::string key);
  13. };
  14.  
  15. MyBag::MyBag()
  16. {
  17.     nLines = 0;
  18.     lineArray = new std::string[0] ();
  19. }
  20. void MyBag::ResizeArray(int newLength)
  21. {
  22.     std::string *newArray = new std::string[newLength];
  23.     //create new array with new length
  24.     for (int nIndex=0; nIndex < nLines; nIndex++)
  25.     {
  26.         newArray[nIndex] = lineArray[nIndex];
  27.         //copy the old array into the new array
  28.     }
  29.     delete[] lineArray; //delete the old array
  30.     lineArray = newArray; //point the old array to the new array
  31.     nLines = newLength; //set new array size
  32. }
  33. void MyBag::add(std::string line)
  34. {
  35.     ResizeArray(nLines+1); //add one to the array size
  36.     lineArray[nLines] = line; //add the new line to the now extended array
  37.     nLines++;
  38. }
  39. int MyBag::size()
  40. {
  41.     return nLines;
  42.     //return size of array
  43. }
  44. void MyBag::lineNum (int index)
  45. {
  46.     std::cout << lineArray[index-1] <<  std::endl;
  47.     //return current array index
  48. }
  49. void MyBag::lineSearch (std::string key)
  50. {
  51.     int counter = 0;
  52.     for (int n=nLines-1; n>0; n--)
  53.     {
  54.         if (lineArray[n].find(key) != std::string::npos)
  55.         {
  56.             std::cout << lineArray[n] << std::endl;
  57.             counter++;
  58.         }
  59.     }
  60.     if (counter == 0)
  61.     {
  62.         std::cout << "No matching lines" << std::endl;
  63.     }
  64.     //search for a specific string within a line
  65. }
  66.  
  67.  
  68.  
  69.  
  70. /*
  71.  * weblogclient.cpp
  72.  *
  73.  *
  74.  */
  75.  
  76. #include <iostream>
  77. #include <fstream>
  78. #include <cassert>
  79. #include <string>
  80. #include "dynamic_array_header.h"
  81. #include "using_vectors_header.h"
  82.  
  83. using namespace std;
  84.  
  85. void getData(MyBag& webLog);
  86. void printResults(MyBag& webLog);
  87.  
  88. int main()
  89. {
  90.     MyBag webLog;
  91.  
  92.     getData(webLog);
  93.     printResults(webLog);
  94.  
  95.     return 0;
  96. }
  97.  
  98. //********************************************************************
  99.  
  100. void getData(MyBag& webLog)
  101. // This function gets the data from a file.
  102.  
  103. {
  104.     ifstream inFile;
  105.     string webLine;
  106.  
  107.     inFile.open("weblog_medium.txt");
  108.     assert(inFile);
  109.  
  110.     while (getline(inFile, webLine))
  111.        {
  112.         webLog.add(webLine);
  113.         std::cout << webLine << std::endl;
  114.        }
  115.         inFile.close();
  116.         inFile.clear();
  117.  
  118. }
  119.  
  120. //********************************************************************
  121.  
  122. void printResults(MyBag& webLog)
  123.  
  124.  
  125. {
  126.  
  127.     cout << "There were " << webLog.size()
  128.          << " total page requests." << endl << endl;
  129.  
  130.  
  131.     cout << "Line number 5: \n";
  132.       webLog.lineNum(5);
  133.  
  134.     cout << "Search for IP 24.138.48.78: \n";
  135.       webLog.lineSearch("24.138.48.78");
  136.  
  137.       cout << "Search for IP 24.138.48.79: \n";
  138.       webLog.lineSearch("24.138.48.79");
  139. }
Advertisement
Add Comment
Please, Sign In to add comment