Advertisement
Guest User

Untitled

a guest
Nov 19th, 2010
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.53 KB | None | 0 0
  1. /***************************************************************
  2. *              cross_reference - a cross-referencing           *
  3. *                                program. It will allow        *
  4. *                                you to store very             *
  5. *                                precise links to information  *
  6. *                                in other documents.           *
  7. *                                                              *
  8. *                         Usage: to start, just type in        *
  9. *                                <cross-reference> on the CMD  *
  10. *                                Interactive commands are:     *
  11. *                                    add                       *
  12. *                                        store a new reference *
  13. *                                    remove                    *
  14. *                                        remove a stored       *
  15. *                                        reference             *
  16. *                                    printref                  *
  17. *                                        print one given       *
  18. *                                        reference             *
  19. *                                    printallrefs              *
  20. *                                        prints all refernces  *
  21. *                                    quit                      *
  22. *                                        quit the program      *
  23. *                                                              *
  24. * *************************************************************/  
  25.  
  26. #include <iostream>
  27. #include <string>
  28. #include <fstream>
  29.  
  30. class linked_list
  31. {
  32.     private:
  33.         class linked_list_element
  34.         {
  35.             private:
  36.                 unsigned int element_number;
  37.                 std::string original_file;
  38.                 unsigned int original_file_line_number;
  39.                 unsigned int original_file_starting_char;
  40.                 std::string new_file;
  41.                 unsigned int new_file_line_number;
  42.                 unsigned int new_file_starting_char;
  43.                 linked_list_element * next_element;
  44.                 friend class linked_list;
  45.         };
  46.         linked_list_element * first_element_ptr;
  47.     public:
  48.         linked_list ( ) { first_element_ptr = NULL; }
  49.         ~linked_list ( );
  50.         void add_element ( std::string & p_original_file,
  51.               unsigned int & p_original_file_line_number,
  52.               unsigned int & p_original_file_starting_char,
  53.               std::string & p_new_file,
  54.               unsigned int & p_new_file_line_number,
  55.               unsigned int & p_new_file_starting_char );
  56.         unsigned int delete_element ( unsigned int & p_element_number );
  57.         unsigned int print_element ( unsigned int & p_element_number );
  58. } references;
  59.  
  60. linked_list::~linked_list ( )
  61. {
  62.     linked_list_element * current_ptr;
  63.     linked_list_element * after_ptr;
  64.    
  65.     current_ptr = first_element_ptr;
  66.     while ( current_ptr != NULL )
  67.     {
  68.         after_ptr = current_ptr->next_element;
  69.         delete current_ptr;
  70.         current_ptr = after_ptr;
  71.     }
  72. }
  73. void linked_list::add_element ( std::string & p_original_file,
  74.               unsigned int & p_original_file_line_number,
  75.               unsigned int & p_original_file_starting_char,
  76.               std::string & p_new_file,
  77.               unsigned int & p_new_file_line_number,
  78.               unsigned int & p_new_file_starting_char )
  79. {
  80.     linked_list_element * before_ptr;
  81.     linked_list_element * new_element_ptr;
  82.     if ( first_element_ptr == NULL )
  83.     {
  84.        first_element_ptr = new linked_list_element;
  85.        first_element_ptr->element_number = 1;
  86.        first_element_ptr->original_file = p_original_file;
  87.        first_element_ptr->original_file_line_number = p_original_file_line_number;
  88.        first_element_ptr->original_file_starting_char = p_original_file_starting_char;
  89.        first_element_ptr->new_file = p_new_file;
  90.        first_element_ptr->new_file_line_number = p_new_file_line_number;
  91.        first_element_ptr->new_file_starting_char = p_new_file_starting_char;
  92.        first_element_ptr->next_element = NULL;
  93.    }
  94.    else
  95.    {
  96.        before_ptr = first_element_ptr;
  97.        while ( true )
  98.        {
  99.            if ( before_ptr == NULL || before_ptr->next_element == NULL )
  100.                break;
  101.            else
  102.                before_ptr = before_ptr->next_element;
  103.        }
  104.        before_ptr->next_element = new linked_list_element;
  105.        new_element_ptr = before_ptr->next_element;
  106.        new_element_ptr->element_number = ++ before_ptr->element_number;
  107.        new_element_ptr->original_file = p_original_file;
  108.        new_element_ptr->original_file_line_number = p_original_file_line_number;
  109.        new_element_ptr->original_file_starting_char = p_original_file_starting_char;
  110.        new_element_ptr->new_file = p_new_file;
  111.        new_element_ptr->new_file_line_number = p_new_file_line_number;
  112.        new_element_ptr->new_file_starting_char = p_new_file_starting_char;
  113.        new_element_ptr->next_element = NULL;
  114.    }
  115. }
  116. unsigned int linked_list::delete_element ( unsigned int & p_element_number )
  117. {
  118.     linked_list_element * to_delete_ptr;
  119.     linked_list_element * before_ptr;
  120.     if ( first_element_ptr == NULL )
  121.         return ( 0 );
  122.     else
  123.         to_delete_ptr = first_element_ptr;
  124.     while ( to_delete_ptr->next_element != NULL )
  125.     {
  126.         if ( to_delete_ptr->element_number != p_element_number )
  127.         {
  128.             before_ptr = to_delete_ptr;
  129.             to_delete_ptr = to_delete_ptr->next_element;
  130.         }
  131.     }
  132.     if ( to_delete_ptr == NULL )
  133.         return ( 0 );
  134.     before_ptr->next_element = to_delete_ptr->next_element;
  135.     delete to_delete_ptr;
  136.     return ( 1 );
  137. }
  138. unsigned int linked_list::print_element ( unsigned int & p_element_number )
  139. {
  140.     linked_list_element * to_print_ptr;
  141.     if ( first_element_ptr == NULL )
  142.     {
  143.         return ( 0 );
  144.     }
  145.     else
  146.         to_print_ptr = first_element_ptr;
  147.     while ( to_print_ptr != NULL )
  148.     {
  149.         if ( to_print_ptr->element_number != p_element_number )
  150.             to_print_ptr = to_print_ptr->next_element;
  151.         else
  152.             break;
  153.     }
  154.     if ( to_print_ptr == NULL )
  155.     {
  156.         return ( 0 );
  157.     }
  158.     std::cout << "Cross-reference number " << to_print_ptr->element_number << ":\n"
  159.               << "Original file: " << to_print_ptr->original_file << '\n'
  160.               << "Number of line in original file: " << to_print_ptr->original_file_line_number << '\n'
  161.               << "First character of reference on that line: " << to_print_ptr->original_file_starting_char << '\n'
  162.               << "New file: " << to_print_ptr->new_file << '\n'
  163.               << "Number of line in new file: " << to_print_ptr->new_file_line_number << '\n'
  164.               << "First character of reference on that line: " << to_print_ptr->new_file_starting_char << "\n\n";
  165.     return ( 1 );
  166. }
  167. void operation_add ( )
  168. {
  169.     std::string original_file;
  170.     unsigned int original_file_line_number;
  171.     unsigned int original_file_starting_char;
  172.     std::string new_file;
  173.     unsigned int new_file_line_number;
  174.     unsigned int new_file_starting_char;
  175.    
  176.     std::cout << "\tOriginal file: ";
  177.     std::getline ( std::cin, original_file );
  178.     std::cout << "\tOriginal file line number: ";
  179.     std::cin >> original_file_line_number;
  180.     std::cout << "\tStarting char on that line: ";
  181.     std::cin >> original_file_starting_char;
  182.     std::cin.ignore ( );
  183.     std::cout << "\tNew file: ";
  184.     std::getline ( std::cin, new_file );
  185.     std::cout << "\tNew file line number: ";
  186.     std::cin >> new_file_line_number;
  187.     std::cout << "\tStarting char on that line: ";
  188.     std::cin >> new_file_starting_char;
  189.     std::cin.ignore ( );
  190.     references.add_element ( original_file, original_file_line_number, original_file_starting_char,
  191.                              new_file, new_file_line_number, new_file_starting_char );
  192.     std::cout << "Reference added. \n";
  193. }
  194. void operation_printallrefs ( )
  195. {
  196.     unsigned int current_element_number ( 0 );
  197.     unsigned int print_status ( 0 );
  198.    
  199.     do
  200.     {
  201.         ++ current_element_number;
  202.         print_status = references.print_element ( current_element_number );
  203.     } while ( print_status != 0 );
  204. }
  205.  
  206. int main ( )
  207. {
  208.     std::string command;
  209.    
  210.     std::cout << "Cross-referencing program v1.0. \nReady for commands.\n";
  211.     while ( true )
  212.     {
  213.         std::cout << "> ";
  214.         std::getline ( std::cin, command );
  215.         if ( command == "add" )
  216.         {
  217.             operation_add ( );
  218.             continue;
  219.         }
  220.         else if ( command == "printallrefs" )
  221.         {
  222.             operation_printallrefs ( );
  223.             continue;
  224.         }
  225.         else if ( command == "quit" )
  226.             break;
  227.         else
  228.             std::cout << "Error: unknown command \"" << command << "\". Please enter a valid command. \n";
  229.     }
  230.     return ( 0 );
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement