Question
Name list management (Linked lists practice) : Hello I need help with this assignment. I worked most of it but I'm stuck. My code is
Name list management (Linked lists practice) :
Hello I need help with this assignment. I worked most of it but I'm stuck. My code is near the bottom and with what I need help with. Thanksss. C++.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is my code so far:
#include
using namespace std;
typedef struct node_type { long int id; string fname; string lname; string s; string dob; string sort; float gpa; struct node_type *next; }node;
void addRecord(node* &head, string line)
{ node *new_node = new node; string fn, ln, dob; string id; string gpa; stringstream ss; int i = 0; cout dob = dob; new_node->fname = fn; new_node->lname = ln; new_node->next = NULL; ss >> new_node->id; ss >> new_node->gpa; ss.clear(); if (head == NULL) { head = new_node;
} else { node *cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = new_node; } }
void swap(node *first, node *second) { node *temp = new node; temp->dob = first->dob; temp->fname = first->fname; temp->gpa = first->gpa; temp->id = first->id; temp->lname = first->lname; first->dob = second->dob; first->fname = second->fname; first->lname = second->lname; first->id = second->id; first->gpa = second->gpa; second->dob = temp->dob; second->fname = temp->fname; second->lname = temp->lname; second->id = temp->id; second->gpa = temp->gpa; }
void sortByFirstName(node *head) { node *cur1 = head; node *cur2 = head;
while (cur1 != NULL) { cur2 = cur1->next; while (cur2 != NULL) { if (cur1->fname > cur2->fname) { swap(cur1, cur2); } else if (cur1->fname == cur2->fname) { if (cur1->id > cur2->id) { swap(cur1, cur2); } } cur2 = cur2->next; } cur1 = cur1->next; } } void sortByLastName(node *head) { node *cur1 = head; node *cur2 = head;
while (cur1 != NULL) { cur2 = cur1->next; while (cur2 != NULL) { if (cur1->lname > cur2->lname) { swap(cur1, cur2); } else if (cur1->lname == cur2->lname) { if (cur1->id > cur2->id) { swap(cur1, cur2); } } cur2 = cur2->next; } cur1 = cur1->next; } } void sortByGpa(node *head) { node *cur1 = head; node *cur2 = head;
while (cur1 != NULL) { cur2 = cur1->next; while (cur2 != NULL) { if (cur1->gpa > cur2->gpa) { swap(cur1, cur2); } else if (cur1->gpa == cur2->gpa) { if (cur1->id > cur2->id) { swap(cur1, cur2); } } cur2 = cur2->next; } cur1 = cur1->next; } }
void sortByDOB(node *head) { node *cur1 = head; node *cur2 = head;
while (cur1 != NULL) { cur2 = cur1->next; while (cur2 != NULL) { if (cur1->dob > cur2->dob) { swap(cur1, cur2); } else if (cur1->dob == cur2->dob) { if (cur1->id > cur2->id) { swap(cur1, cur2); } } cur2 = cur2->next; } cur1 = cur1->next; } }
void sortByID(node *head) { node *cur1 = head; node *cur2 = head;
while (cur1 != NULL) { cur2 = cur1->next; while (cur2 != NULL) { if (cur1->id > cur2->id) { swap(cur1, cur2); } cur2 = cur2->next; } cur1 = cur1->next; }
}
void write(string file, node *head) { ofstream ofs; ofs.open(file.c_str());
if (ofs.is_open()) { node *cur = head; node *cur1 = head; cur1 = cur->next; while (cur != NULL && cur1 != NULL) { if (cur1->id != cur->id && cur->gpa == 4 || cur->gpa == 3 || cur->gpa == 2 || cur->gpa == 1 || cur->gpa == 0) { ofs id fname lname; ofs dob gpa next; cur1 = cur1->next; } else if (cur1->id != cur->id) //when ids are not the same. print. { ofs id fname lname; ofs dob gpa next; cur1 = cur1->next; } else //else ignore line { cur = cur->next; cur1 = cur1->next; } if (cur1 == NULL) //print last line { if (cur->gpa == 4 || cur->gpa == 3 || cur->gpa == 2 || cur->gpa == 1 || cur->gpa == 0) { ofs id fname lname; ofs dob gpa id fname lname; ofs dob gpa
} int main(int argc, char* argv[]) {
node *head = NULL; std::ifstream ifs("input.txt"); string input; string line;
if (ifs.is_open()) { while (!ifs.eof()) { getline(ifs, line); addRecord(head, line);
}
input = "output.txt"; sortByID(head); // calling to sort by id. write(input, head); //delete duplicates and output the list } else { cout
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
What I have so far is:
-Reading the list from the input file.
-Storing that list
-Sorting Functions (id, firstname, lastname, DOB, GPA)
-Deleting duplicates lines by comparing IDs and printing only 1 of them
-Outputing the list in its sorted form.
What I don't have and need is:
-Reading the sort.txt file
-Calling the sorting functions based off of the sort.txt
-reading the "delete id" line in the input.txt file.
-deleting the id that the delete line is calling for
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Example testing my code:
INPUT.TXT
{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0} {id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8} {id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0} {id:1234587,first:Katy,last:Green,DOB:1995-08-18,GPA:4.0} {id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0} {id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}
OUTPUT.TXT
{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0} {id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8} {id:1234587,first:Katy,last:Green,DOB:1995-08-18,GPA:4.0} {id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}
SORT.TXT
first id
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MY MAIN PROBLEM: EXAMPLE 2
INPUT.TXT
{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0} {id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8} {id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0} {id:1234587,first:Katy,last:Green,DOB:1995-08-18,GPA:4.0} delete 1234568 {id:1234570,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}
SORT.TXT
id DOB
OUTPUT.TXT
{id:1234587,first:Katy,last:Green,DOB:1995-08-18,GPA:4.0} {id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0} {id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0} {id:1234570,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}
I'm stomped at the delete id line. Don't know how to read the line and store the id without skrewing up my code.
If you can only help me out with this part that would be great. :)
If you can help me with what I need left that would be awesome. :D
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sorting Description:
There will be two input files. one called input.txt which containes the list of students and their information. The second is sort.txt which contains a list of sorting commands in their own line (id, first, last, DOB, GPA). The sort file will have one or more commands.
Example:
Input.txt
{id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0} {id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8} {id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0}
sort.txt
DOB first last id
This is how it should be output.
output.txt
{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0} {id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8} {id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}
*Notice the last line in the sort.txt file. That line should be how the list should be sorted and outputed in the end*
Description of output:
I also fix my header in my code. For some reason it got removed when posting this.
IF MORE INFORMATION IS NEEDED PLEASE COMMENT
COSC2430 Hw2: Name list management (Linked lists practice) 1. Introduction You will create a C++ program to manage a name list. Read a file that has a lot of items that stand for the information of a name list of certain class. Then sort the records by specific attributes to alphabetical ascending order, and output to a plain text file. 2. Input and Output a. Input file 1) The input file has multiple records (number >1). Your program should read the records one by one from the beginning of the file to the end Each record has uniform attributes (columns), the attributes appear in a fixed order, a record should contain all the keys, no empty values part will be given. But the input file may contain duplicated records. If two records have same id value, they can be recognized as duplicated records. You should always update record with the latter one. If somebody's name has more than one words, the separator would be underline _". Other than useful information in records, no other character or space will be given 2) 3) Each record takes one line (ends in n), and one line only contains oneStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started