Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

AMAL AL-ABED 133056

#include <iostream>
#include <string>
using namespace std;

struct student { // this struct for student details


string name;
int id;
char grade;
};

class std_arr { //this array is array of type student struct


private:
student *st; // pointer to make dynamic array
int length;
int maxsize;

public:
std_arr(int size = 10) { // constructor
maxsize = size;
length = 0;
st = new student[maxsize];
}
~std_arr() { // destructor
delete st;
}
bool isempty() {
if (length == 0) return 1;
else return 0;
}
bool isfull() {
if (length == maxsize) return 1;
else return 0;
}

void insert(int i , string n , char g) { // insertion and sort , the


insertion will be (id , name , grade)

if (this->isempty()) { // if this is the first insertion


st[0].name = n;
st[0].id = i;
st[0].grade = g;
length++;
}
else if (this->isfull())cout << "array is full" << endl; // if the array
full
else { // not first insertion , and not full
for (int j = length-1; j >= 0; j--) { // will check the right place
to insert
if (i < st[j].id) {
st[j + 1].id = st[j].id;
st[j + 1].name = st[j].name;
st[j + 1].grade = st[j].grade;

if (j == 0) {
st[0].id = i;
st[0].name = n;
st[0].grade = g;
length++;
break;
}
}
else {
st[j + 1].id = i;
st[j + 1].name = n;
st[j + 1].grade = g;
length++;
break;
}
}
}

void delete_by_id(int i) {
int loc = this->Search(i); // find location
if (loc == -1)cout << "not exist ..." << endl;
else {
for (int j = loc; j < length; j++) { // delete by moving the array
elements
st[j].id = st[j + 1].id;
st[j].name = st[j + 1].name;
st[j].grade = st[j + 1].grade;
}
length--;
}
}
int Search(int gid) { //return location
for (int i = 0; i < length; i++) {
if (gid == st[i].id)return i;
}
return -1;

void retrieve(int i, string &n, char &c) { // to use retrive you should call
it like this (id , string (to save name in it) , char (to save grade in it))
int loc = this->Search(i);
if (loc == -1)cout << "not exist ..." << endl;
else {
n = st[loc].name;
c = st[loc].grade;
}
}

void print() {

for (int i = 0; i < length; i++) {


cout << "name is : " << st[i].name << endl;
cout << "id is : " << st[i].id << endl;
cout << "grade is : " << st[i].grade << endl;
cout << "............................................" << endl;
}
}
};

int main() {

std_arr a;
//step 1
cout << "step one ................................................." << endl;
cout << "enter 4 students details (id , name , grade)" << endl;
for (int i = 0; i < 4; i++) {
int id;
string name;
char grade;
cout << "enter id " << i+1 << " :";
cin >> id;
cout << "enter name " << i+1 << " :";
cin >> name;
cout << "enter grade " << i+1 << " :";
cin >> grade;
a.insert(id, name, grade);
}
// step 3
cout << endl << "step
three ................................................." << endl;
cout << "inter id to delete :";
int i;
cin >> i;
a.delete_by_id(i);
// step 5
cout << endl << "step five ................................................."
<< endl;
cout << "enter another 3 students details (id , name , grade) , one of them
must be your details" << endl;
for (int i = 3; i < 6; i++) {
int id;
string name;
char grade;
cout << "enter id " << i + 1 << " :";
cin >> id;
cout << "enter name " << i + 1 << " :";
cin >> name;
cout << "enter grade " << i + 1 << " :";
cin >> grade;
a.insert(id, name, grade);
}
//step 6
cout << endl << "step six ................................................."
<< endl;
cout << "delete a record that does not exist in the list" << endl;
cout << "enter id to delete :";
int c1;
cin >> c1;
a.delete_by_id(c1);
// step 8
cout << endl << "step
eight ................................................." << endl;
cout << "we will call the search fun , then retrieve fun ..." << endl;
cout << "enter id to search , must be in the list " << endl;
int c2;
cin >> c2;
string name1; }
#include <iostream>
#include <string>
using namespace std;

struct student { // this struct for student details


string name;
int id;
char grade;
};

class std_arr { //this array is array of type student struct


private:
student *st; // pointer to make dynamic array
int length;
int maxsize;

public:
std_arr(int size = 10) { // constructor
maxsize = size;
length = 0;
st = new student[maxsize];
}
~std_arr() { // destructor

delete st;
}
bool isempty() {
if (length == 0) return 1;
else return 0;
}
bool isfull() {
if (length == maxsize) return 1;
else return 0;
}

void insert(int i, string n, char g) { // insertion and sort , the insertion will
be (id , name , grade)

if (this->isempty()) { // if this is the first insertion


st[0].name = n;
st[0].id = i;
st[0].grade = g;
length++;
}
else if (this->isfull())cout << "array is full" << endl; // if the array
full
else { // not first insertion , and not full
for (int j = length - 1; j >= 0; j--) { // will check the right
place to insert
if (i < st[j].id) {
st[j + 1].id = st[j].id;
st[j + 1].name = st[j].name;
st[j + 1].grade = st[j].grade;

if (j == 0) {
st[0].id = i;
st[0].name = n;
st[0].grade = g;
length++;
break;
}
}
else {
st[j + 1].id = i;
st[j + 1].name = n;
st[j + 1].grade = g;
length++;
break;
}
}
}

void delete_by_id(int i) {
int loc = this->Search(i); // find location
if (loc == -1)cout << "not exist ..." << endl;
else {
for (int j = loc; j < length; j++) { // delete by moving the array
elements
st[j].id = st[j + 1].id;
st[j].name = st[j + 1].name;
st[j].grade = st[j + 1].grade;
}
length--;
}
}

int Search(int gid) { //return location


for (int i = 0; i < length; i++) {
if (gid == st[i].id)return i;
}
return -1;

void retrieve(int i, string &n, char &c) { // to use retrive you should call it
like this (id , string (to save name in it) , char (to save grade in it))
int loc = this->Search(i);
if (loc == -1)cout << "not exist ..." << endl;
else {
n = st[loc].name;
c = st[loc].grade;
}
}

void print() {

for (int i = 0; i < length; i++) {


cout << "name is : " << st[i].name << endl;
cout << "id is : " << st[i].id << endl;
cout << "grade is : " << st[i].grade << endl;
cout << "............................................" << endl;
}
}
};
int main() {

std_arr a;
//step 1
cout << "step one ................................................." << endl;
cout << "enter 4 students details (id , name , grade)" << endl;
for (int i = 0; i < 4; i++) {
int id;
string name;
char grade;
cout << "enter id " << i + 1 << " :";
cin >> id;
cout << "enter name " << i + 1 << " :";
cin >> name;
cout << "enter grade " << i + 1 << " :";
cin >> grade;
a.insert(id, name, grade);
}
// step 3
cout << endl << "step three ................................................." <<
endl;
cout << "inter id to delete :";
int i;
cin >> i;
a.delete_by_id(i);
// step 5
cout << endl << "step five ................................................." <<
endl;
cout << "enter another 3 students details (id , name , grade) , one of them must
be your details" << endl;
for (int i = 3; i < 6; i++) {
int id;
string name;
char grade;
cout << "enter id " << i + 1 << " :";
cin >> id;
cout << "enter name " << i + 1 << " :";
cin >> name;
cout << "enter grade " << i + 1 << " :";
cin >> grade;
a.insert(id, name, grade);
}
//step 6
cout << endl << "step six ................................................." <<
endl;
cout << "delete a record that does not exist in the list" << endl;
cout << "enter id to delete :";
int c1;
cin >> c1;
a.delete_by_id(c1);
// step 8
cout << endl << "step eight ................................................." <<
endl;
cout << "we will call the search fun , then retrieve fun ..." << endl;
cout << "enter id to search , must be in the list " << endl;
int c2;
cin >> c2;
string name1;}

You might also like