برنامه اي كه نام ، شماره دنشجويي و معدل تعداد 15 دانشجو را از ورودي خوانده در آرايهاي از اشيا قرار مي دهد ، سپس اين موارد را مشخص مي كند : دانشجويي كه بيشترين معدل را دارد ، ليست كامل آرايه ، جست و جوي دانشجويي با شماره دنشجو ، و حذف دانشجو از آرايه .
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#define size 5
Int menu();
Int findslot();
Void search();
Void del();
Void findmax();
Void report();
Class student {
Char name[21];
Int stno;
Float ave;
Public:
Student() { name[0] = '\0' ; }
Void enter();
Char getstate();
Int getstno() { return stno ;}
Void print (int &) ;
Void seNul { name[0] = '\0' ;}
Float getAve { return aVe ; }
};
//*****************************
Student st[SIZE];
Char student::getstate()
{
Return name[0];
}
//*****************************
Void student::enter()
{
Clrscr();
Cout<<"Name "<<"stno"<<"Average"<<endl;
Cin>>name;
Gotoxy(22,2);
Cin>>stno;
Gotoxy(30,2);
Cin>>ave;
}
//******************************
Void student::print(int& row)
{
Gotoxy(1,row);
Cout<<name;
Gotoxy(22,row);
Cout<<stno;
Gotoxy(30,row);
Cout<<ave;
Row++;
}
//******************************
Int main()
{
Int I,c ;
For(;;){
Slrscr();
C = nenu();
Switch (c) {
Case 1 :
I = findslot();
If(i==-1) {
Cout<<"\n list is full . press a key …";
Getch();
}//end of if
Else
St[i].enter();
Break;
Case 2 :
Findmax();
Break;
Case 3:
Del();
Break;
Case 3 :
Del();
Break;
Case 4:
Search();
Break;
Case 5:
Report();
Break;
Case 6:
Exit(0);
}//end of swich
}//end of for(;;)
}
//******************************
Int menu()
{
Int c;
Gotoxy(5,5);
Cout<<"1. Enter a student.";
Gotoxy(5,6);
Cout<<"2. Find best student.";
Gotoxy(5,7);
Cout<<3. Delete a student.";
Gotoxy(5,8);
Cout<<"4. Search a student.";
Gotoxy(5,9);
Cout<<5. Report list.";
Gotoxy(5,10);
Cout<<"6. Exit.";
Do {
Gotoxy(5,12);
Cout<<"enter your select(1-6) :";
Cin>>c;
}while (c < 1 && c > 6);
Return c;
}//*************************************
Int findslot()
{
Int slot ;
For(slot = 0 ; slot < SIZE && si[slot].getstate(); slot ++);
If (slot == SIZE)
Return -1 ;
Return slot;
}//******************************
Void findmax()
{
Int I,p = 0 , row =2 ;
Float mAve =st[0].getAve();
For(I =1 ; I < SIZE ; i++)
If (st[i].getAve() > mAve){
mAve = st[i].getAve();
p= I;
}
Gotoxy (1,1);
Cout<<" name "<<"stno" <<" average"<<endl;
St[p].print(row);
Getch();
}//***********************************
Void del()
{
Int I,no;
Cout<<"\n Enter stno to delete : ";
Cin>>no;
For (I = 0 ; i< SIZE && st[i].getstno() !=no ; i++);
If(i==SIZE && !st[i].getstate()) {
Cout<<"student"<< no<< "not exist." <<"press a key to continue…:;
}
Else{
St[i].seNul();
Cout<<"student"<<no<<"deleted.";
} getch()
}//*******************************
Void search()
{
Int I, no ,row = 2;
Cout<<"\nEnter stno to search:";
Cin>>no;
For(I = 0 ; i<SIZE && st[i].getstno() !=no ; i++);
If ( i== SIZE) {
Cout << " student" << no << " not exist." <<"press a key to continue…";
}
Else{
Gotoxy (1,1);
Cout<< "Name "<<"stno" <<" Average"<<endl;
St[i].print(row);
}getch();
}//*****************************
Void report()
{
Int I, row = 2;
Clrscr();
Cout<< "Name "<<"stno"<< " Average"<<endl;
For(I = 0 ; I < SIZE ; i++) {
If(st[i].getstate())
St[i].print(row);
}getch();
}
عملكرد برنامه :پس از اجراي برنامه منويي با شش گزينه ظاهر مي شود..
گزينه اول : اطلاعات دانشجو را از صفحه كليد مي خواند.
گزينه دوم : دانشجويي با بيشترين معدل را پيدا مي كند.
گزينه سوم : دانشجويي را از ليست حذف مي كند.
گزينه چهارم : دانشجويي را در آرايه جست و جو مي كند.
گزينه پنجم : گزارش كاملي از آرايه تهيه مي كند.
گزينه ششم : به اجراي برنامه خاتمه مي دهد .