fork(7) download
  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. std::string *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. //THE CODE FAILS HERE AT THIS DELETE (I think)
  30. delete[] lineArray; //delete the old array
  31. lineArray = newArray; //point the old array to the new array
  32. nLines = newLength; //set new array size
  33. }
  34. void MyBag::add(std::string line)
  35. {
  36. ResizeArray(nLines+1); //add one to the array size
  37. lineArray[nLines] = line; //add the new line to the now extended array
  38. nLines++;
  39. }
  40. int MyBag::size()
  41. {
  42. return nLines;
  43. //return size of array
  44. }
  45. void MyBag::lineNum (int index)
  46. {
  47. std::cout << lineArray[index-1] << std::endl;
  48. //return current array index
  49. }
  50. void MyBag::lineSearch (std::string key)
  51. {
  52. int counter = 0;
  53. for (int n=nLines-1; n>0; n--)
  54. {
  55. if (lineArray[n].find(key) != std::string::npos)
  56. {
  57. std::cout << lineArray[n] << std::endl;
  58. counter++;
  59. }
  60. }
  61. if (counter == 0)
  62. {
  63. std::cout << "No matching lines" << std::endl;
  64. }
  65. //search for a specific string within a line
  66. }
  67.  
  68. /*
  69.  * weblogclient.cpp
  70.  *
  71.  *
  72.  */
  73.  
  74. #include <iostream>
  75. #include <fstream>
  76. #include <cassert>
  77. #include <string>
  78. #include "dynamic_array_header.h"
  79. #include "using_vectors_header.h"
  80.  
  81. using namespace std;
  82.  
  83. void getData(MyBag& webLog);
  84. void printResults(MyBag& webLog);
  85.  
  86. int main()
  87. {
  88. MyBag webLog;
  89.  
  90. getData(webLog);
  91. printResults(webLog);
  92.  
  93. return 0;
  94. }
  95.  
  96. //********************************************************************
  97.  
  98. void getData(MyBag& webLog)
  99. // This function gets the data from a file.
  100.  
  101. {
  102. ifstream inFile;
  103. string webLine;
  104.  
  105. inFile.open("weblog_medium.txt");
  106. assert(inFile);
  107.  
  108. while (getline(inFile, webLine))
  109. {
  110. webLog.add(webLine);
  111. std::cout << webLine << std::endl;
  112. }
  113. inFile.close();
  114. inFile.clear();
  115.  
  116. }
  117.  
  118. //********************************************************************
  119.  
  120. void printResults(MyBag& webLog)
  121.  
  122.  
  123. {
  124.  
  125. cout << "There were " << webLog.size()
  126. << " total page requests." << endl << endl;
  127.  
  128.  
  129. cout << "Line number 5: \n";
  130. webLog.lineNum(5);
  131.  
  132. cout << "Search for IP 24.138.48.78: \n";
  133. webLog.lineSearch("24.138.48.78");
  134.  
  135. cout << "Search for IP 24.138.48.79: \n";
  136. webLog.lineSearch("24.138.48.79");
  137. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty