Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************
- * cross_reference - a cross-referencing *
- * program. It will allow *
- * you to store very *
- * precise links to information *
- * in other documents. *
- * *
- * Usage: to start, just type in *
- * <cross-reference> on the CMD *
- * Interactive commands are: *
- * add *
- * store a new reference *
- * remove *
- * remove a stored *
- * reference *
- * printref *
- * print one given *
- * reference *
- * printallrefs *
- * prints all refernces *
- * quit *
- * quit the program *
- * *
- * *************************************************************/
- #include <iostream>
- #include <string>
- #include <fstream>
- class linked_list
- {
- private:
- class linked_list_element
- {
- private:
- unsigned int element_number;
- std::string original_file;
- unsigned int original_file_line_number;
- unsigned int original_file_starting_char;
- std::string new_file;
- unsigned int new_file_line_number;
- unsigned int new_file_starting_char;
- linked_list_element * next_element;
- friend class linked_list;
- };
- linked_list_element * first_element_ptr;
- public:
- linked_list ( ) { first_element_ptr = NULL; }
- ~linked_list ( );
- void add_element ( std::string & p_original_file,
- unsigned int & p_original_file_line_number,
- unsigned int & p_original_file_starting_char,
- std::string & p_new_file,
- unsigned int & p_new_file_line_number,
- unsigned int & p_new_file_starting_char );
- unsigned int delete_element ( unsigned int & p_element_number );
- unsigned int print_element ( unsigned int & p_element_number );
- } references;
- linked_list::~linked_list ( )
- {
- linked_list_element * current_ptr;
- linked_list_element * after_ptr;
- current_ptr = first_element_ptr;
- while ( current_ptr != NULL )
- {
- after_ptr = current_ptr->next_element;
- delete current_ptr;
- current_ptr = after_ptr;
- }
- }
- void linked_list::add_element ( std::string & p_original_file,
- unsigned int & p_original_file_line_number,
- unsigned int & p_original_file_starting_char,
- std::string & p_new_file,
- unsigned int & p_new_file_line_number,
- unsigned int & p_new_file_starting_char )
- {
- linked_list_element * before_ptr;
- linked_list_element * new_element_ptr;
- if ( first_element_ptr == NULL )
- {
- first_element_ptr = new linked_list_element;
- first_element_ptr->element_number = 1;
- first_element_ptr->original_file = p_original_file;
- first_element_ptr->original_file_line_number = p_original_file_line_number;
- first_element_ptr->original_file_starting_char = p_original_file_starting_char;
- first_element_ptr->new_file = p_new_file;
- first_element_ptr->new_file_line_number = p_new_file_line_number;
- first_element_ptr->new_file_starting_char = p_new_file_starting_char;
- first_element_ptr->next_element = NULL;
- }
- else
- {
- before_ptr = first_element_ptr;
- while ( true )
- {
- if ( before_ptr == NULL || before_ptr->next_element == NULL )
- break;
- else
- before_ptr = before_ptr->next_element;
- }
- before_ptr->next_element = new linked_list_element;
- new_element_ptr = before_ptr->next_element;
- new_element_ptr->element_number = ++ before_ptr->element_number;
- new_element_ptr->original_file = p_original_file;
- new_element_ptr->original_file_line_number = p_original_file_line_number;
- new_element_ptr->original_file_starting_char = p_original_file_starting_char;
- new_element_ptr->new_file = p_new_file;
- new_element_ptr->new_file_line_number = p_new_file_line_number;
- new_element_ptr->new_file_starting_char = p_new_file_starting_char;
- new_element_ptr->next_element = NULL;
- }
- }
- unsigned int linked_list::delete_element ( unsigned int & p_element_number )
- {
- linked_list_element * to_delete_ptr;
- linked_list_element * before_ptr;
- if ( first_element_ptr == NULL )
- return ( 0 );
- else
- to_delete_ptr = first_element_ptr;
- while ( to_delete_ptr->next_element != NULL )
- {
- if ( to_delete_ptr->element_number != p_element_number )
- {
- before_ptr = to_delete_ptr;
- to_delete_ptr = to_delete_ptr->next_element;
- }
- }
- if ( to_delete_ptr == NULL )
- return ( 0 );
- before_ptr->next_element = to_delete_ptr->next_element;
- delete to_delete_ptr;
- return ( 1 );
- }
- unsigned int linked_list::print_element ( unsigned int & p_element_number )
- {
- linked_list_element * to_print_ptr;
- if ( first_element_ptr == NULL )
- {
- return ( 0 );
- }
- else
- to_print_ptr = first_element_ptr;
- while ( to_print_ptr != NULL )
- {
- if ( to_print_ptr->element_number != p_element_number )
- to_print_ptr = to_print_ptr->next_element;
- else
- break;
- }
- if ( to_print_ptr == NULL )
- {
- return ( 0 );
- }
- std::cout << "Cross-reference number " << to_print_ptr->element_number << ":\n"
- << "Original file: " << to_print_ptr->original_file << '\n'
- << "Number of line in original file: " << to_print_ptr->original_file_line_number << '\n'
- << "First character of reference on that line: " << to_print_ptr->original_file_starting_char << '\n'
- << "New file: " << to_print_ptr->new_file << '\n'
- << "Number of line in new file: " << to_print_ptr->new_file_line_number << '\n'
- << "First character of reference on that line: " << to_print_ptr->new_file_starting_char << "\n\n";
- return ( 1 );
- }
- void operation_add ( )
- {
- std::string original_file;
- unsigned int original_file_line_number;
- unsigned int original_file_starting_char;
- std::string new_file;
- unsigned int new_file_line_number;
- unsigned int new_file_starting_char;
- std::cout << "\tOriginal file: ";
- std::getline ( std::cin, original_file );
- std::cout << "\tOriginal file line number: ";
- std::cin >> original_file_line_number;
- std::cout << "\tStarting char on that line: ";
- std::cin >> original_file_starting_char;
- std::cin.ignore ( );
- std::cout << "\tNew file: ";
- std::getline ( std::cin, new_file );
- std::cout << "\tNew file line number: ";
- std::cin >> new_file_line_number;
- std::cout << "\tStarting char on that line: ";
- std::cin >> new_file_starting_char;
- std::cin.ignore ( );
- references.add_element ( original_file, original_file_line_number, original_file_starting_char,
- new_file, new_file_line_number, new_file_starting_char );
- std::cout << "Reference added. \n";
- }
- void operation_printallrefs ( )
- {
- unsigned int current_element_number ( 0 );
- unsigned int print_status ( 0 );
- do
- {
- ++ current_element_number;
- print_status = references.print_element ( current_element_number );
- } while ( print_status != 0 );
- }
- int main ( )
- {
- std::string command;
- std::cout << "Cross-referencing program v1.0. \nReady for commands.\n";
- while ( true )
- {
- std::cout << "> ";
- std::getline ( std::cin, command );
- if ( command == "add" )
- {
- operation_add ( );
- continue;
- }
- else if ( command == "printallrefs" )
- {
- operation_printallrefs ( );
- continue;
- }
- else if ( command == "quit" )
- break;
- else
- std::cout << "Error: unknown command \"" << command << "\". Please enter a valid command. \n";
- }
- return ( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement