C++ Please add as many comments to your code as possible. I want to make sure I
ID: 3746020 • Letter: C
Question
C++
Please add as many comments to your code as possible. I want to make sure I understand what is happening.
Read the entire document carefully. ONLY the CORRECT solution will receive a thumbs up.
Thank you,
Here is the assignment. Again, read carefully. Correct solution will be appreciated and given a thumbs up. Also, no need to rush. You have 2 days or so to finish this.
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 oneExplanation / Answer
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sys/stat.h>
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 << line << endl;
while (line[i] != ':') {
i++;
}
i++;
while (line[i] != ',') {
id.push_back(line[i]);
i++;
}
i++;
while (line[i] != ':') {
i++;
}
i++;
while (line[i] != ',') {
fn.push_back(line[i]);
i++;
}
i++;
while (line[i] != ':') {
i++;
}
i++;
while (line[i] != ',') {
ln.push_back(line[i]);
i++;
}
i++;
while (line[i] != ':') {
i++;
}
i++;
while (line[i] != ',') {
dob.push_back(line[i]);
i++;
}
i++;
while (line[i] != ':') {
i++;
}
i++;
while (line[i] != '}') {
gpa.push_back(line[i]);
i++;
}
ss << id;
ss << " ";
ss << gpa;
new_node->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:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;
ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << ".0}";
ofs << endl;
cur = cur->next;
cur1 = cur1->next;
}
else if (cur1->id != cur->id) //when ids are not the same. print.
{
ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;
ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << "}";
ofs << endl;
cur = cur->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:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;
ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << ".0}";
ofs << endl;
}
else
{
ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;
ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << "}";
ofs << endl;
}
}
}
}
}
int main(int argc, char* argv[])
{
node *head = NULL;
ifstream ifs,ifsSort;
string input;
string line;
// You can add code here to split file name from the command line
// For now, using static file names for input, sort and output
ifs.open("input11.txt", ios::in);
if (ifs.is_open() == false)
{
cout << " File Opening Error - Input File";
exit(0);
}
while (!ifs.eof())
{
getline(ifs, line);
addRecord(head, line);
}
//code to sort
ifsSort.open("sort11.txt", ios::in);
if (ifsSort.is_open() == false)
{
cout << " File Opening Error - Sort File";
exit(0);
}
// To get last line from sort text file
while (!ifsSort.eof())
{
getline(ifsSort, line);
}
int choice;
if (line == "id")
choice = 1;
else if (line == "first")
choice = 2;
else if (line == "last")
choice = 3;
else if (line == "DOB")
choice = 4;
else if (line == "GPA")
choice = 5;
//Note : In cpp switch can not take string as choice variable. we need to convert choice to integers first.
switch (choice) // run-time
{
case 1: // compile-time
sortByID(head);
break;
case 2: // compile-time
sortByFirstName(head);
break;
case 3: // compile-time
sortByLastName(head);
break;
case 4: // compile-time
sortByDOB(head);
break;
case 5: // compile-time
sortByGpa(head);
break;
default:
std::cout << "Invalid Sorting Criteria" << std::endl;
break;
};
input = "output11.txt";
write(input, head); //delete duplicates and output the list
//system("pause");
int x;
cin >> x;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.