Sei sulla pagina 1di 5

Roberto Garcia

ID: 10295008
main.cpp
#include "std.h"
using namespace std;

int main()
{
list listing;
student mystudent;
studentList stulist;
int Id;
string name,
universitY;
int findId;
int input;
while(1)
{
do
{
//options
cout <<
"///////////////////////////////////////////////////////////////////////\n";
cout << " Please select one of the following options:\n";
cout << "\n";
cout << " 1. Add the student's id, name, and university \n";
cout << " 2. Input the student's id to find his/her name and
university\n";
cout << " he/she is attending.\n";
cout << " 3. Exit.\n";
cout <<
"///////////////////////////////////////////////////////////////////////\n\n";
cout << " ->";

// input
cin >> input;

if(input == 1)
{
cout << endl;
cout << " Insert the student's name: ";
cin >> name;
cout << " Enter the ID: ";
cin >> Id;
cout << " Enter the name of the university the student is
attending: ";
cin >> universitY;
cout << endl;
cout << " Thanks! \n\n";
mystudent.name = name;
mystudent.id = Id;
mystudent.university = universitY;
listing.addStudentToList(mystudent);
}
else if(input == 2)
{
cout << endl;
cout << " Enter the ID of the student you want to find:";
cin >> findId;
cout << endl << endl;
listing.getStudent(findId);
}
else if(input == 3)
{
cout << " Bye!\n";
return 0;
}
else
{
cout << " That option does not exist.\n";
cout << " Try again.\n\n";

}while(input != 1 && input != 2 && input != 3);


}
}

Std.h

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class student
{
public:
int id; //student ID number
string name; //student’s name
string university; //student’ university
};
class studentList
{
private:
class node
{
public:
student data;
node * next;
};
node * head,* end,* current, * point;

public:
studentList()
{
head = NULL;
end = NULL;
current = NULL;
point = NULL;
}

~studentList()
{
delete head;
delete end;
delete current;
delete point;
}
void add(student s)
{
current = new node;
current->data = s;
current->next = NULL;

if(head == NULL)
{
head = current;
end = current;
}
else
{
end->next = current;
end = current;
}
}

void display(int id)


{

cout << "The student with the ID:" << id << " is:";
cout << endl;
for(point = head; point != NULL; (point = point->next) )
{
cout << "\t Name | identification | university" <<
endl;
cout << "\t ---------------------------------------------" <<
endl;
cout << "\t " << point->data.name << ". | " ;
cout << point->data.id << ". | ";
cout << point->data.university << "." << endl;
cout << "\t ---------------------------------------------" <<
endl;
}
cout << endl;
}
};

class list
{
private:
class node
{
public:
studentList alist;
int iden;
node * next;
};
node * head, *end, *current, *point;
public:
void addStudentToList(student s)
{
bool f = false;
for(point = head; point != NULL; point = point->next)
{
if(s.id == point->iden)
{
point->alist.add(s);
f = true;
break;
}
}
if(f == false)
{
current = new node;
current->iden = s.id;
current->next = NULL;
current->alist.add(s);
if(head == NULL)
{
head = current;
end = current;
}
else
{
end->next = current;
end = current;
}
}
}
void getStudent(int id )
{
bool f = false;
for(point = head; point != NULL; point = point->next)
{
if(id == point->iden)
{
point->alist.display(id);
f = true;
break;
}
}
if(f == false)
{
cout <<"--ERROR--\n";
cout << "I'm sorry, " << id << " does not exist";
cout << " in the system." << endl << " Try again";
cout << endl;
}
}
list()
{
head = NULL;
end = NULL;
current = NULL;
point = NULL;
}

~list()
{
delete head;
delete end;
delete current;
delete point;
}
};

Potrebbero piacerti anche