Plz, helping with writing a bit of code. Thanks

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 13
Reputation: shahid is an unknown quantity at this point 
Solved Threads: 0
shahid shahid is offline Offline
Newbie Poster

Plz, helping with writing a bit of code. Thanks

 
0
  #1
Sep 29th, 2005
Note:- There are three files that will work by combining in a project.

Most of the code for the Tokenizer class is already provided. You are to complete the routine Tokenizer::getTokens. The routine currently handles single entry, single range start and single range end. Your job is to the put in the code that will handle the subrange start and subrange end cases. The place where your code goes is indicated by a comment.

Compile and execute the project once you have put in your code. Look at the the output generated by the main program to make sure everything is working properly.


------------------------------------------------------------------------------------------------------------------------------------------

  1. //main.cpp
  2.  
  3.  
  4. #include <iostream>
  5. #include <stdlib.h>
  6. #include "Tokenizer.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. char entry[80];
  11. char subentry[80];
  12. char page[24];
  13. Tokenizer t("dsaa.txt");
  14. IndexEntryType iType;
  15. int indexLineNo = 0;
  16.  
  17. // It may be worthwhile to send the output to a file instead of cout
  18.  
  19. while ( (iType = t.getTokens( entry, subentry, page )) != ENDOFFILE )
  20. {
  21. indexLineNo++;
  22. cout << indexLineNo << ": ";
  23. switch(iType){
  24. case SINGLEENTRY:
  25. cout << "SingleEntry: " << entry << " " << page << endl;
  26. break;
  27. case SINGLERANGESTART:
  28. cout << "SingleRangeStart: " << entry << " " << page << endl;
  29. break;
  30. case SINGLERANGEEND:
  31. cout << "SingleRangeEnd: " << entry << " " << page << endl;
  32. break;
  33. case SUBRANGESTART:
  34. cout << "SubRangeStart: " << entry << " " << subentry << " " << page << endl;
  35. break;
  36. case SUBRANGEEND:
  37. cout << "SubRangeEnd: " << entry << " " << subentry << " " << page << endl;
  38. break;
  39. default:
  40. cout << "Erroroneous index entry found - skipping " << endl;
  41. break;
  42. }
  43. }
  44. system("PAUSE");
  45.  
  46. return 0;
  47. }

---------------------------------------------------------------------
---------------------------------------------------------------------

  1. //Tokenizer.cpp
  2.  
  3.  
  4. #include "Tokenizer.h"
  5.  
  6. Tokenizer::Tokenizer(char* indexFilename)
  7. {
  8. infile = fopen(indexFilename, "r");
  9. if( infile == NULL){
  10. cout << "Error opening index file " << indexFilename << endl;
  11. }
  12. else
  13. cout << "Opened index file " << indexFilename << endl;
  14. }
  15.  
  16. Tokenizer::~Tokenizer()
  17. {
  18. if( infile != NULL )
  19. fclose(infile);
  20. }
  21.  
  22. IndexEntryType Tokenizer::getTokens( char* entry, char* subentry, char* page )
  23. {
  24. int ip, i, j;
  25. // see DevC++ Help->Help on DevC++->An Introduction to Programming
  26. // ->Topics->File handling for details on fopen, fclose, fgets etc.
  27. if( fgets(line, 80, infile) == NULL ) return ENDOFFILE;
  28.  
  29. // now scan through the input line and extract index entry and
  30. // its type. The types are
  31. // SINGLEENTRY, for example
  32. // IX: {Hutchinson, J. P.} {12}
  33. // SINGLERANGESTART, for example
  34. // IX: {Big-Oh notation|(} {14}
  35. // SINGLERANGEEND, for example
  36. // IX: {Big-Oh notation|)} {16}
  37. // SUBRANGESTART, for example
  38. // IX: {Recursion!four basic rules|(} {10}
  39. // SUBRANGEEND, for example
  40. // IX: {Recursion!four basic rules|)} {10}
  41.  
  42. // first skip over the "IX:" and get to opening "{". The test
  43. // for null \0 is to guard against going beyond the data in line.
  44. ip = 0;
  45. while( line[ip] != '{' && line[ip] != '\0' ) ip++;
  46.  
  47. if( line[ip] == '\0' ) return ERROR; // this shouldn't happen
  48.  
  49. // if we this far, we are positioned at '{'. Skip over it and pick
  50. // index entry. We will stop at '}', '|' or '!'
  51.  
  52. ip++; i=0; // i is array index for entry
  53. while( line[ip] != '}' && line[ip] != '|' &&
  54. line[ip] != '!' && line[ip] != '\0' )
  55. entry[i++] = line[ip++];
  56. entry[i] = '\0'; // must null terminate the entry
  57.  
  58. // we have the main entry. Now take care of range and subrange
  59. // entries
  60.  
  61. if( line[ip] == '}' ){
  62. extractPage( line, ip, page ); // a helper function
  63. return SINGLEENTRY;
  64. }
  65. if( line[ip] == '|' ){
  66. ip = ip+1; // skip over '|'
  67. if( line[ip] == '(' ){
  68. extractPage( line, ip, page ); // a helper function
  69. return SINGLERANGESTART;
  70. }
  71. if( line[ip] == ')' ){
  72. extractPage( line, ip, page ); // a helper function
  73. return SINGLERANGEEND;
  74. }
  75. }
  76. if( line[ip] == '!' ){ // we have a subrange
  77.  
  78.  
  79.  
  80. /***** WRITE CODE *****/
  81. /** WRITE THE CORRECT CODE HERE FOR SUBRANGE entry
  82.   The subentry will be extracted and placed in the
  83.   'subentry' paramter. The page number will go in 'page'
  84.   */
  85.  
  86.  
  87.  
  88. // Temporary: these two lines are to be removed
  89. subentry[0] = page[0] = '\0';
  90. return SUBRANGESTART;
  91.  
  92. }
  93. else return ERROR; // something is wrong in the index line.
  94. }
  95.  
  96. // private methods.
  97.  
  98. // skip until the page entry in the line. Extract the number between
  99. // {}. e.g., {14}
  100. void Tokenizer::extractPage( char* line, int ip, char* page )
  101. {
  102. int j = 0;
  103. // skip characters in line until '{' found
  104. while(line[ip] != '{' && line[ip] != '\0' )
  105. ip++;
  106. if( line[ip] == '\0' )
  107. {
  108. cout << "Tokenizer::extractPage - error! no page number found in index line " << line << endl;
  109. page[0] = '\0';
  110. return;
  111. }
  112. ip = ip+1; // skip over the '{'
  113. while(line[ip] != '}' && line[ip] != '\0' )
  114. page[j++] = line[ip++];
  115. page[j] = '\0';
  116. return;
  117. }

---------------------------------------------------------------------
---------------------------------------------------------------------

  1. //Tokenizer.h
  2.  
  3.  
  4. #ifndef TOKENIZER_H
  5. #define TOKENIZER_H
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <iostream> // Provides cout and cin
  10.  
  11. // token types
  12. enum IndexEntryType {SINGLEENTRY, SINGLERANGESTART, SINGLERANGEEND,
  13. SUBRANGESTART, SUBRANGEEND, ERROR, ENDOFFILE};
  14.  
  15. class Tokenizer
  16. {
  17. public:
  18. Tokenizer(char* indexFilename);
  19. ~Tokenizer();
  20.  
  21. /*
  22.   * each call to getTokens will do the following:
  23.   * 1. read a single line from the index file
  24.   * 2. pick up the main entry
  25.   * 3. pick up the range (start or stop) indicator
  26.   * 4. pick up sub entry if present and its range start or stop
  27.   * indicator
  28.   * 5. pick up the page number.
  29.   *
  30.   * The routine will load the parameter 'entry' and subentry
  31.   * (if found). The page number will be loaded in the 'page'
  32.   * parameter. The function will return value of enum type
  33.   * IndexEntryType according to the entry type.
  34.   */
  35. IndexEntryType getTokens( char* entry, char* subentry, char* page );
  36.  
  37. private:
  38. FILE* infile;
  39. char line[81]; // input buffer for a line from indexfile
  40.  
  41. void extractPage( char* line, int ip, char* page );
  42.  
  43. };
  44. #endif

------------------------------------------------------------------------------------------------------------------------------------------
Last edited by Dave Sinkula; Sep 29th, 2005 at 10:44 am. Reason: Added [code][/code] tags.
Attached Files
File Type: zip CS301_3.zip (5.0 KB, 5 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Plz, helping with writing a bit of code. Thanks

 
0
  #2
Sep 29th, 2005
What is your question.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC