1. 程式人生 > >順序表學生成績系統

順序表學生成績系統

#include<iostream> using namespace std; class Student { private: char *name[5]; int length; float grade[5]; public: Student(){length=0;} Student(char *n[],float g[],int l); ~Student(){} void insert(char *a,int b,float c);   //a=name,b=location,c=grade void Delete(int b); void locate(int b); void print(); };

Student::Student(char *n[],float g[],int l) {
for(int i=0;i<=3;i++) { name[i]=n[i]; grade[i]=g[i]; } length=l; }

void Student::insert(char *a,int b,float c) { if(length>=5) cout<<"不能再輸入資料"<<endl; else{ length--; for(int i=length;i>b;i--)
{ name[i-1]=name[i-2]; grade[i-1]=grade[i-2]; } name[b-1]=a; grade[b-1]=c; cout<<"資訊列入成功"<<endl; } }

void Student::Delete(int b) { if(length==0)cout<<"無資料"<<endl; else if(b<1||b>length)
cout<<"位置非法"<<endl; else { cout<<name[b-1]<<"資料已經刪除。"<<endl;} for(int i=b;i<length;i++) { name[i-1]=name[i]; grade[i-1]=grade[i]; } length--; }

void Student::locate(int b) { if(length==0)cout<<"無資料"<<endl; else if(b<1||b>length)cout<<"位置非法"<<endl; else {cout<<"第"<<b<<"位學生資料為"<<name[b-1]<<" "<<grade[b-1]<<endl<<endl;} }

void Student::print() { if(length==0)cout<<"無資料"<<endl; else { for(int i=0;i<length;i++) cout<<name[i]<<" "<<grade[i]<<endl; cout<<endl; } }

int main()  { cout<<"******************************"<<endl; cout<<"        學生成績系統"<<endl; cout<<"******************************"<<endl; char *n[2]={"ke","rou"}; float g[2]={78,87}; Student a(n,g,2); int y; float z; char x[5]; for(int i=0;i<3;i++) { cout<<"@@@輸入學生的名字:"; cin>>x; cout<<endl<<"@@@輸入學生的分數:"; cin>>z; cout<<endl<<"@@@輸入學生的學號:"; cin>>y; cout<<endl; } a.insert(x,y,z); cout<<endl<<"@@@輸入要刪除的學號:"; cin>>y; a.Delete(y); cout<<endl<<"@@@輸入要查詢的學生資訊的學號:"; cin>>y;  a.locate(y); cout<<endl<<endl<<endl<<"*****************顯示所有學生資訊:"<<endl; a.print(); return 0; }