You are on page 1of 7

#ifndef KT_H_INCLUDED #define KT_H_INCLUDED

#include <iostream> #include <ctype.h> #include <sstream> using namespace std;

class InputException { public:

static bool CheckInputName(string ); static bool CheckInputSex( string ); static bool CheckInputDate( string ); static bool CheckInputPoint( string ); static bool CheckInputObject( string ); static bool CheckInputKV( string ); static bool CheckInputYear( string ); static bool CheckInputCMTNN( string ); static bool CheckInputKhoiThi( string ); static bool CheckInputDiemThuong( string ); static bool CheckInputHeSo ( string );

static bool LeapYear( int year); //Gregory };

bool InputException::CheckInputName(string inputName) { if (inputName[inputName.length()-1] == ' ') return false; if ( inputName[0] < 65 || inputName[0] > 90 ) return false; int i=1, n = inputName.length()-2; while (i < n) { if ( inputName[i] == ' ' && (inputName[i+1] == ' ' || inputName[i+1] < 65 || inputName[i+1] > 90 ) ) return false; else if( inputName[i] != ' ' && inputName[i+1] != ' ' && (inputName[i+1] < 97 || inputName[i+1] > 122 ) ) return false; i++; } return true; }

bool InputException::CheckInputSex( string inputSex) { if(inputSex != "0" && inputSex != "1" ) return false; return true; }

bool InputException::CheckInputDate( string inputDate) {

if ( inputDate.length() != 10 ) return false;

if (!( ((inputDate[0] == '0' || inputDate[0] == '1') && isdigit(inputDate[1])) || (inputDate[0] == '2' && inputDate[1] >= '0' && inputDate[1] <= '9') || (inputDate[0] == '3' && (inputDate[1] == '0' || inputDate[1] == '1') ) ) || !( (inputDate[3] == '0' && isdigit(inputDate[4]) ) || (inputDate[3] == '1' && inputDate[4]>='0' && inputDate[4] < '3') ) || !isdigit(inputDate[6]) || !isdigit(inputDate[7]) || !isdigit(inputDate[8]) || !isdigit(inputDate[9]) || inputDate[2] != '/' || inputDate[5] != '/' ) return false;

int year = (inputDate[6]-48)*1000 + (inputDate[7]-48)*100 + (inputDate[8]-48)*10 + inputDate[9] - 48;

if ( !LeapYear(year) && inputDate[0] == '2' && inputDate[1] == '9' &&

inputDate[3] == '0' && inputDate[4] == '2' ) return false; return true; }

bool InputException::CheckInputPoint( string inputPoint ) { if ( inputPoint.length() == 1 && isdigit(inputPoint[0]) ) return true; if ( inputPoint.length() == 2 && inputPoint == "10" ) return true; if ( inputPoint.length() == 3 && isdigit(inputPoint[0]) && inputPoint[1] == '.' && inputPoint[2] == '5' ) return true; if ( inputPoint.length() == 4 && isdigit(inputPoint[0]) && inputPoint[1] == '.' && (((inputPoint[2] == '0' || inputPoint[2] == '5') && inputPoint[3] == '0') || ((inputPoint[2] == '2' || inputPoint[2] == '7') && inputPoint[3] == '5')) ) return true; return false; }

bool InputException::CheckInputObject( string inputObject) { if (inputObject[0]=='0' && inputObject[1]>47 && inputObject[1]<56 )return true; return false; }

bool InputException::CheckInputKV( string inputKV ) { if ( inputKV.length()==1 && inputKV[0]>48 && inputKV[0]<52 ) return true; if ( inputKV=="2NT" ) return true; return false; }

bool InputException::CheckInputYear( string iYear ) { if ( iYear.length() != 4 ) return false; if ( !isdigit(iYear[0]) || !isdigit(iYear[1]) || !isdigit(iYear[2]) || !isdigit(iYear[3]) ) return false; return true; }

bool InputException::CheckInputCMTNN( string iCMT ) { for ( int i=0; i<iCMT.length(); i++) { if ( !isdigit(iCMT[i]) ) return false; } return true; }

bool InputException::CheckInputKhoiThi( string iKhoi )

{ if ( iKhoi.length()==1 && isalpha(iKhoi[0]) ) return true; return false; }

bool InputException::CheckInputHeSo ( string iHeSo ) { if ( iHeSo == "20" ) return true; if ( iHeSo.length() == 1 && ( isdigit (iHeSo[0]) ) ) return true; if ( iHeSo.length() == 2 && isdigit( iHeSo[1] ) && ( iHeSo[0] == '0' || iHeSo[0] == '1' ) ) return true; if ( iHeSo.length() == 3 && isdigit( iHeSo[0] ) && iHeSo[1] == '.' && isdigit( iHeSo[2] ) ) return true; if ( iHeSo.length() == 4 && iHeSo[2] == '.' && ( iHeSo[0] == '0' || iHeSo[0] == '1') && ( iHeSo[3] == '0' || iHeSo[3] == '5' ) ) return true; if ( iHeSo.length() == 4 && iHeSo[1] == '.' && isdigit( iHeSo[0] ) && ( iHeSo[3] == '0' && ( iHeSo[2] == '0' || iHeSo[2] == '5' ) ) && ( iHeSo[3] == '5' && ( iHeSo[2] == '2' || iHeSo[2] == '7' ) ) ) return true; return false; }

bool InputException::CheckInputDiemThuong( string _iDiemThuong ) { /*if ( iDiemThuong.length() == 1 && iDiemThuong[0] > 47 && iDiemThuong[0] < 51 ) return true;

if ( iDiemThuong.length() == 3 && iDiemThuong[0] > 47 && iDiemThuong[0] < 51 && iDiemThuong[1] == '.' && ( iDiemThuong[2] == '0' || iDiemThuong[2] == '5' ) ) return true; if ( iDiemThuong.length() == 4 && iDiemThuong[0] > 47 && iDiemThuong[0] < 51 && iDiemThuong[1] == '.' && (( ( iDiemThuong[2] == '2' || iDiemThuong[2] == '7' ) && iDiemThuong[3] == '5') || ( iDiemThuong[2] == '0' && iDiemThuong[3] == '0')) ) return true; return false;*/ return true; }

bool InputException::LeapYear( int year) //Gregory { if ( year % 4 != 0 ) return false; if ( year % 100 == 0 && year % 400 != 0 ) return false; return true; }

#endif

You might also like