📄 student.cpp
字号:
#include <iostream>
using namespace std;
#include "student.h"
int showMenu()//show menu and get choice
{
char ch;
do
{
cout << "====== menu ======" << endl;
cout << "1--append student" << endl;
cout << "2--remove student" << endl;
cout << "3--update student" << endl;
cout << "4--search student" << endl;
cout << "5--display student" << endl;
cout << "6--falled student" << endl;
cout << "0--exit" << endl;
cout << "==================" << endl;
cout << "please choose:";
cin >> ch;
}while( ch<'0' || ch>'6' );
return ch-'0';
}
int showSortMenu()//show menu and get choice
{
char ch;
do
{
cout << "====== menu ======" << endl;
cout << "1--order by ID" << endl;
cout << "2--order by name" << endl;
cout << "3--order by birthday" << endl;
cout << "4--order by score" << endl;
cout << "0--exit" << endl;
cout << "==================" << endl;
cout << "please choose:";
cin >> ch;
}while( ch<'0' || ch>'4' );
return ch-'0';
}
int showSearchMenu()//show menu and get choice
{
char ch;
do
{
cout << "====== menu ======" << endl;
cout << "1--search by ID" << endl;
cout << "2--search by name" << endl;
cout << "0--exit" << endl;
cout << "==================" << endl;
cout << "please choose:";
cin >> ch;
}while( ch<'0' || ch>'2' );
return ch-'0';
}
inline bool isLeap( int year )
{
return (year%4==0&&year%100!=0 || year%400==0);
}
bool isValid( const Date& cd )
{
int days[12]={
31,28,31,30,31,30,31,31,30,31,30,31
};
if( cd.month==2 && isLeap( cd.year ) )
days[1] = 29;
if( cd.month<1 || cd.month>12
|| cd.day<1 || cd.day>days[cd.month-1] )
return false;
return true;
}
bool input( Date&da )
{
Date d;
cin >> d.year >> d.month >> d.day;
if( !cin || !isValid( d ) )
return false;
da = d;
return true;
}
bool input( Student& st )
{
Student s;
cout << "input ID:";
cin >> s.id;
if( !cin )
return false;
cout << "input name:";
cin >> s.name;
cout << "input birthday:";
if( !input( s.birthday ) )
return false;
cout << "input score:";
cin >> s.score;
if( !cin || s.score<0 || s.score>100 )
return false;
st = s;
return true;
}
int findID( Student*p, int num, int id )
{
for( int i=0; i<num; i++ )
{
if( p++->id==id )
return i;
}
return -1;
}
void copyArray( Student*src, Student*dst, int num )
{
for( int i=0; i<num; i++ )
{
*dst++ = *src++;
}
}
bool append( Student*&p, int&num )
{
Student st;
if( !input( st ) )
{
cout << "invalid input!" << endl;
return false;
}
if( findID( p, num, st.id )!=-1 )
{
cout << "id " << st.id << " repeated!" << endl;
return false;
}
Student* pn = new Student[num+1];
copyArray( p, pn, num );
delete[] p;
p = pn;
p[num++] = st;
return true;
}
int getIDIndex( Student*p, int num )
{
int id;
cout << "input student ID:";
cin >> id;
if( id<1 )
{
cout << "invalid ID!" << endl;
return -1;
}
int index = findID( p, num, id );
if( index==-1 )
{
cout << "not found!" << endl;
return -1;
}
return index;
}
bool update( Student*&p, int&num )
{
int index = getIDIndex( p, num );
if( index==-1 )
return false;
Student st;
if( !input( st ) )
{
cout << "invalid input!" << endl;
return false;
}
int idx = findID( p, num, st.id );
if( idx!=index && idx!=-1 )
{
cout << "id " << st.id << " repeated!" << endl;
return false;
}
p[index] = st;
return true;
}
bool remove( Student*&p, int&num )
{
int index = getIDIndex( p, num );
if( index==-1 )
return false;
Student* pn = new Student[num-1];
copyArray( p, pn, index );
copyArray( p+1+index, pn+index, num-1-index );
delete[] p;
p = pn;
num--;
return true;
}
void display( const Date& cd )
{
cout << cd.year << '-' << cd.month << '-' << cd.day;
}
void display( const Student&cs )
{
cout << cs.id << '\t' << cs.name << '\t';
display( cs.birthday );
cout << '\t' << cs.score << endl;
}
bool display( Student*&p, int num )
{
for( int i=0; i<num; i++ )
display( p[i] );
return true;
}
bool search( Student*&p, int&num )
{
int choice = showSearchMenu();
if( choice==0 )
return true;
if( choice==1 )
{
int index = getIDIndex( p, num );
if( index==-1 )
return false;
display( p[index] );
}
if( choice==2 )
{
char name[20];
cout << "input student name:";
cin >> name;
int sn = 0;
for( int i=0; i<num; i++ )
{
if( strcmp( name, p[i].name )==0 )
{
display( p[i] );
sn++;
}
}
if( sn==0 )
{
cout << "not found!" << endl;
return false;
}
}
return true;
}
int compID( Student&s1, Student&s2 )
{
return s1.id-s2.id;
}
int compName( Student&s1, Student&s2 )
{
return strcmp(s1.name,s2.name);
}
int compDate( Date&d1, Date&d2 )
{
if( d1.year!=d2.year )
return d1.year-d2.year;
if( d1.month!=d2.month )
return d1.month-d2.month;
return d1.day-d2.day;
}
int compDate( Student&s1, Student&s2 )
{
return compDate( s1.birthday, s2.birthday );
}
int compScore( Student&s1, Student&s2 )
{
if( s1.score>s2.score )
return 1;
if( s1.score<s2.score )
return -1;
return 0;
}
void swap( Student&s1, Student&s2 )
{
Student s=s1;
s1 = s2;
s2 = s;
}
void sort( Student*p, int num, int(*comp)(Student&,Student&) )
{
for( int i=0; i<num; i++ )
{
for( int j=0; j<i; j++ )
{
if( comp(p[j],p[i])>0 )
{
swap( p[j], p[i] );
}
}
}
}
bool list( Student*&p, int&num )
{
int (*compf[4])(Student&,Student&)={
compID, compName, compDate, compScore
};
if( num==0 )
{
cout << "no record to display!" << endl;
return false;
}
int choice = showSortMenu();
if( choice==0 )
return false;
sort( p, num, compf[choice-1] );
display( p, num );
return true;
}
bool showFall( Student*&p, int&num )
{
int cnt=0;
for( int i=0; i<num; i++ )
{
if( p[i].score<60 )
{
display( p[i] );
cnt++;
}
}
if( cnt==0 )
{
cout << "not found!" << endl;
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -