البرنامج فيه مشكلة بسيطة مادري وش هي تفحصت الاقواس بس برضه مشكلة
وبالنسبة فيه شي باستفسر عنه هو اني سويت فنكشن find بحيث ابي لما ادخل مستوى معين للطالب
يطبع جميع الطلاب في نفس المستوى مادري فنكشن حقتي صح والالا؟؟
وكيف اقدر ادمج قائمتين بدون ماثر على القائمة الاصلية وابي اطبعها بشكل مرتب
//READ STUDENTS FROM A FILE AND BUILD A LINKED LIST
//
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
#include<iostream.h>
#include<fstream.h>
#include<cstring>
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
class Student
{
public:
char name[150];
int id;
int level;
int phone;
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
Student() //constructor
{
for(int i = 0;i < 150;i++)
name='\0';
id=0;
level=0;
phone=0;
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
Student(const Student &s) //copy constructor
{
for(int i=0;i<150;i++)
name= s.name;
id = s.id;
level = s.level;
phone = s.phone;
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
class StudentList()
{
private:
Student student[50];
int act_num;
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
public:
StudentList() //constructor
{
act_num = 0;
ifstream infile;
Student currS;
infile.open("student.txt");
if(!infile)
cout<<"\aCan not open \"student.txt\" file!\n";
else
{
//reading the file and store the students in the array
infile.clear();
infile.seekg(0,ios::beg);
infile.getline(currS.name,150);
infile >> currS.id;
infile >> currS.level;
infile >> currS.phone;
infile.clear();
while(!infile.eof())
{
addStudent(&currS);
for(int i=0;i<150;i++)
currS.name='\0';
infile.seekg(2,ios::cur);
infile.ignore();
infile.getline(currS.name,150);
infile >> currS.id;
infile >> currS.level;
infile >> currS.phone;
}
cout << act_num << " STUDENTS has been LOADED..\n";
infile.close();
}
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
~StudentList() // destructor
{
ofstream ofile;
ofile.open("student.txt");
if(act_num!=0)///if there is no item in the list there...
//...is no need to write and the file will be empty...
//...because it open for writing
{
for (int i = 0;i < act_num-1;i++)
{
ofile << student.name<<'\n';
ofile << student.id<<'\n';
ofile << student.v<<'\n';
ofile << student.phone<<"\n\n";
}
ofile << student.name<<'\n';
ofile << student.id<<'\n';
ofile << student.level<<'\n';
ofile << student.phone<<'\n';
ofile.close();
cout << "Information SAVEd successfully\n";
}
else
cout << "The list is empty ,SO the file is empty\n";
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
bool addStudent(Student *s) // adds a student in SORTED order (by name)
{
if(act_num == 20)//<<----if the array is full;
return false;
else
{
int i=0;
while((strcmp(s->name,student.name)==1) && (i < act_num))
i++;// find the suitable position to keep the array sorted
if(i <= act_num)//<<---shift the element
for(int j = act_num;j > i;j--)
student = student[j-1];
student = *s;//<<----insert the new object
act_num++;//<<----if it inserted,increment the actual number.
}
return true;
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
Student *findStudent(char *input_level) // finds a student by level
{
int i = 0;
if(act_num == 0)
cout<<"\aThe array is empty:\n";
else
{
while(strcmp(input_level,student.level)!=0 && i <= act_num)
i++;
if(i > act_num)
cout<<"There is no Student with this level!!\n";
else
print(&student);
}
return &student;
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
bool deleteStudent(char *input_name) // deletes the first student matching that name
{
if(act_num == 0)//<<----if there is no one to delete them?
return false;
int i=0;
while((strcmp(input_name,student.name)!=0) &&(i <= act_num))
i++;//<<<----find the object
if (i > act_num)
return false;
for(int j = i;j < act_num;j++)//<<---shifting the elemnt
student[j] = student[j+1];
act_num--;//<<----if delete it decrement the actual number.
return true;
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
void print(Student *s)
{
cout << "\n";
cout << " NAME\t\t\t|" << s->name<<endl;
cout << " ID\t|" << s->id << endl;
cout << " LEVEL\t\t|" << s->level << endl;
cout << " PHONE NUMBER\t|"<< s->phone<< endl;
cout << ".\t.\t.\t.\t.\t.\t.\n";
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
void printOrder() // prints all student's information in order
{
if(act_num == 0)
cout<<"The List Is Empty\n";
else
for(int i=0;i<act_num;i++)
{
cout<<" #:"<<i+1;
print(&student);
}
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
void read(Student *s)
{
cout << "Enter STUDENT information:\n";
cin.ignore();
cout << "Name\t\t\t:";
cin.getline(s->name,100,'\n');
cout << "The unique ID\t\t:";
cin >> s->id;
cout << "Level\t\t:";
cin >> s->level;
cout << "Phone Number\t:";
cin >> s->phone;
}
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
/*^M^A^I^N^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ /
/*^ ^ ^ ^ ^ ^ ^ ^ ^ ^^F^U^N^C^T^I^O^N ^ ^ ^ ^ ^/
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
int main()
{
StudentList SL;
char ch,ch2,ch3,ch4;
Student s;
do
{
cout << "\a";
cout << "\n\t\tMAIN menu|\n\n";
cout << "\t1.Add STUDENT.\n";
cout << "\t2.Delete STUDENT.\n";
cout << "\t3.Find STUDENT.\n";
cout << "\t4.Display.\n";
cout << "\t5.Exit.\n";
cout <<"\t?";
cin>>ch;
switch (ch)
{
case '1':
read(&s);
if(SL.addStudent(&s))
cout<<"\n\twill be Added...\n";
else
cout<<"\n\tThe List is full!\n";
break;
case '2':
cout<<"Enter the Student name:";
cin.ignore();
cin.getline(s.name,100);
if(!SL.deleteStudent(s.name))
cout<<"The array is empty |Or| not found the Student\n";
else
cout<<"The Student will be daleted:\n";
break;
case '3':
cout<<"Enter the Level:";
cin.ignore();
cin.getline(s.level);//,100);
SL.findStudent(s.level);
break;
case '4':
do
{
cout << "\n\t\tPRINT menu|\n\n";
cout << "\t1.Print VEHICLEs in order.\n";
cout << "\t2.Print Student in given level.\n";
cout << "\t3.Return to MAIN menu.\n";
cout << "\t?";
cin >> ch3;
switch(ch3)
{
case '1':
SL.print();
break;
case '2':
SL.printOrder();
break;
}
}while(ch3!='3');//PRINT menu
break;
}
}while(ch!='5' );////MAIN menu
return 0;
}