I am writing a program that reads in a text file that has a name of something, and a weight associated with that name and and stores it in a linked list and sorts it based on the weight and by the name in it. That part is working but I also need to create a function that sorts thru the file and finds every word that starts with a prefix that is entered by the user. For example if the user enters "Am" the function will search the file and return every word that starts with "Am". Anyways I have never done anything like this before and so any help on how to do this would be greatly appreciated!
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <list>
usingnamespace std;
//declare node structure
struct node
{
//declare string text
string text;
//string name;
int number;
//declare next and prev node
struct node *next;
struct node *prev;
}*start; //start or head of linked list
//delcare class LinkedList
class Term
{
//public part
public:
//declare all fucntions
//this will create linked list
void CreateList(string file);
//insert a line at end of list
void InsertAtEnd(int num,string file, int size);
void print(string file_name);
void sort_weight ();
void sort_query();
//void SelectionSort();
//void swap(node *p1, node*p2);
//private
private:
//declare head and tail
node *head;
node *tail;
};
#include "Term.h"
void Term :: CreateList(string file)
{
//delclare node temp.
node *temp;
//initialize temp
temp = new(struct node);
//set text(which was declared in class) to file.
temp -> text = file;
temp -> next = NULL;
//If start of list equals NULL enter statement.
if (start == NULL)
{
temp -> prev = NULL;
//set beginning of list to temp.
start = temp;
}
//if not enter else statement.
else
{
//enter loop if the term after start is not NULL.
while (start -> next != NULL)
{
//set start equal to line proceeding it.
start = start -> next;
//set that line equal to temp.
start -> next = temp;
//set previous line equal to start.
temp -> prev = start;
}
}
//End function and return too main.
}
//Creat function that insert something at end of file taking parameters file(content of file), and size(number of lines).
//This function will enter lines into list but reorder them since orginally it insert backwards and finally it will insert new line.
void Term :: InsertAtEnd(int num, string file, int size)
{
//Declare nodes temp and temp_2.
struct node *temp, *temp_2;
//set temp_2 to beginning of list(start).
temp_2 = start;
//if you have not reached the end of the file enter loop and do not exit till you reach end of file.
for (int i = 0; i < size; i++)
{
//progress through loop.
temp_2 = temp_2 -> next;
}
//initialize temp.
temp = new(node);
temp -> text = file;
temp -> number = num;
//if line following q equals NULL enter
if (temp_2 -> next == NULL)
{
//progress q and set equal to tmp.
temp_2 -> next = temp;
//set line following tmp to NULL.
temp -> next = NULL;
//set line Proceeding tmp to temp_2.
temp -> prev = temp_2;
}
//if not enter if statement
else
{
//set line following tmp equal to line following temp_2.
temp -> next = temp_2 -> next;
temp -> next -> prev = temp;
temp_2 -> next = temp;
temp -> prev = temp_2;
}
}
void Term :: sort_weight ()
{
node * temphead = start;
//ListNode * tempnode = NULL;
int temproll;
string tempname;
int counter = 0;
while (temphead )
{
temphead = temphead->next;
counter++;
}
temphead = start -> next;
for (int j=0; j<counter; j++)
{
while (temphead->next) //iterate through list until next is null
{
if (temphead->number < temphead->next->number)
{
/*tempnode = temphead;
temphead = temphead->next;
temphead->next = tempnode;*/
temproll = temphead->number;
temphead->number = temphead->next->number;
temphead->next->number = temproll;
tempname = temphead->text;
temphead->text = temphead->next->text;
temphead->next->text = tempname;
temphead = temphead->next;//increment node
}
else
temphead = temphead->next;//increment node
}
temphead = start;//reset temphead
}
}
void Term :: sort_query ()
{
node * temphead = start;
//ListNode * tempnode = NULL;
string temp;
int temp_2;
int counter = 0;
while (temphead)
{
temphead = temphead->next;
counter++;
}
temphead = start -> next;
for (int j=0; j<counter; j++)
{
while (temphead->next) //iterate through list until next is null
{
if (temphead->text > temphead->next->text)
{
/*tempnode = temphead;
temphead = temphead->next;
temphead->next = tempnode;*/
temp = temphead->text;
temphead->text = temphead->next->text;
temphead->next->text = temp;
temp_2 = temphead->number;
temphead->number = temphead->next->number;
temphead->next->number = temp_2;
temphead = temphead->next;//increment node
}
else
temphead = temphead->next;//increment node
}
temphead = start;//reset temphead
}
}
//Declare void function(does not return anything) that will print the current linked list to the screen when requested
//or output it to the input file when requested. It takes parameters letter(letter entered when prompted in main function) and file_name
//(name of file).
void Term :: print(string file_name)
{
//declare and initialize line_number.
int line_number = 1;
//declare node temp_2
node *temp_2;
//set equal to start or beginning of list
temp_2 = start;
//if first line is equal to blank space enter statement
if( temp_2->text == "")
{
//proceed to followinf line skipping over this plan line
temp_2 = temp_2-> next;
}
//enter loop if temp_2 != null (have not reached end of file)
while (temp_2 != NULL && temp_2-> text != "")
{
//output line number and each line in the list till you reach the end
cout << temp_2 -> number << " " << temp_2 -> text << endl;
//progress to next line
temp_2 = temp_2 -> next;
//increment line number
line_number += 1;
}
}
int main()
{
Term txt_file;
string file;
int num;
string file_name;
ifstream infile;
int size = 0;
int largest_pos = 0;
//call function to create list
txt_file.CreateList(file);
//ask user for file to open and store in variable
cout << "What file would you like to open: ";
getline(cin, file_name);
infile.open(file_name);
//check if file name is valid
if(infile.fail())
{
//if not display error message and end program
cout <<"The file cannot be opened, terminating program!" << endl;
system("pause");
exit(0);
}
//use loop to read through file
while(infile >> num && (getline(infile, file)))
{
//call function to insert each line in file into linked list one following the other
txt_file.InsertAtEnd(num,file, size);
//increase size
size += 1;
//increase largest_pos
largest_pos += 1;
}
txt_file.sort_weight();
txt_file.print(file_name);
cout << endl << endl << endl;
txt_file.sort_query();
//call function to print current file to screen
txt_file.print(file_name);
system("pause");
return 0;
}
Here is a snippet from one of the text files:
1 2 3 4 5 6 7 8 9 10 11
4551 Pinnacle West Capital
7018 Automatic Data Proc.
8969 Continental Airlines
5725 PPL
1193 Goody's Family Clothing
1694 Community Health Sys.
7875 Cenex Harvest States
5723 Air Products & Chem.
2238 Consol. Freightways
31138 Goldman Sachs Group
2352 PNM Resources