Professional Documents
Culture Documents
Amal Al-Abed 133056: Iostream String
Amal Al-Abed 133056: Iostream String
#include <iostream>
#include <string>
using namespace std;
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;
}
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() {
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;
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 (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--;
}
}
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() {
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;}