This programming use basic struct - class in C/ C++ language .
Here code write object orientation , link list.....
1. Basic class: Date , Full Name , Password......................................................................
2. Struct array :sort , search , delete , display, add..............................................................
3. struct link list : sort , search , delete , display ,add .............................................................
4. Class link list: class , Node , List. add , delete.................................................................
5. Abstract : virtual function ................................................................................................
6. Template: Function-class..................................................................................................
1.Basic class: Date , Full Name , Password........................................................................
This header file is "ClassDate.h"
#ifndef Class_Date_H
#define Class_Date_H
/*
File : "ClassDate.h"
*Author :ebook Java how to programming
*Date update : 10/10/2012
*/
class ClassDate
{
private :
// 1-12
int m_Month;
// 1-31 based on month
int m_Day;
// any year
int m_Year;
public :
// contructor
ClassDate();
/*input day , month ,year */
bool setDate(int day , int montn ,int year);
//get day
int getDay();
//get month
int getMonth();
//get year
int getYear();
/*Display day , month , year*/
void show();
private :
/*check validate of month */
bool checkMonth(int testMonth);
/*Check validete of Day */
bool checkDay(int testDay);
};
#endif
This source file is "ClassDate.cpp"
/***************************************************** *File "ClassDate.cpp" *Date create : 10/10/2012 *****************************************************/ #include <iostream> #include "ClassDate.h" using namespace std; /*=================================================== * * public * *=====================================================*/ ClassDate::ClassDate()// constructor { m_Day = 0; m_Month = 0; m_Year = 0; }; /** ................................................. */ bool ClassDate::setDate(int day, int month ,int year) { bool check1 = true; bool check2 = true; check1 = checkDay(day); check2 = checkMonth(month); if((check1 ==true) &&(check2 ==true)) { m_Day = day; m_Month = month; m_Year = year; return true; } return false; }; /** get day */ int ClassDate::getDay() { return m_Day; }; /** get month */ int ClassDate::getMonth() { return m_Month; }; /** get year */ int ClassDate::getYear() { return m_Year; } /** ................................................. */ void ClassDate::show() { if((getDay()!= 0)&&(getMonth()!=0)) { cout<< "Date (DDMMYY):"<< m_Day <<"/"<<m_Month<<"/"<<m_Year<<endl; } else { cout<< "Date (DDMMYY):Date don't declare"<<endl; } } /*=================================================== * * private * *=====================================================*/ /** ................................................. */ bool ClassDate::checkMonth(int testMonth) { // validate month if (testMonth > 0 && testMonth <= 12) { return true; } else { // month is invalid return false ;//"month must be 1-12" } } /** ................................................. */ bool ClassDate::checkDay(int testDay) { // days in each month int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // check if day in range for month if (testDay > 0 && testDay <= daysPerMonth[m_Month]) { return true; } // check for leap year if (m_Month == 2 && testDay == 29 && (m_Year % 400 == 0 || (m_Year % 4 == 0 && m_Year % 100 != 0))) { return true; } else { return false;//day out-of-range for the specified month and year } }This header file is "ClassSex.h"
#ifndef CLASS_Sex_H
#define CLASS_Sex_H
class ClassSex
{
private :
int m_Sex;
public:
//........................
ClassSex();
//.......................
bool setSex(int sex);
//.......................
int getSex();
//.......................
void show();
private:
bool checkSex(int sex);
};
#endif
This source File is "ClassSex.cpp"
#include <iostream>
#include "ClassSex.h"
using namespace std;
/** Comments on C++ function
...
*/
ClassSex::ClassSex()
{
m_Sex = -1;
}
/** Comments on C++ function
...
*/
bool ClassSex::setSex(int sex)
{
if(checkSex(sex)==true)
{
m_Sex = sex;
return true;
}
return false;
}
/** Comments on C++ function
...
*/
int ClassSex::getSex()
{
return m_Sex;
}
/** Comments on C++ function
...
*/
void ClassSex::show()
{
switch(m_Sex)
{
case 0 :
cout <<"Sex : female."<<endl;
break;
case 1 :
cout <<"Sex : male."<<endl;
break;
default :
break;
}
}
/** Comments on C++ function
...
*/
bool ClassSex::checkSex(int sex)
{
if((sex ==0)||(sex ==1))
{
return true;
}
return false;
}
This is header file : "ClassName.h"#ifndef CLASS_Name_H
#define CLASS_Name_H
/**File : "ClassName.h"
*@author :TLT.
*Day create : 04 OCt 2012
*/
class ClassName
{
private :
char m_aFirst[10];
char m_aLast[20];//last name and middle name
public :
/** constructor */
ClassName();
/** input full name */
bool setName(char* fisrtName, char* lastName);
char* getFirst();
char* getLast();
/** display name*/
void show();
private :
/**validate of first name. */
bool checkFirstName(char *ptr);
/**validate of first name. */
bool checkLastName(char *ptr);
/* char* ptr copy to char* pt */
void copyString(char* ptr , char* pt);
};
#endif
Source file : "ClassName.cpp"
/*****************************************************
*File "ClassName.cpp"
*Date create : 10/10/2012
*****************************************************/
#include <iostream>
#include <string.h>
#include "ClassName.h"
using namespace std;
/** constructor */
ClassName::ClassName()
{
//char * strcpy ( char * destination, const char * source );
strcpy (m_aFirst," ");
strcpy (m_aLast," ");
}
/** input full name */
bool ClassName::setName(char* fisrtName, char* lastName)
{
if((checkFirstName(fisrtName) == true)
&&(checkLastName(lastName)== true))
{
strcpy (m_aFirst,fisrtName);
strcpy (m_aLast,lastName);
return true;
}
return false;
}
char* ClassName::getFirst()
{
return m_aFirst;
}
char* ClassName::getLast()
{
return m_aLast;
}
/** display name*/
void ClassName::show()
{
cout <<"Full Name : "<< m_aFirst <<" "<<m_aLast<<endl;
}
/**validate of first name. */
bool ClassName::checkFirstName(char *ptr)
{
while(*ptr != 0)
{
if((*ptr < 97)|| (*ptr >122))
{
return false;
}
ptr ++;
}
return true;
}
/**validate of first name.*/
bool ClassName::checkLastName(char *ptr)
{
while(*ptr !=0)
{
if(((*ptr < 97)&&(*ptr != 32))
|| ((*ptr >122)&&(*ptr != 32)))
{
return false;
}
ptr ++;
}
return true;
}
Password requirements:+Password must be at least 7 characters long
+Contain one of each of the following
-uppercase letter
-lowercase letter
-special character or number
- not contain space
This is header file : "ClassPassword.h"
#ifndef Class_Password
_H #define Class_Password
_H /** * File :"ClassPassword.h" * @author TLT */ class ClassPassword { private : char m_aPass[20];//Password of user. public: /** *constructor of the pass. */ ClassPassword(); /** * set password of user *@param : char arrays */ bool setPassword(char* password); /** * get password * return char array */ char* getPassword(); /** * shown a String of the pass. */ void show(); private : bool checkNumber(char* password); bool checkUpper(char* password); bool checkLower(char* password); bool checkLength(char* password); bool checkSpace(char* password); bool checkSpecialCharacter(char* password); }; #endif
This is source file : " ClassPassword.cpp"
#include <iostream>
#include <string.h>
#include "ClassPassword.h"
using namespace std;
/**
*constructor of the pass.
*/
ClassPassword::ClassPassword()
{
strcpy(m_aPass," ");
};
/**
* set password of user
*@param : char arrays
*/
bool ClassPassword::setPassword(char* password)
{
bool check1 = checkSpace(password);
bool check2 = checkLength(password);
bool check3 = checkLower(password);
bool check4 = checkUpper(password);
bool check5 = checkNumber(password);
bool check6 = checkSpecialCharacter(password);
if((check1 == true)&&(check2 == true)&&(check3 == true)
&&(check4 == true)&&((check5 == true)||(check6 == true)))
{
strcpy(m_aPass, password);
return true;
}
return false;
};
/**
* get password
* return char array
*/
char* ClassPassword::getPassword()
{
return m_aPass;
};
/**
* shown a String of the pass.
*/
void ClassPassword::show()
{
cout <<"password :" <<m_aPass<<endl;
};
/**
* special character or number
*/
bool ClassPassword::checkNumber(char* p)
{
while(*p !=0)
{
if((*p>47)&&(*p<58))
{
return true;
}
p++;
}
return false;
};
bool ClassPassword::checkUpper(char* p)
{
while(*p!= 0)
{
if((*p>64)&&(*p<91) )
{
return true;
}
p++;
}
return false;
};
bool ClassPassword::checkLower(char* p)
{
while(*p != 0)
{
if((*p>96)&&(*p<123) )
{
return true;
}
p++;
}
return false;
};
bool ClassPassword::checkLength(char* p)
{
int i = 0;
while(*p != 0)
{
if(i>7)
{
return true;
}
i++;
p++;
}
return false;
};
bool ClassPassword::checkSpace(char* p)
{
while(*p != 0)
{
if(*p == 32)
{
return false;
}
p++;
}
return true;
}
bool ClassPassword::checkSpecialCharacter(char* p)
{
while(*p != 0)
{
if(((*p>32)&&(*p<48))||
((*p>57)&&(*p<65))||
((*p>90)&&(*p<97))||
((*p>122)&&(*p<127)))
{
return true;
}
p++;
}
return false;
}
}
Source code: Download2. Struct array :sort , search , delete , display, add..............................................................
This is header file : "BirthDay.h"
/** *Author :ebook Java how to programming */ struct BirthDay { // 1-12 int month; // 1-31 based on month int day; // any year int year; /** */ bool checkMonth(int testMonth) { // validate month if (testMonth > 0 && testMonth <= 12) { return true; } else { // month is invalid return false ;//"month must be 1-12" } } /** */ bool checkDay(int testDay) { // days in each month int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // check if day in range for month if (testDay > 0 && testDay <= daysPerMonth[month]) { return testDay; } // check for leap year if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))) { return true; } else { return false;//day out-of-range for the specified month and year } } void input() { bool check1 = true; bool check2 = true; do { cout<<" Enten birth day :"; cin >> day; cout<<"\nEnten birth month :"; cin>> month; cout<<"\nEnten birth year :"; cin>>year; check1 = checkDay(day); check2 = checkMonth(month); } while((check1 ==false) ||(check2 ==false)); } void display() { cout<< "Birth Day :"<< month<<"/"<<day<<"/"<<year<<endl; } };
This is header file : "FullName.h
/**File : "FullName.h" *Author :TLT. *Day create : 04 OCt 2012 */ struct FullName { char first[10]; char middle[15]; char last[10]; /**validate of name. */ bool checkName(char *ptr) { while(*ptr != 0) { if((*ptr < 97)|| (*ptr >122) ||(*ptr == 32)) { return false; } ptr ++; } return true; } /** input full name */ void input() { bool check1 = true; bool check2 = true; bool check3= true; do { cout<<"Enten fisrt name :"; cin >> first; cout<<"\nEnten middle name :"; cin>> middle; cout<<"\nEnten last name :"; cin>>last; check1 = checkName(first); check2 = checkName(middle); check3 =checkName(last); } while((check1 ==false) ||(check2 ==false)||(check3 ==false)); } /** display name*/ void display() { cout<<"full Name :"<<first<<" " <<middle<<" "<<last<<endl; } };
This is header file : "Object.h"
/*File:Object.h *Author:ThongLt *Day create : 4 Oct 2012. **/ struct Object { float math; float physical; float chemistry; /** validate mark */ bool checkMark(float p) { if( (p>=0)&&(p<=10)) { return true; } else { return false; } } /** input to keyboard*/ void input() { bool check1 = true; bool check2 = true; bool check3= true; do { cout<<" Enten math mark :"; cin >> math; cout<<"\nEnten physical mark :"; cin>> physical; cout<<"\nEnten chemistry mark :"; cin>>chemistry; check1 = checkMark(math); check2 = checkMark(physical); check3 =checkMark(chemistry); } while((check1 ==false) ||(check2 ==false)||(check3 ==false)); } /** average of 3 objects*/ float average() { float value =(math+physical+chemistry)/3; return value; } /** display result of object*/ void display() { cout<<"math :"<<math<<"\nphysical :"<<physical<<"\nchemistry :"<<chemistry<<endl; } };//end struct Object
This is header file : "staff.h"
#include <stdlib.h> #include <fstream> #include <iostream> using namespace std; #include "fullName.h" #include "birthDay.h" #include "Object.h" struct Staff { FullName name; BirthDay birth; char address[30]; Object object; }; void input(struct Staff *pt,int n); void display(struct Staff *pt,int n); void sortBrith(struct Staff *pt,int n); void searchName(struct Staff *pt,int n); void sortMark(struct Staff *pt,int n); void deleteStaff(struct Staff *pt,int *n); void SortNameAndAverage(struct Staff *pt,int n); //----------------------------------------- bool compareBirth(struct BirthDay a, struct BirthDay b); void swapStaff(struct Staff *a,struct Staff *b); bool compareName(char *a , char *b); bool compareSort(char *a , char *b); void saveText(struct Staff *pt,int n);
This is source file : "Staff.cpp"
#include "Staff.h"
#
void input(struct Staff *pt,int n )
{
cout<<" this imformation of staff need input"<<endl;
for(int i = 0; i< n; i++)
{
pt->name.input();
pt->birth.input();
cout<<"Enter address :";
cin>>pt->address;
cout<<endl;
pt->object.input();
pt ++;
}
}
/** */
void display(struct Staff *pt,int n)
{
cout<<" This all imformation of staff ."<<endl;
for(int i = 0; i< n; i++)
{
cout<<"-------------------"<<endl;
pt->name.display();
pt->birth.display();
cout<<"\nAddress "<<pt->address<<endl;
pt->object.display();
pt ++;
}
}
/** */
void sortBrith(struct Staff *pt,int n)
{
int i , j ;
for( i = 0; i < n -1; i++)
{
for( j = i+1; j <n; j++)
{
if(compareBirth(pt[i].birth , pt[j].birth)== true)
{
swapStaff(&pt[i],&pt[j]);
}
}
}
}
/** */
void searchName(struct Staff *pt,int n)
{
char a[10];
cout<<" Enten last name need find :";
fflush(stdin);
cin>>a;
std::cin.clear();
for(int i = 0; i<n ; i++)
{
if(compareName(pt[i].name.last, a)==true)
{
pt[i].name.display();
pt[i].birth.display();
pt[i].object.display();
}
pt++;
}
}
/** */
void sortMark(struct Staff *pt,int n)
{
int i , j ;
for( i = 0; i < n -1; i++)
{
for( j = i+1; j <n; j++)
{
if( pt[i].object.average() < pt[j].object.average() )
{
swapStaff(&pt[i],&pt[j]);
}
}
}
}
/***/
void deleteStaff(struct Staff *pt,int *n){
sortMark(pt, *n);
for(int i =0 ; i< *n ; i++)
{
if( pt[i].object.average()< 4)
{
*n = i;
break;
}
}
}
/** */
void SortNameAndAverage(struct Staff *pt,int n){
int i , j ;
for( i = 0; i < n -1; i++)
{
for( j = i+1; j <n; j++)
{
if(compareSort(pt[i].name.last,pt[j].name.last)==true){
swapStaff(&pt[i],&pt[j]);
}else{
if(compareName( pt[i].name.last,pt[j].name.last)==true){
// To need compare middle name and fisrt name before testing average.
if(pt[i].object.average() < pt[j].object.average())
{
swapStaff(&pt[i],&pt[j]);
}
}
}
}
}
// store data
saveText(pt, n);
}
/** */
void saveText(struct Staff *pt,int n)
{
ofstream myfile ;
myfile.open ("example.txt");
if(myfile.is_open()){
for(int i = 0; i<n ;i++)
{
myfile << pt[i].name.first <<" "<<pt[i].name.middle <<" "<<pt[i].name.last<<"\t";
myfile << pt[i].address<<"\t";
myfile << pt[i].birth.month<<"/" <<pt[i].birth.day<<"/"<<pt[i].birth.year<<"\t,";
myfile << pt[i].object.math<<" "<<pt[i].object.physical<<" " <<pt[i].object.chemistry<<endl;
pt++;
}
myfile.close();
}else{
cout << "Can't open the file!" << endl;
}
}
/** */
bool compareBirth(struct BirthDay a, struct BirthDay b)
{
if(a.year> b.year)
{
return true;
}
else
{
if(a.year == b.year)
{
if(a.month>b.month)
{
return true;
}
else
{
if(a.month==b.month)
{
if(a.day >b.day)
{
return true;
}
}
}
}
}
return false;
}
/** */
void swapStaff(struct Staff *a,struct Staff *b)
{
struct Staff tem;
tem = *a;
*a = *b;
*b = tem;
}
/** */
bool compareName(char *a , char *b)
{
while(*a!= 0&&*b!= 0)
{
if(*a != *b)
{
return false;
}
a++;
b++;
}
return true;
}
/***/
bool compareSort(char *a , char *b){
while(*a!= 0 &&*b!= 0)
{
if(*a < *b)
{
return true;
}
a++;
b++;
}
return false;
}
/** */
This is source file : " main.cpp"#include "staff.h"
int main()
{
struct Staff ptr[10];
int n = 2;
input(ptr,n);
display(ptr,n);
sortBrith(ptr,n);
searchName(ptr,n);
sortMark(ptr,n);
deleteStaff(ptr,&n);
SortNameAndAverage(ptr,n);
display(ptr,n);
return 0;
}
Source code : Download3. Struct link list : sort , search , delete , display ,add .............................................................
/**File : "FullName.h"
*Author :ThongLT.
*Day create : 04 OCt 2012
*/
struct FullName
{
char first[10];
char middle[15];
char last[10];
/**validate of name. */
bool checkName(char *ptr)
{
while(*ptr != 0)
{
if((*ptr < 97)|| (*ptr >122) ||(*ptr == 32))
{
return false;
}
ptr ++;
}
return true;
}
/** input full name */
void input()
{
bool check1 = true;
bool check2 = true;
bool check3= true;
do
{
cout<<"Enten fisrt name :";
cin >> first;
cout<<"\nEnten middle name :";
cin>> middle;
cout<<"\nEnten last name :";
cin>>last;
check1 = checkName(first);
check2 = checkName(middle);
check3 =checkName(last);
}
while((check1 ==false) ||(check2 ==false)||(check3 ==false));
}
/** display name*/
void display()
{
cout<<"full Name :"<<first<<" " <<middle<<" "<<last<<endl;
}
};
/*File:Object.h
*Author:ThongLt
*Day create : 4 Oct 2012.
**/
struct Object
{
float math;
float physical;
float chemistry;
/** validate mark */
bool checkMark(float p)
{
if( (p>=0)&&(p<=10))
{
return true;
}
else
{
return false;
}
}
/** input to keyboard*/
void input()
{
bool check1 = true;
bool check2 = true;
bool check3= true;
do
{
cout<<" Enten math mark :";
cin >> math;
cout<<"\nEnten physical mark :";
cin>> physical;
cout<<"\nEnten chemistry mark :";
cin>>chemistry;
check1 = checkMark(math);
check2 = checkMark(physical);
check3 =checkMark(chemistry);
}
while((check1 ==false) ||(check2 ==false)||(check3 ==false));
}
/** average of 3 objects*/
float average()
{
float value =(math+physical+chemistry)/3;
return value;
}
/** display result of object*/
void display()
{
cout<<"math :"<<math<<"\nphysical :"<<physical<<"\nchemistry :"<<chemistry<<endl;
}
};//end struct Object
/**
*Author :ebook Java how to programming
*/
struct BirthDay
{
// 1-12
int month;
// 1-31 based on month
int day;
// any year
int year;
/** */
bool checkMonth(int testMonth)
{
// validate month
if (testMonth > 0 && testMonth <= 12)
{
return true;
}
else
{
// month is invalid
return false ;//"month must be 1-12"
}
}
/** */
bool checkDay(int testDay)
{
// days in each month
int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// check if day in range for month
if (testDay > 0 && testDay <= daysPerMonth[month])
{
return true;
}
// check for leap year
if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
{
return true;
}
else
{
return false;//day out-of-range for the specified month and year
}
}
void input()
{
bool check1 = true;
bool check2 = true;
do
{
cout<<" Enten birth day :";
cin >> day;
cout<<"\nEnten birth month :";
cin>> month;
cout<<"\nEnten birth year :";
cin>>year;
check1 = checkDay(day);
check2 = checkMonth(month);
}
while((check1 ==false) ||(check2 ==false));
}
void display()
{
cout<< "Birth Day :"<< month<<"/"<<day<<"/"<<year<<endl;
}
};
#include <stdlib.h>
#include <fstream>
#include <iostream>
using namespace std;
#include "fullName.h"
#include "birthDay.h"
#include "Object.h"
struct Staff
{
FullName name;
BirthDay birth;
char address[30];
Object object;
};
typedef struct Node
{
struct Staff data;
struct Node *link;
}NODE;
typedef struct List
{
NODE* first;
NODE* last;
}LIST;
NODE *getNOde(struct Staff pt);
void init(LIST &l);
void addNode( LIST &l, NODE *new_ele);
void addLastList(LIST &l);
void input(LIST &l);
void displayNode(LIST &l);
void input(LIST &l);
void display(LIST &l);
void sortBrith(LIST &l);
void searchName(LIST &l);
void deleteStaff(LIST &l);
void RemoveAverage(LIST &l);
void sortmark(LIST &l);
//-----------------------------------------
bool compareBirth(struct BirthDay a, struct BirthDay b);
void swapStaff(struct Staff *a,struct Staff *b);
bool compareName(char *a , char *b);
#include "Staff.h"
/** **/
NODE *getNOde(struct Staff pt){
NODE *p;
p = (NODE*)malloc(sizeof(NODE));//p= new NODE
if (p==NULL)
{
cout<<"Khong du bo nho!"; exit(1);
}
p->data = pt;
p->link = NULL;
return p;
}
/** **/
void init(LIST &l){
l.first = NULL;
l.last = NULL;
}
/** **/
void addNode( LIST &l, NODE* new_ele){
if (l.first == NULL)
{
l.first = new_ele;
l.last = l.first;
}
else
{
new_ele->link = l.first;
l.first = new_ele;
}
}
/** */
void input(LIST &l){
char check = 'y';
do
{ struct Staff staff;
staff.name.input();
staff.birth.input();
staff.object.input();
cout<<"address :";
cin>>staff.address;
NODE *new_ele = getNOde(staff);
addNode( l,new_ele);
cout<<"\ncontinue N/Y ?.";
cin>>check;
}while((check=='y')||(check=='y'));
}
/** */
void displayNode(LIST &l){
NODE *ptr = l.first;
while(ptr!= NULL){
ptr->data.name.display();
ptr->data.birth.display();
cout<<"Address : "<<ptr->data.address<<endl;
ptr->data.object.display();
ptr = ptr->link;
}
}
/** **/
void addLastList(LIST &l){
struct Staff staff;
staff.name.input();
staff.birth.input();
staff.object.input();
cout<<"address :";
cin>>staff.address;
NODE *new_ele = getNOde(staff);
addNode( l,new_ele);
}
/** **/
void sortBrith(LIST &l){
for ( NODE * p=l.first ; p!=l.last ; p=p->link )
{
for ( NODE * q=p->link ; q!=NULL ; q=q->link )
{
if ( compareBirth(p->data.birth , q->data.birth)==true)
{
swapStaff(&p->data,&q->data);
}
}
}
}
/** **/
void searchName(LIST &l){
char b[10];
cout<<" Enten last name need find :";
fflush(stdin);
cin>>b;
std::cin.clear();
NODE *ptr=l.first;
while( ptr!=NULL ){
if(compareName(ptr->data.name.last , b)== true){
ptr->data.name.display();
ptr->data.birth.display();
cout<<"Address : "<<ptr->data.address<<endl;
ptr->data.object.display();
}
ptr=ptr->link;
}
}
/** */
void deleteStaff(LIST &l){
sortmark(l);
RemoveAverage(l);
}
/***/
void sortmark(LIST &l){
NODE *ptr=l.first;
// sort list
for ( NODE * p=l.first ; p!=l.last ; p=p->link )
{
for ( NODE * q=p->link ; q!=NULL ; q=q->link )
{
if ( (p->data.object.average()) > (q->data.object.average()))
{
swapStaff(&p->data,&q->data);
}
}
}
}
/** */
void RemoveAverage(LIST &l)
{
while(( l.first != NULL) && (l.first->data.object.average()<4))
{
NODE* p=l.first;
l.first=p->link;
if(l.first == NULL)
{
l.last=NULL;
}
free(p);
}
}
/** */
bool compareBirth(struct BirthDay a, struct BirthDay b)
{
if(a.year> b.year)
{
return true;
}
else
{
if(a.year == b.year)
{
if(a.month>b.month)
{
return true;
}
else
{
if(a.month==b.month)
{
if(a.day >b.day)
{
return true;
}
}
}
}
}
return false;
}
/** */
void swapStaff(struct Staff *a,struct Staff *b)
{
struct Staff tem;
tem = *a;
*a = *b;
*b = tem;
}
/** */
bool compareName(char *a , char *b)
{
while(*a!= 0&&*b!= 0)
{
if(*a != *b)
{
return false;
}
a++;
b++;
}
return true;
}
/** */
#include "staff.h"
int main()
{
int i ;
LIST list;
init(list);
input(list);
while(true)
{
cout<<"**************************************\n";
cout<<"* 1 : add new staff *\n";
cout<<"* 2 :delete staff if staff average <4*\n";
cout<<"* 3 :Search Last Name *\n";
cout<<"* 4 :Sort birth day *\n";
cout<<"* 5 :Display list *\n";
cout<<"*************************************";
cin>>i;
switch ( i ) {
case 1: addLastList(list);
break;
case 2: deleteStaff(list);
break;
case 3:searchName(list);
break;
case 4:sortBrith(list);
break;
case 5:displayNode(list);
break;
default:
return 0;
}
}//end loop
return 0;
}
source code :Download4. Class link list: class , Node , List. add , delete..................................................................
/**
*Author :ebook Java how to programming
*/
class BirthDay
{
// 1-12
private :
int month;
// 1-31 based on month
int day;
// any year
int year;
public :
void input();
void display();
private :
/** */
bool checkMonth(int testMonth);
/** */
bool checkDay(int testDay);
};
#include <iostream>
using namespace std;
#include "BirthDay.h"
bool BirthDay::checkMonth(int testMonth)
{
// validate month
if (testMonth > 0 && testMonth <= 12)
{
return true;
}
else
{
// month is invalid
return false ;//"month must be 1-12"
}
}
/** */
bool BirthDay::checkDay(int testDay)
{
// days in each month
int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// check if day in range for month
if (testDay > 0 && testDay <= daysPerMonth[month])
{
return true;
}
// check for leap year
if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
{
return true;
}
else
{
return false;//day out-of-range for the specified month and year
}
}
void BirthDay::input()
{
bool check1 = true;
bool check2 = true;
do
{
cout<<"Enten birth day :";
cin >> day;
cout<<"\nEnten birth month :";
cin>> month;
cout<<"\nEnten birth year :";
cin>>year;
check1 = checkDay(day);
check2 = checkMonth(month);
}
while((check1 ==false) ||(check2 ==false));
}
void BirthDay::display()
{
cout<< "Birth Day :"<< month<<"/"<<day<<"/"<<year<<endl;
}
class CodeNo
{
private:
char code[10];
public :
void input();
void display();
private:
bool checkCodeNo(char* p);
};
#include <iostream>
using namespace std;
#include "CodeNo.h"
void CodeNo::input()
{
bool check1 = true;
do
{
cout<<"Enten CodeNO (08xx) :";
cin >> code;
cout<<endl;
check1 =checkCodeNo(code);
}
while(check1 ==false);
}
bool CodeNo::checkCodeNo(char* ptr)
{
while(*ptr != 0)
{
if((*ptr < 48)|| (*ptr >57) )
{
return false;
}
ptr ++;
}
return true;
}
void CodeNo::display()
{
cout <<"Code :"<< code <<endl;
}
/**File : "FullName.h"
*Author :ThongLT.
*Day create : 04 OCt 2012
*/
class FullName
{
private :
char first[10];
char middle[15];
char last[10];
public :
/** input full name */
void input();
/** display name*/
void display();
char* getLastName();
private :
/**validate of name. */
bool checkName(char *ptr);
};
#include <iostream>
using namespace std;
#include "FullName.h"
bool FullName::checkName(char *ptr)
{
while(*ptr != 0)
{
if((*ptr < 97)|| (*ptr >122) ||(*ptr == 32))
{
return false;
}
ptr ++;
}
return true;
}
/** input full name */
void FullName::input()
{
bool check1 = true;
bool check2 = true;
bool check3= true;
do
{
cout<<"Enten fisrt name :";
cin >> first;
cout<<"\nEnten middle name :";
cin>> middle;
cout<<"\nEnten last name :";
cin>>last;
check1 = checkName(first);
check2 = checkName(middle);
check3 =checkName(last);
}
while((check1 ==false) ||(check2 ==false)||(check3 ==false));
}
/** display name*/
void FullName::display()
{
cout<<"full Name :"<<first<<" " <<middle<<" "<<last<<endl;
}
char* FullName::getLastName()
{
return last;
}
class Diploma
{
private :
char school[30];
int result;
int year;
public :
void input();
void display();
int getYear();
int getResult();
private:
bool checkResult(int a);
bool checkYear(int a);
};
#include <iostream>
using namespace std;
#include <stdio.h>
#include "Diploma.h"
void Diploma::input()
{
bool check1 = true;
bool check2 = true;
do
{
cout<<"Name university :";
fflush(stdin);
cin.getline( school,29);
cout<<"\nEnten result (1 Yeu ,2 TB ,3 K ,4 G:)";
cin>> result;
cout<<"\nEnten graduation years (2012):";
cin>>year;
check1 = checkResult(result);
check2 = checkYear(year);
}
while((check1 ==false) ||(check2 ==false));
}
void Diploma::display()
{
cout<<"School Name :"<<school<<endl;
cout<<"graduation years :"<<year <<endl;
switch ( result )
{
case 1:
cout<<"graduation result is bad "<<endl;
break;
case 2:
cout<<"graduation result is Normal"<<endl;
break;
case 3:
cout<<"graduation result is Good "<<endl;
break;
case 4:
cout<<"graduation result is very Good "<<endl;
break;
default:
break;
}
}
int Diploma::getYear()
{
return year;
}
int Diploma::getResult()
{
return result;
}
bool Diploma::checkResult(int a)
{
if((a>0)&&(a<5))
{
return true;
}
return false;
}
bool Diploma::checkYear(int a)
{
if(a>0)
{
return true;
}
return false;
}
#include "CodeNo.h"
#include "FullName.h"
#include "BirthDay.h"
#include "Diploma.h"
class Student
{
private:
CodeNo code;
FullName name;
BirthDay birth;
Diploma diploma;
public :
void input();
void display();
char* lastName();
int yearGraduate();
int resultGraduate();
};
#include <iostream>
using namespace std;
#include "Student.h"
void Student::input()
{
code.input();
name.input();
birth.input();
diploma.input();
}
void Student::display()
{
code.display();
name.display();
birth.display();
diploma.display();
}
char* Student::lastName()
{
return (name.getLastName());
}
int Student::yearGraduate()
{
return diploma.getYear();
}
int Student::resultGraduate()
{
return diploma.getResult();
}
#include "Student.h"
class Node
{
private:
Student data;
Node *Next;
public :
void SetData(Student aData);
void SetNext(Node* Next);
Student getData();
Node* getNext();
} ;
#include "Node.h"
void Node::SetData(Student aData)
{
data = aData;
};
void Node::SetNext(Node* next)
{
Next = next;
};
Student Node:: getData()
{
return data;
};
Node* Node::getNext()
{
return Next;
};
#include "Node.h"
class List
{
private :
Node *first;
Node *last;
public :
List();
void input();
void addLastStudent();
void searchName();
void sortYearDraduate();
void deleteStudent();
void display();
~List();
private:
Node *getNOde(Student pt);
void addNode( Node* new_ele);
void InsertLast( Node *new_ele);
void sortResult();
bool compareName(char *a , char *b);
void swapStudent(Node *a,Node *b);
};
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#include "List.h"
Node* List::getNOde(Student pt)
{
Node *p;
p = (Node*)malloc(sizeof(Node));//p= new Node
if (p==NULL)
{
cout<<"Khong du bo nho!";
exit(1);
}
p->SetData(pt);
p->SetNext(NULL);
return p;
}
void List::addNode( Node *new_ele)
{
if (first == NULL)
{
first = new_ele;
last= first;
}
else
{
new_ele->SetNext(first);
first=new_ele;
}
}
bool List::compareName(char *a , char *b)
{
while(*a!= 0&&*b!= 0)
{
if(*a != *b)
{
return false;
}
a++;
b++;
}
return true;
}
//*
void List::swapStudent(Node *a,Node *b)
{
Student tem;
tem = a->getData();
a->SetData(b->getData());
b->SetData(tem);
}
void List::InsertLast( Node *new_ele)
{
if (first == NULL)
{
first = new_ele;
last = first;
}
else
{
last->SetNext(new_ele);
last = new_ele ;
}
}
void List::sortResult()
{
for ( Node *i=first ; i!= last ; i=i->getNext() )
{
for ( Node *j=i->getNext() ; j!=NULL ; j=j->getNext())
{
if( i->getData().resultGraduate() > j->getData().resultGraduate() )
{
swapStudent(i,j);
}
}
}
}
//----------------public ------------------
List::List()
{
first= NULL;
last = NULL;
}
void List::input()
{
char check = 'y';
do
{
Student student;
student.input();
Node *new_ele = getNOde(student);
addNode(new_ele);
cout<<"\ncontinue N/Y ?.";
cin>>check;
}
while((check=='y')||(check=='y'));
}
void List::addLastStudent()
{
Student student;
student.input();
Node *new_ele = getNOde(student);
InsertLast(new_ele);
}
void List::display()
{
Node *ptr = first;
while( ptr!=NULL )
{
ptr->getData().display();
ptr=ptr->getNext();
}
}
/** ****/
void List::searchName()
{
Node *ptr=first;
char b[10];
cout<<" Enten last name need find :";
fflush(stdin);
cin>>b;
std::cin.clear();
while( ptr!=NULL )
{
if( compareName( ptr->getData().lastName(),b)== true )
{
ptr->getData().display();
}
ptr=ptr->getNext();
}
}
//-----------------------------------------------------------------------
void List::sortYearDraduate()
{
for ( Node *i=first ; i!= last ; i=i->getNext() )
{
for ( Node *j=i->getNext() ; j!=NULL ; j=j->getNext())
{
if( i->getData().yearGraduate() < j->getData().yearGraduate() )
{
swapStudent(i,j);
}
}
}
}
void List::deleteStudent()
{
sortResult();
while(( first != NULL) && (first->getData().resultGraduate()<3))
{
Node* p=first;
first= p->getNext();
if(first == NULL)
{
last=NULL;
}
free(p);
}
}
List::~List()
{
Node *p;
while (first!= NULL)
{
p = first;
first = p->getNext();
free( p);
}
last = NULL;
cout<<"release memory"<<endl;
}
//------------------------------------------------------------------------
#include <iostream>
using namespace std;
#include "List.h"
int main()
{
List list;
list.input();
int i ;
while(true)
{
cout<<"*********************************************\n";
cout<<"* 1 : add new student *\n";
cout<<"* 2 :delete student if Graduate bad result *\n";
cout<<"* 3 :Search Last Name *\n";
cout<<"* 4 :Sort by graduation year *\n";
cout<<"* 5 :Display list *\n";
cout<<"**************************************** i = ?";
cin>>i;
switch (i)
{
case 1:
list.addLastStudent();
break;
case 2:
list.deleteStudent();
break;
case 3:
list.searchName();
break;
case 4:
list.sortYearDraduate();
break;
case 5:
list.display();
break;
default:
return 0;
}
}//end loop */
return 0;
}
Source code :Download5. Abstract : virtual function .................................................................................................
source code :Download
6. Template: Function -class ..................................................................................................
Ho Chi Minh city.
No comments:
Post a Comment