📄 array.cpp
字号:
// array.cpp
//
// Copyright (c) 1999-2007 Symbian Software Ltd. All rights reserved.
//
// $Change: 937687 $
// Example to demonstrate arrays within EPOC.
//
// The example:
//
// 1. Creates implements a simple tui based menu driven
// console based application to demonstrate features of
// the CArrayFixFlat<class T> array class.
// 2. Several methods within the CUniversity class will be
// left for students to complete.
//
// Notes:
//
// Uses a simple Text editor /*Tuiedit*/ to handle console based
// data entry.
//
// File Layout:
//
// class TStudent{...};
// class CUniversity{...};
// Function doTestStudentL{...}
// Function doExampleL{...} /*Called indirectly via E32Main()*/
//
// SYSTEM HEADERS
#include <e32svr.h>
// PROJECT HEADERS
#include "arrayCons.h"
#include "Tuiedit.h"
//*******************************************************************************************************
// Partial example of a University/Student system implemented
// as a CUniverity class maintaining an array of TStudent objects.
class CUniversity;
class TStudent
{
friend class CUniversity;
public :
TStudent();
inline void SetNameL(const TDesC& aName);
inline const TDesC& GetName() const;
inline void SetCourseL(const TDesC& aCourse);
inline const TDesC& GetCourse() const;
inline void SetAge(const TInt aAge);
inline const TInt GetAge() const;
inline void SetStudentID(const TInt aID);
inline const TInt GetStudentID() const;
private :
TInt iAge;
TInt iStudentID;
TBuf<20> iName;
TBuf<30> iCourse;
};
TStudent::TStudent()
: iAge(0), iStudentID(0)
{
}
inline void TStudent::SetNameL(const TDesC& aName){iName = aName;}
inline const TDesC& TStudent::GetName() const {return iName;}
inline void TStudent::SetCourseL(const TDesC& aCourse) {iCourse = aCourse;}
inline const TDesC& TStudent::GetCourse() const {return iCourse;}
inline void TStudent::SetAge(const TInt aAge){iAge = aAge;}
inline const TInt TStudent::GetAge() const {return iAge;}
inline void TStudent::SetStudentID(const TInt aID){iStudentID = aID;}
inline const TInt TStudent::GetStudentID() const {return iStudentID;}
//*******************************************************************************************************
class CUniversity : public CBase
{
public :
enum TSortCategory {ESortName, ESortStudentID};
static CUniversity* NewL();
~CUniversity();
inline void SetNameL(const TDesC& aName);
inline const TDesC& GetName() const;
inline void SetAddressL(const TDesC& aAddress);
inline const TDesC& GetAddress() const;
inline void SetConsole(CConsoleBase* aConsole);
void DisplayStudents();
void DisplayStudent(const TInt aPos);
void DisplayStudent(const TStudent& aStudent);
void DisplayUniversityDetails();
// Array maintenance
TStudent& AddStudentL();
void AddStudentL(const TDesC& aName, const TInt aAge, const TDesC& aCourse);
//TODO
void AddStudentL(TStudent& aStudent);
void DeleteStudent(const TDesC& aName);
void SortStudents(const TSortCategory aSort);
const TInt FindStudent(const TDesC& aName, TStudent& aStudent, TInt& aPos);
//TODO
const TInt FindStudent(const TInt aStudentID, TStudent& aStudent, TInt& aPos);
TStudent& Student(const TInt aPos);
inline TInt NameKeyOffset();
private :
CUniversity();
void ConstructL();
CArrayFixFlat<TStudent>* iStudents;
TBuf<50> iName;
HBufC* iAddress;
CConsoleBase* iConsole;
TInt iUniqueID;
};
CUniversity* CUniversity::NewL()
{
CUniversity* obj = new(ELeave) CUniversity();
CleanupStack::PushL(obj);
obj->ConstructL();
CleanupStack::Pop(obj);
return obj;
}
CUniversity::CUniversity()
: iAddress(0), iUniqueID(0)
{
}
void CUniversity::ConstructL()
{
iStudents = new(ELeave) CArrayFixFlat<TStudent>(10);
}
CUniversity::~CUniversity()
{
delete iAddress;
delete iStudents;
}
inline void CUniversity::SetNameL(const TDesC& aName) {iName = aName;}
inline const TDesC& CUniversity::GetName() const {return iName;}
inline void CUniversity::SetAddressL(const TDesC& aAddress) {iAddress = aAddress.AllocL();}
inline const TDesC& CUniversity::GetAddress() const {return *iAddress;}
inline void CUniversity::SetConsole(CConsoleBase* aConsole) {iConsole = aConsole;}
inline TInt CUniversity::NameKeyOffset() {return _FOFF(CUniversity, iName);}
void CUniversity::SortStudents(const TSortCategory aSort)
{
// Sort on either name or ID
switch(aSort)
{
case ESortName:
{
TKeyArrayFix aNameKey(_FOFF(TStudent, iName), ECmpFolded);
iStudents->Sort(aNameKey);
break;
}
case ESortStudentID:
{
TKeyArrayFix aIDKey(_FOFF(TStudent, iStudentID), ECmpTInt);
iStudents->Sort(aIDKey);
break;
}
default:
break;
}
}
void CUniversity::DisplayStudents()
{
// Display the details of each student.
TInt numStudents = iStudents->Count();
_LIT(KStudentFormat, "\nID \t\t: %d \nName \t: %S \nAge \t: %d \nCourse \t: %S");
_LIT(KNewLine, "\n");
iConsole->Printf(KNewLine);
for (TInt count = 0; count < numStudents; count++)
{
TStudent currentStudent = (*iStudents)[count];
iConsole->Printf(KStudentFormat, currentStudent.GetStudentID(),
¤tStudent.GetName(), currentStudent.GetAge(),
¤tStudent.GetCourse());
}
iConsole->Printf(KNewLine);
}
void CUniversity::DisplayStudent(const TInt aPos)
{
// Display the details of the student at position aPos.
_LIT(KStudentFormat, "\nID \t\t: %d \nName \t: %S \nAge \t: %d \nCourse \t: %S");
_LIT(KNewLine, "\n");
iConsole->Printf(KNewLine);
TStudent currentStudent = (*iStudents)[aPos];
iConsole->Printf(KStudentFormat, currentStudent.GetStudentID(),
¤tStudent.GetName(), currentStudent.GetAge(), ¤tStudent.GetCourse());
iConsole->Printf(KNewLine);
}
void CUniversity::DisplayStudent(const TStudent& aStudent)
{
// Display the details of the supplied student
_LIT(KStudentFormat, "\nID \t\t: %d \nName \t: %S \nAge \t: %d \nCourse \t: %S");
_LIT(KNewLine, "\n");
iConsole->Printf(KNewLine);
iConsole->Printf(KStudentFormat, aStudent.GetStudentID(),
&aStudent.GetName(), aStudent.GetAge(), &aStudent.GetCourse());
iConsole->Printf(KNewLine);
}
void CUniversity::DisplayUniversityDetails()
{
// Display the name and address of the university and also
// the number of students in residence.
_LIT(KUniComment1, "\n\nWelecome to %S \nUniversity\n");
_LIT(KUniComment2, "You can find us at the \nfollowing address : \n%S");
_LIT(KUniComment3, "\nWe have %d student(s)\nin residence.\n");
_LIT(KCommonFormat1, "%S");
iConsole->Printf(KUniComment1, &iName);
iConsole->Printf(KUniComment2, iAddress);
iConsole->Printf(KUniComment3, iStudents->Count());
}
TStudent& CUniversity::AddStudentL()
{
// Extend the array of students and allow the user to initialize the
// properties of a new student directly
return iStudents->ExtendL();
}
void CUniversity::AddStudentL(const TDesC& aName, const TInt aAge, const TDesC& aCourse)
{
// Extend the array of students and allow initialize the
// properties of the new student with the supplied parameters
TStudent& newStudent = iStudents->ExtendL();
newStudent.SetNameL(aName);
newStudent.SetAge(aAge);
newStudent.SetCourseL(aCourse);
newStudent.SetStudentID(++iUniqueID);
}
void CUniversity::AddStudentL(TStudent& aStudent)
{
// Allow the user to insert a student by name key
TKeyArrayFix aNameKey(_FOFF(TStudent, iName), ECmpFolded);
aStudent.SetStudentID(++iUniqueID);
iStudents->InsertIsqL(aStudent, aNameKey);
}
void CUniversity::DeleteStudent(const TDesC& aName)
{
// Remove a student by name
CLineEdit* ptrEdit = CLineEdit::NewL(iConsole, 10);
TInt posToDelete;
TStudent stuToDelete;
TInt result = FindStudent(aName, stuToDelete, posToDelete);
if (result > 0)
{
delete ptrEdit;
return;
}
else
{
iStudents->Delete(posToDelete);
delete ptrEdit;
}
}
const TInt CUniversity::FindStudent(const TDesC& aName, TStudent& aStudent, TInt& aPos)
{
// Return result which is 0 if element is found, non-zero otherwise
TKeyArrayFix aNameKey(_FOFF(TStudent, iName), ECmpFolded);
TInt foundPos;
TStudent refElement;
refElement.SetNameL(aName);
TInt result = iStudents->Find(refElement, aNameKey, foundPos);
if (result>0)
{
return result;
}
else
{
aStudent = (*iStudents)[foundPos];
aPos = foundPos;
return result;
}
}
const TInt CUniversity::FindStudent(const TInt aStudentID, TStudent& aStudent, TInt& aPos)
{
// Return result which is 0 if element is found, non-zero otherwise
TKeyArrayFix aIDKey(_FOFF(TStudent, iStudentID), ECmpTInt);
TInt foundPos;
TStudent refElement;
refElement.SetStudentID(aStudentID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -