钱能《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>
02年07月06日
03年08月04日
1,c++版本
#include<fstream>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
usingnamespacestd;
//日期类
classDate

{
private:
stringyear;//年
stringmonth;//月
stringday;//日
public:
Date(stringstrYear,stringstrMonth,stringstrDay):year(strYear),month(strMonth),day(strDay)

{
}
~Date()

{
}
stringYear()const

{
returnyear;
}
stringMonth()const

{
returnmonth;
}
stringDay()const

{
returnday;
}
};
//用于比较日期
classLessThan

{
public:
booloperator()(constDate*date1,constDate*date2)

{
if(date1->Year()<date2->Year())

{
returntrue;
}
elseif(date1->Month()<date2->Month())

{
returntrue;
}
elseif(date1->Day()<date2->Day())

{
returntrue;
}
else
returnfalse;
}
};

//日期文件
classDateContainer

{
private:
staticconststringfileName;
vector<Date*>m_dates;
public:
DateContainer()

{
}
~DateContainer()

{
vector<Date*>::iteratoriter;
for(iter=m_dates.begin();iter!=m_dates.end();++iter)

{
delete(*iter);
}
m_dates.clear();
}
boolProcessDate()

{//读数据
ifstreaminfile(DateContainer::fileName.c_str());
if(!infile)

{
returnfalse;
}
stringtmpLine;
while(infile>>tmpLine)

{
//读一行数据
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))

{//符合要求,保存此记录
Date*date=newDate(strOne,strTwo,strThree);
m_dates.push_back(date);
}
}
sort(m_dates.begin(),m_dates.end(),LessThan());//排序
printDates();
returntrue;
}
intProcessField(string&field)

{//处理各个域
if(field[0]=='0')

{
returnfield[1]-'0';
}
else

{
return(field[0]-'0')*10+(field[1]-'0');
}
}
boolIsValidRecord(intfirst,intsecond,intthird)

{//是否合法记录
return(first+second+third)==15;
}
voidprintDates()

{//遍历输出
vector<Date*>::iteratoriter;
for(iter=m_dates.begin();iter!=m_dates.end();++iter)

{
cout<<(*iter)->Year()<<"年"<<(*iter)->Month()<<"月"<<(*iter)->Day()<<"日"<<endl;
}
}
};
conststringDateContainer::fileName="D://abc.txt";//数据文件
intmain()

{
DateContainercontainer;
//读取数据
if(!container.ProcessDate())

{
cout<<"error"<<endl;
return-1;
}
system("pause");
return0;
}
2,C#
版:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Collections;
usingSystem.IO;
namespaceConsoleApplication1

{
//日期类
classDate:IComparable

{
privateStringyear;//年
privateStringmonth;//月
privateStringday;//日
publicDate(StringstrYear,StringstrMonth,StringstrDay)

{
this.year=strYear;
this.month=strMonth;
this.day=strDay;
}
publicStringYear

{
get

{
returnyear;
}
}
publicStringMonth

{
get

{
returnmonth;
}
}
publicStringDay

{
get

{
returnday;
}
}
publicintCompareTo(objectobj)

{
if(objisDate)

{
DateotherDate=(Date)obj;
if(this.Year!=otherDate.Year)

{
returnthis.Year.CompareTo(otherDate.Year);
}
elseif(this.Month!=otherDate.Month)

{
returnthis.Month.CompareTo(otherDate.Month);
}
elseif(this.Day!=otherDate.Day)

{
returnthis.Day.CompareTo(otherDate.Day);
}
else
return0;
}
else

{
thrownewArgumentException("objectisnotaDate");
}
}
}
//日期文件
classDateContainer

{
privateconstStringfileName="D://abc.txt";
privateArrayListm_dates=newArrayList();
publicDateContainer()

{
}
publicboolProcessDate()

{//读数据
StreamReaderdin=File.OpenText(fileName);
StringtmpLine;
while((tmpLine=din.ReadLine())!=null)

{
//读一行数据
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))

{//符合要求,保存此记录
Datedate=newDate(strOne,strTwo,strThree);
m_dates.Add(date);
}
}
m_dates.Sort();
printDates();
returntrue;
}
publicintProcessField(Stringfield)

{//处理各个域
if(field[0]=='0')

{
returnfield[1]-'0';
}
else

{
return(field[0]-'0')*10+(field[1]-'0');
}
}
publicboolIsValidRecord(intfirst,intsecond,intthird)

{//是否合法记录
return(first+second+third)==15;
}
publicvoidprintDates()

{//遍历输出
foreach(Datedateinm_dates)

{
System.Console.WriteLine("{0}年{1}月{2}日",date.Year,date.Month,date.Day);
}
}
}
classProgram

{
staticvoidMain(string[]args)

{
DateContainercontainer=newDateContainer();
container.ProcessDate();
}
}
}
3,java版:


importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.io.BufferedReader;
importjava.io.FileReader;
importjava.io.FileNotFoundException;
importjava.io.IOException;

//日期类
classMyDate

{
privateStringyear;//年
privateStringmonth;//月
privateStringday;//日
publicMyDate(StringstrYear,StringstrMonth,StringstrDay)

{
this.year=strYear;
this.month=strMonth;
this.day=strDay;
}
publicStringgetDay()
{
returnday;
}
publicStringgetMonth()
{
returnmonth;
}
publicStringgetYear()
{
returnyear;
}
}
//用于比较日期
classLessThanimplementsComparator

{
publicintcompare(Objecto1,Objecto2)

{
MyDatedate1=(MyDate)o1;
MyDatedate2=(MyDate)o2;
if(date1.getYear()!=date2.getYear())

{
returndate1.getYear().compareTo(date2.getYear());
}
elseif(date1.getMonth()!=date2.getMonth())

{
returndate1.getMonth().compareTo(date2.getMonth());
}
elseif(date1.getDay()!=date2.getDay())

{
returndate1.getDay().compareTo(date2.getDay());
}
else
return0;
}
}
//日期文件
classDateContainer

{
privatefinalStringfileName="D://abc.txt";
privateArrayList<MyDate>m_dates=newArrayList();
publicDateContainer()

{
}
@SuppressWarnings("unchecked")
publicbooleanProcessDate()

{//读数据
FileReaderfr=null;
BufferedReaderbr=null;
StringBuffersBuffer=newStringBuffer();
try

{
try

{
fr=newFileReader(fileName);//建立FileReader对象,并实例化为fr
}
catch(FileNotFoundExceptione)

{
e.printStackTrace();
}
br=newBufferedReader(fr);//建立BufferedReader对象,并实例化为br
StringtmpLine=br.readLine();//从文件读取一行字符串
//判断读取到的字符串是否不为空
while(tmpLine!=null)

{
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))

{//符合要求,保存此记录
MyDatedate=newMyDate(strOne,strTwo,strThree);
m_dates.add(date);
}
tmpLine=br.readLine();//从文件中继续读取一行数据
}
}
catch(IOExceptione)

{
e.printStackTrace();
}
finally

{
try

{
if(br!=null)
br.close();//关闭BufferedReader对象
if(fr!=null)
fr.close();//关闭文件
}
catch(IOExceptione)

{
e.printStackTrace();
}
}
Comparatorcomp=newLessThan();
Collections.sort(m_dates,comp);
printDates();
returntrue;
}
publicintProcessField(Stringfield)

{//处理各个域
if(field.charAt(0)=='0')

{
returnfield.charAt(1)-'0';
}
else

{
return(field.charAt(0)-'0')*10+(field.charAt(1)-'0');
}
}
publicbooleanIsValidRecord(intfirst,intsecond,intthird)

{//是否合法记录
return(first+second+third)==15;
}
publicvoidprintDates()

{//遍历输出
for(MyDatedate:m_dates)

{
System.out.println(date.getYear()+"年"+date.getMonth()+"月"+date.getDay()+"日");
}
}
}
publicclassDateDemo

{
publicstaticvoidmain(String[]args)throwsIOException

{
//TODOAuto-generatedmethodstub
DateContainercontainer=newDateContainer();
container.ProcessDate();
System.in.read();
}
}
141

被折叠的 条评论
为什么被折叠?



