学生信息查询器

来源:互联网 发布:windows failed start 编辑:程序博客网 时间:2024/04/27 14:33
 

【实验题目:】

 

 

定义一个学生信息结构体: struct student{

Sno ;//学号

Sage;//年龄

Sname;//姓名

}

用快速排序和折半查找的方法,对输入任意学号,给出学社具体信息。

 


#include <iostream>
#include <string.h>
using namespace std;

 bool error=0;
 typedef long int SNO;
 typedef struct {
        SNO Sno;  //学号
        int Sage;  //年龄
        string Sname; //姓名
        }Student;
       
 SNO &isRightPutSno( );  //判断学号输入的合法性 并输入合法学号 
 int &isRightPutAge( );  //判断年龄输入的合法性 并输入合法年龄  
 void Input_Student_Info( Student *pST,int n ); //录入n个学生的信息
 Student* &Find_Student(  Student **pST,int n,SNO sno );  //查找指定学号的学生
 void  Type_Student_Info( Student ST );  //显示该学生的具体信息
 Student ** InsertSort( Student **p, int n ); //用直接插入法对索引排序
 bool isRepeat( Student *pST,int n,SNO sno ); //检查输入的学号是否重复
 
main()
{
     SNO sno;
     int n;
     cout<<"----------------------欢迎使用学生信息查询器---------------------------/n"<<endl;
     cout<<"   在使用本查询器之前,您需要先建立学生记录集,以便提供可查询的信息/n" <<endl;
     cout<<"请输入您想要建立的记录集个数n:( n>0 ):";
     cin>>n;
    
     Student *pST,stu[n];
    
     Input_Student_Info( stu,n ); //录入学生信息
    
     Student **pointer,**tpointer;    //索引指针
     pointer=new Student *[n+1]; //注意,分配的空间从下标为 0 开始
     for( int i=1; i<n+1; ++i ) //建立索引
        pointer[i]=&stu[i-1];
     
     tpointer=InsertSort( pointer, n );  //用直接插入法对索引排序
    
    
     while( 1 )
     {
            cout<<"-----------------------------------------------------------------"<<endl;
            cout<<"/n请输入您所要查询的学生的学号:";
            cin>>sno;

            pST=Find_Student( pointer,n,sno );
            if( error==0 )
            {
                cout<<"/n您所查询的学号为"<<sno<<"的学生具体信息如下:"<<endl;
                Type_Student_Info( *pST );
            }
            else
            {   error=0;
                pointer=tpointer;  //重置指针 ,使其复位
            }
           
            cout<<"/n继续查询请按:y  结束查询请按:n  ,是否继续查询(y/n)?: ";
            char ch;
            ch=cin.get();
            ch=cin.get();
            if( ch!='y' && ch!='Y' ) break;
     }
     system("pause");     
}
       
void Input_Student_Info( Student *pST,int n ) //录入n个学生的信息
{
     cout<<"/n----------------------开始录入"<<n<<"个学生的信息-----------------------------"<<endl;
     for( int i=0; i<n; ++i )
     {
          cout<<"/n********请输入第"<<i+1<<"人的信息:*********"<<endl;
         
          while( 1 )
          {
                 SNO temp;
                 temp=isRightPutSno( ); //暂存输入的学号

                 if( (isRepeat( pST,i,temp )) == false ) //判断是否重复
                     pST[i].Sno=temp;
                 else
                     {
                      cout<<"您输入了一个重复的学号,请重新输入!"<<endl;
                      continue;               
                     }

                 if( cin.fail()==1 )
                     {
                       cin.clear();
                       cout<<"您输入的学号不合法!请输入范围(0~2147483647)的数字。"<<endl;
                       continue;
                     }
                 break;
          }
         
          while( 1 )
          {
                 pST[i].Sage=isRightPutAge( );

                 if( cin.fail()==1 )
                     {
                       cin.clear();
                       cout<<"您输入的年龄不合法!请输入范围(1~150)的数字。"<<endl;
                       continue;
                     }
                 break;
          }
          
          cout<<"/n姓名:";
          cin>>pST[i].Sname;
     }

}

Student* &Find_Student(  Student **pST,int n,SNO sno )  //查找指定学号的学生
 {    
      int low, high, mid;
      low=1;   high=n;
      while( low<=high )
      {
             mid=(int )( low+high )/2;
             if( sno == pST[mid]->Sno )  return pST[mid];  
             else if( sno < pST[mid]->Sno )  high=mid-1;
             else low=mid+1;
      } 
     
      cout<<"对不起,没有您要查找的学生,请核对输入的学号是否有误!"<<endl;
      error=1;
      return pST[0];
 }
 
 void  Type_Student_Info( Student ST ) //显示该学生的具体信息
 {
     cout<<"/n学号:"<<ST.Sno<<endl;
     cout<<"/n年龄:"<<ST.Sage<<endl;
     cout<<"/n姓名:"<<ST.Sname<<endl;
 
 }

int &isRightPutAge(  )  //判断年龄输入的合法性 并输入合法年龄  
{
     int temp; 
     while( 1  )
     {
         cout<<"/n年龄:";
         cin>>temp; 

         if( temp>0 && temp<150  ) 
             return (temp);
         else
            { cout<<"您输入的年龄不合法!请输入范围(1~150)的数字。"<<endl; }      
      }

}


SNO &isRightPutSno( )//判断学号输入的合法性 并输入合法学号
{   
      SNO s;

      while( 1 )
      {
          cout<<"/n学号:";
          cin>>s;

          if( s>=0 && s<=4294967295  ) 
              {   return (s); }
          else 
              { cout<<"您输入的学号不合法!请输入范围(0~2147483647)的数字。"<<endl; }
      }
}


 Student ** InsertSort( Student **p, int n ) //用直接插入法对索引排序
 {
         int i,j;
         for( i=2; i<=n; ++i )
         {
              if( p[i]->Sno < p[i-1]->Sno )
              {
                  p[0]=p[i];
                  p[i]=p[i-1];
                  for( j=i-2; p[0]->Sno < p[j]->Sno ; --j ) 
                       p[j+1]=p[j];
                  p[j+1]=p[0];
              }    
         }
         return p;      
 }

bool isRepeat( Student *pST,int n,SNO sno ) //检查输入的学号是否重复
{
     if( n==0 ) return false;
     for( int i=0; i<n; ++i )
     {
          if( pST[i].Sno== sno )     return true;
     }   
     return false;
}