Goal: Write a program that will make use of your knowledge of pointers to build
ID: 3738068 • Letter: G
Question
Goal: Write a program that will make use of your knowledge of pointers to build a sorted linked list. The program should include the following: 1. 2. Create a struct with the following items: name (string), score (int) and grade (char). Create a node and fill it by doing the following: a. read names from in.dat data file (attached) b. Generate a score for each node from 0 to 100. This field of the struct should be randomly generated by using the rand function. Assign a grade to each node as the following: 0-59: F; 60-69: D; 70-79: C; 80-89: B and 90-100: A. c. 3. Build a linked list of the nodes according to ascending order of score. 4. Print the listing of students/score/grade to the screen in row/column format. There should be proper t so that reader can tell what each column representing for Program Specifications The program you develop must have the following characteristics: It must create the class definition provided with this assignment . It must make used of linked lists based on the assignment specification . It must declare any additional necessary variables required in the program It must be commented adequately o Label the class members o Label each function call o Before each function, give a brief description of what the function will doExplanation / Answer
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
#include <time.h>
using namespace std;
struct List{
string name;
int score;
char grade;
struct List* next;
};
class linked_list{
private:
struct List *head;
public:
linked_list()
{
head = NULL;
}
void add_node(string name);
void display();
void sortFunction();
};
void linked_list::add_node(string filename){
srand(time(0));
ifstream in( filename.c_str());
if(!in) {
cout << "Cannot open input file. " << filename<<endl;
return ;
}
string strLine;
while(in) {
getline (in, strLine);
if(in) {
struct List *new_node = new List;
new_node->name = strLine;
new_node->next = NULL;
// get the score
new_node->score = rand() % 101;
char grade;
// get the grade
if(new_node->score >= 90 && new_node->score <= 100){
grade = 'A';
}
else if(new_node->score >= 80 && new_node->score <= 89){
grade = 'B';
}
else if(new_node->score >= 70 && new_node->score <= 79){
grade = 'C';
}
else if(new_node->score >= 60 && new_node->score <= 69){
grade = 'D';
}
else if(new_node->score >= 0 && new_node->score <= 59){
grade = 'F';
}
else{
grade = 'U'; //unknown
}
new_node->grade = grade;
struct List* current;
/* Special case for the head end */
if (head == NULL || head->score >= new_node->score)
{
new_node->next = head;
head = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = head;
while (current->next!=NULL && current->next->score < new_node->score)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
}
in.close();
}
void linked_list::display()
{
List *temp=new List;
temp=head;
while(temp!=NULL)
{
cout<<"name: "<< temp->name<<" "<<"score: " << temp->score<<" "<<"grade: "<< temp->grade<<" " <<endl;
temp=temp->next;
}
}
bool fexists(const char *name)
{
ifstream ifile(name);
return static_cast<bool>(ifile);
}
int main(int argc, char **argv){
linked_list filelist;
string fileName = "in.dat";
if (!fexists(fileName.c_str())){
cout << "file " << fileName << " does not Exist" <<endl;
exit(1);
}
filelist.add_node( fileName);
filelist.display();
}
// the in.dat contents are as below
Steve
Richard
Donald
Dave
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.