#include<iostream.h>
#include
<stdlib.h>
const int max=5;
class stu
{
private:
    
int length;
    
int result[max];
public:
    stu (
int length=0){}
    ~stu(){}
    void input();
//輸入函數
    void output()
const;//輸出函數
    char 
* insert();//插入函數
    char 
* remove();//刪除函數
    void search()
const;//順序查找
    void sort();
//排序函數
};

void stu::input()
{
    cout
<<"開始建立順序表,請輸入學生成績表中學生的人數:";
    cin
>>length;
    cout
<<endl;
    
    
if(length<=0||length>max)
    {
        cerr
<<"學生成績表的長度范圍輸入錯誤!"<<endl;
        
exit(1);
    }
    
    
    
for(int i=0;i<length;i++)
    {
        cout
<<"請輸入第"<<i+1<<"學生的成績:";
        cin
>>result[i];
    }
}

void stu::output()
const
{
    
if(length<=0)
    {
        cerr
<<"學生成績表為空!"<<endl;
        
exit(1);
    }
    
for(int i=0;i<length;i++)
    {
        cout
<<""<<i+1<<"個學生的成績是:"<<result[i]<<endl;
    }
}

char 
*stu::insert()
{
    
if(length==max)
    {
        cerr
<<"學生成績表滿,不能插入!";
        
exit(1);
    }
    
    
int i=0;//要插入學生成績的位置
    cout
<<"請輸入要插入學生成績的位置:";
    cin
>>i;
    cout
<<endl;
    cout
<<endl;
    
if(i<0||i>length)
    {
        cerr
<<"插入位置不合理!"<<endl;
        
exit(1);
    }
    
    cout
<<"請輸入要插入學生的成績:";
    
int temp=0;//要插入學生的成績
    cin
>>temp;
    cout
<<endl;
    
for(int j=length-1;j>=i;j--)
    {
        result[j
+1]=result[j];
    }
    result[i]
=temp;
    length
++;
    return 
"插入成功\n";
    

}

char 
*stu::remove()
{
    
if(length==0)
    {
        cerr
<<"學生成績表為空,不能刪除!";
        
exit(1);
    }
    
    
int i=0;//要刪除學生成績的位置
    cout
<<"請輸入要刪除學生成績的位置:";
    cin
>>i;
    cout
<<endl;
    
if(i<0||i>length-1)
    {
        cerr
<<"不存在要刪除學生的位置!";
        
exit(1);
    }

    
//int temp=result[i-1];//將要被刪除的學生成績存入一個臨時變量
    
for(int j=i;j<length;j++)
        result[j]
=result[j+1];
    length
--;
    return 
"刪除成功\n";
    
}

void stu::search()
const
{
    
if(length==0)
    {
        cerr
<<"學生成績表為空!";
        
exit(1);
    }
    
int temp=0;
    cout
<<"請輸入要查詢學生成績的成績:";
    cin
>>temp;
    cout
<<endl;
    
for(int i=0;i<length;i++)
        
if(result[i]==temp) 
        {
            cout
<<"查找成功!您查找的成績在學生成績表的第"<<i<<"個位置"<<endl;
            break;
        }
    cout
<<"查找失敗!"<<endl;
}

void stu::sort()
{
    
int temp=0;
    
for(int i=0;i<length-1;i++)
    {
        
int k=i;
        
for(int j=i+1;j<length;j++)
            
if(result[k]>=result[j]) k=j;
        
if(k!=i)
        {
            temp
=result[k];
            result[k]
=result[i];
            result[i]
=temp;
        }
    }
    cout
<<"排序完成"<<endl;
    
for(i=0;i<length;i++)
        cout
<<result[i]<<endl;
}


int menu()
{
    cout
<<"************************* 學生成績管理系統(線性表) ****************************"<<endl;
    cout
<<"*                                                                             *"<<endl;
    cout
<<"*                                                                             *"<<endl;
    cout
<<"*       1. 輸入學生的成績                                                     *"<<endl;
    cout
<<"*       2. 插入學生的成績                                                     *"<<endl;
    cout
<<"*       3. 刪除指定學生的成績                                                 *"<<endl;
    cout
<<"*       4. 查找指定學生的成績                                                 *"<<endl;
    cout
<<"*       5. 對學生的成績排序                                                   *"<<endl;
    cout
<<"*       6. 輸出學生的成績                                                     *"<<endl;
    cout
<<"*       7. 退出                                                               *"<<endl;
    cout
<<"*                                                                             *"<<endl;
    cout
<<"*******************************************************************************"<<endl;
    
int choice=0;
    cout
<<"請選擇動作:";
    cin
>>choice;
    cout
<<endl;
    return choice;
}

void main()
{
    stu student;
    bool 
exit=false;
    
int choice=menu();
    
while(true)
    {
        switch(choice)
        {
        
case 1:student.input();break;
        
case 2:student.insert();break;
        
case 3:student.remove();break;
        
case 4:student.search();break;
        
case 5:student.sort();break;
        
case 6:student.output();break;
        
case 7:exit=true;break;
        }
        
if(exit==true)
            break;
        cout
<<endl;
        cout
<<"請繼續選擇動作:";
        cin
>>choice;
        cout
<<endl;
            
    }
}