钱能《C++程序设计教材》P14
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
日期数据存在在文件abc.txt中,格式如下面所示,若年,月,日加起来等于15,则收集,然后按日期从小到大的顺序打印出来
Sample Input:
03
-
11
-
12
03
-
08
-
12
04
-
08
-
11
02
-
07
-
06
Sample Output:
<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chsdate year="2003" month="08" day="04" islunardate="False" isrocdate="False" w:st="on"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: 'Times New Roman'"></span></chsdate>
1,c++版本
#include
<
fstream
>
#include
<
iostream
>
#include
<
string
>
#include
<
vector
>
#include
<
algorithm
>
using
namespace
std;
![None.gif](/Images/OutliningIndicators/None.gif)
//
日期类
class
Date
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private:
stringyear;//年
stringmonth;//月
stringday;//日
public:
Date(stringstrYear,stringstrMonth,stringstrDay):year(strYear),month(strMonth),day(strDay)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
~Date()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
stringYear()const
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnyear;
}
stringMonth()const
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnmonth;
}
stringDay()const
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnday;
}
}
;
![None.gif](/Images/OutliningIndicators/None.gif)
//
用于比较日期
class
LessThan
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public:
booloperator()(constDate*date1,constDate*date2)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(date1->Year()<date2->Year())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returntrue;
}
elseif(date1->Month()<date2->Month())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returntrue;
}
elseif(date1->Day()<date2->Day())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returntrue;
}
else
returnfalse;
}
}
;
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
//
日期文件
class
DateContainer
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private:
staticconststringfileName;
vector<Date*>m_dates;
public:
DateContainer()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
~DateContainer()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
vector<Date*>::iteratoriter;
for(iter=m_dates.begin();iter!=m_dates.end();++iter)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
delete(*iter);
}
m_dates.clear();
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
boolProcessDate()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//读数据
ifstreaminfile(DateContainer::fileName.c_str());
if(!infile)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnfalse;
}
stringtmpLine;
while(infile>>tmpLine)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//读一行数据
intfirstPos=tmpLine.find_first_of('-');//第一个'-'
intlastPos=tmpLine.find_last_of('-');//第二个'-'
stringstrOne=tmpLine.substr(0,2);//第一个域
stringstrTwo=tmpLine.substr(firstPos+1,2);//第二个域
stringstrThree=tmpLine.substr(lastPos+1,2);//第三个域
intyear,month,day;
year=ProcessField(strOne);
month=ProcessField(strTwo);
day=ProcessField(strThree);
if(IsValidRecord(year,month,day))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//符合要求,保存此记录
Date*date=newDate(strOne,strTwo,strThree);
m_dates.push_back(date);
}
}
sort(m_dates.begin(),m_dates.end(),LessThan());//排序
printDates();
returntrue;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
intProcessField(string&field)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//处理各个域
if(field[0]=='0')
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnfield[1]-'0';
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return(field[0]-'0')*10+(field[1]-'0');
}
}
boolIsValidRecord(intfirst,intsecond,intthird)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//是否合法记录
return(first+second+third)==15;
}
voidprintDates()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//遍历输出
vector<Date*>::iteratoriter;
for(iter=m_dates.begin();iter!=m_dates.end();++iter)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
cout<<(*iter)->Year()<<"年"<<(*iter)->Month()<<"月"<<(*iter)->Day()<<"日"<<endl;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
;
![None.gif](/Images/OutliningIndicators/None.gif)
const
string
DateContainer::fileName
=
"
D://abc.txt
"
;
//
数据文件
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DateContainercontainer;
//读取数据
if(!container.ProcessDate())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
cout<<"error"<<endl;
return-1;
}
system("pause");
return0;
}
2,C#
版:
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Collections;
using
System.IO;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
ConsoleApplication1
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//日期类
classDate:IComparable
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
privateStringyear;//年
privateStringmonth;//月
privateStringday;//日
publicDate(StringstrYear,StringstrMonth,StringstrDay)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
this.year=strYear;
this.month=strMonth;
this.day=strDay;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
publicStringYear
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnyear;
}
}
publicStringMonth
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnmonth;
}
}
publicStringDay
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnday;
}
}
publicintCompareTo(objectobj)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(objisDate)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
DateotherDate=(Date)obj;
if(this.Year!=otherDate.Year)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnthis.Year.CompareTo(otherDate.Year);
}
elseif(this.Month!=otherDate.Month)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnthis.Month.CompareTo(otherDate.Month);
}
elseif(this.Day!=otherDate.Day)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnthis.Day.CompareTo(otherDate.Day);
}
else
return0;
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
thrownewArgumentException("objectisnotaDate");
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//日期文件
classDateContainer
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
privateconstStringfileName="D://abc.txt";
privateArrayListm_dates=newArrayList();
publicDateContainer()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
publicboolProcessDate()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//读数据
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
StreamReaderdin=File.OpenText(fileName);
StringtmpLine;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
while((tmpLine=din.ReadLine())!=null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//读一行数据
intfirstPos=tmpLine.IndexOf('-');//第一个'-'
intlastPos=tmpLine.LastIndexOf('-');//第二个'-'
StringstrOne=tmpLine.Substring(0,2);//第一个域
StringstrTwo=tmpLine.Substring(firstPos+1,2);//第二个域
StringstrThree=tmpLine.Substring(lastPos+1,2);//第三个域
intyear,month,day;
year=ProcessField(strOne);
month=ProcessField(strTwo);
day=ProcessField(strThree);
if(IsValidRecord(year,month,day))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//符合要求,保存此记录
Datedate=newDate(strOne,strTwo,strThree);
m_dates.Add(date);
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
m_dates.Sort();
printDates();
returntrue;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
publicintProcessField(Stringfield)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//处理各个域
if(field[0]=='0')
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnfield[1]-'0';
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return(field[0]-'0')*10+(field[1]-'0');
}
}
publicboolIsValidRecord(intfirst,intsecond,intthird)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//是否合法记录
return(first+second+third)==15;
}
publicvoidprintDates()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//遍历输出
foreach(Datedateinm_dates)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
System.Console.WriteLine("{0}年{1}月{2}日",date.Year,date.Month,date.Day);
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
classProgram
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
staticvoidMain(string[]args)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
DateContainercontainer=newDateContainer();
container.ProcessDate();
}
}
}
![None.gif](/Images/OutliningIndicators/None.gif)
3,java版:
![None.gif](/Images/OutliningIndicators/None.gif)
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
import
java.io.BufferedReader;
import
java.io.FileReader;
import
java.io.FileNotFoundException;
import
java.io.IOException;
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
//
日期类
class
MyDate
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
privateStringyear;//年
privateStringmonth;//月
privateStringday;//日
publicMyDate(StringstrYear,StringstrMonth,StringstrDay)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
this.year=strYear;
this.month=strMonth;
this.day=strDay;
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
publicStringgetDay()
{
returnday;
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
publicStringgetMonth()
{
returnmonth;
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
publicStringgetYear()
{
returnyear;
}
}
//
用于比较日期
class
LessThan
implements
Comparator
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
publicintcompare(Objecto1,Objecto2)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
MyDatedate1=(MyDate)o1;
MyDatedate2=(MyDate)o2;
if(date1.getYear()!=date2.getYear())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returndate1.getYear().compareTo(date2.getYear());
}
elseif(date1.getMonth()!=date2.getMonth())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returndate1.getMonth().compareTo(date2.getMonth());
}
elseif(date1.getDay()!=date2.getDay())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returndate1.getDay().compareTo(date2.getDay());
}
else
return0;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![None.gif](/Images/OutliningIndicators/None.gif)
//
日期文件
class
DateContainer
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
privatefinalStringfileName="D://abc.txt";
privateArrayList<MyDate>m_dates=newArrayList();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
publicDateContainer()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
@SuppressWarnings("unchecked")
publicbooleanProcessDate()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//读数据
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
FileReaderfr=null;
BufferedReaderbr=null;
StringBuffersBuffer=newStringBuffer();
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
fr=newFileReader(fileName);//建立FileReader对象,并实例化为fr
}
catch(FileNotFoundExceptione)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
e.printStackTrace();
}
br=newBufferedReader(fr);//建立BufferedReader对象,并实例化为br
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
StringtmpLine=br.readLine();//从文件读取一行字符串
//判断读取到的字符串是否不为空
while(tmpLine!=null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
intfirstPos=tmpLine.indexOf('-');//第一个'-'
intlastPos=tmpLine.lastIndexOf('-');//第二个'-'
StringstrOne=tmpLine.substring(0,2);//第一个域
StringstrTwo=tmpLine.substring(firstPos+1,lastPos);//第二个域
StringstrThree=tmpLine.substring(lastPos+1,tmpLine.length());//第三个域
intyear,month,day;
year=ProcessField(strOne);
month=ProcessField(strTwo);
day=ProcessField(strThree);
if(IsValidRecord(year,month,day))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//符合要求,保存此记录
MyDatedate=newMyDate(strOne,strTwo,strThree);
m_dates.add(date);
}
tmpLine=br.readLine();//从文件中继续读取一行数据
}
}
catch(IOExceptione)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
e.printStackTrace();
}
finally
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(br!=null)
br.close();//关闭BufferedReader对象
if(fr!=null)
fr.close();//关闭文件
}
catch(IOExceptione)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
e.printStackTrace();
}
}
Comparatorcomp=newLessThan();
Collections.sort(m_dates,comp);
printDates();
returntrue;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
publicintProcessField(Stringfield)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//处理各个域
if(field.charAt(0)=='0')
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnfield.charAt(1)-'0';
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return(field.charAt(0)-'0')*10+(field.charAt(1)-'0');
}
}
publicbooleanIsValidRecord(intfirst,intsecond,intthird)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//是否合法记录
return(first+second+third)==15;
}
publicvoidprintDates()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//遍历输出
for(MyDatedate:m_dates)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
System.out.println(date.getYear()+"年"+date.getMonth()+"月"+date.getDay()+"日");
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![None.gif](/Images/OutliningIndicators/None.gif)
public
class
DateDemo
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
publicstaticvoidmain(String[]args)throwsIOException
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//TODOAuto-generatedmethodstub
DateContainercontainer=newDateContainer();
container.ProcessDate();
System.in.read();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![None.gif](/Images/OutliningIndicators/None.gif)