/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class student
{
public:
long ID;
string Name;
string Address;
float grades[3];
student *below;
public:
student(long id, string name, string address) {
this->ID = id;
this->Name = name;
this->Address = address;
}
~student() {
cout << "student destructor is called\n";
}
};
class Node {
public:
int ID;
int NoOfStudents;
int NoOfExams;
student *t;// a linked list of students is allocated dynamically
Node *Next;
public:
Node(int id) {
this->ID = id;
this->NoOfStudents = 0;
this->NoOfExams = 0;
this->t = nullptr;
this->Next = nullptr;
}
~Node() {
student* current = this->t;
student* next;
while (current != nullptr) {
next = current->below;
delete current;
current = next;
}
}
void add_student(student* s)
{
student *last = this->t;
if (this->t == nullptr)
{
this->t = s;
return;
}
while (last->below != nullptr)
last = last->below;
last->below = s;
}
void printList()
{
student* s = this->t;
while (s != nullptr)
{
cout << "Student ID: " << s->ID << " | Name:" << s->Name << " | Address: " << s->Address << endl;
s = s->below;
}
}
};
class school
{
public:
string Name;
Node *Head;
int n;//number of classes in school
public:
school(string name) {
this->Name = name;
this->n = 0;
}
~school() {
Node* current = this->Head;
Node* next;
while (current != nullptr) {
next = current->Next;
delete current;
current = next;
}
}
void add_class(Node* c)
{
this->n++;
Node *last = this->Head;
if (this->Head == nullptr)
{
this->Head = c;
return;
}
while (last->Next != nullptr)
last = last->Next;
last->Next = c;
}
};
Node* create_class() {
ifstream file("file.txt");
string line;
Node* node;
int node_id;
int i = 0;
if (file.is_open()) {
while (getline(file, line))
{
stringstream ss(line);
if (i == 0) {
ss >> node_id;
node = new Node(node_id);
} else if(i == 1) {
ss >> node->NoOfStudents;
} else {
long id;
string name;
string address;
ss >> id;
getline(ss, name, ',');
getline(ss, address);
student* s = new student(id, name, address);
s->below = nullptr;
node->add_student(s);
}
i++;
}
file.close ();
}
else
cout << "Unable to open file";
node->Next = nullptr;
return node;
}
int main ()
{
school s("School Name A");
Node* new_class = create_class();
s.add_class(new_class);
cout << "School Name: " << s.Name << endl;
cout << "School Classes Number: " << s.n << endl;
cout << "Class: " << new_class->ID << " has: " << new_class->NoOfStudents << " students" << endl;
cout << "List of students: " << endl;
new_class->printList();
return 0;
}
1
4
1666 Samir,Algeria
2675 Ahmed,Morocco
4355 Ali,Tunisia
5650 Mohamed,Egypt