钱能《C++程序设计教材》P14
日期数据存在在文件abc.txt中,格式如下面所示,若年,月,日加起来等于15,则收集,然后按日期从小到大的顺序打印出来
Sample Input:
03-11-12
03-08-12
04-08-11
02-07-06
Sample Output:
02年07月06日
03年08月04日
1,c++版本
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//日期类
class Date
{
private:
string year;//年
string month;//月
string day;//日
public:
Date(string strYear,string strMonth,string strDay):year(strYear),month(strMonth),day(strDay)
{
}
~Date()
{
}
string Year()const
{
return year;
}
string Month()const
{
return month;
}
string Day()const
{
return day;
}
};
//用于比较日期
class LessThan
{
public:
bool operator ()(const Date* date1,const Date* date2)
{
if(date1->Year()<date2->Year())
{
return true;
}
else if(date1->Month()<date2->Month())
{
return true;
}
else if(date1->Day()<date2->Day())
{
return true;
}
else
return false;
}
};
//日期文件
class DateContainer
{
private:
static const string fileName;
vector<Date*> m_dates;
public:
DateContainer()
{
}
~DateContainer()
{
vector<Date*>::iterator iter;
for(iter=m_dates.begin();iter!=m_dates.end();++iter)
{
delete (*iter);
}
m_dates.clear();
}
bool ProcessDate()
{//读数据
ifstream infile(DateContainer::fileName.c_str());
if(!infile)
{
return false;
}
string tmpLine;
while(infile>>tmpLine)
{
//读一行数据
int firstPos = tmpLine.find_first_of('-');//第一个'-'
int lastPos = tmpLine.find_last_of('-');//第二个'-'
string strOne = tmpLine.substr(0,2);//第一个域
string strTwo = tmpLine.substr(firstPos+1,2);//第二个域
string strThree = tmpLine.substr(lastPos+1,2);//第三个域
int year,month,day;
year = ProcessField(strOne);
month = ProcessField(strTwo);
day = ProcessField(strThree);
if(IsValidRecord(year,month,day))
{//符合要求,保存此记录
Date* date = new Date(strOne,strTwo,strThree);
m_dates.push_back(date);
}
}
sort(m_dates.begin(),m_dates.end(),LessThan());//排序
printDates();
return true;
}
int ProcessField(string& field)
{//处理各个域
if(field[0]=='0')
{
return field[1]-'0';
}
else
{
return (field[0]-'0')*10+(field[1]-'0');
}
}
bool IsValidRecord(int first,int second ,int third)
{//是否合法记录
return (first+second+third)==15;
}
void printDates()
{//遍历输出
vector<Date*>::iterator iter;
for(iter=m_dates.begin();iter!=m_dates.end();++iter)
{
cout<<(*iter)->Year()<<"年"<<(*iter)->Month()<<"月"<<(*iter)->Day()<<"日"<<endl;
}
}
};
const string DateContainer::fileName = "D:\\abc.txt";//数据文件
int main()
{
DateContainer container;
//读取数据
if(!container.ProcessDate())
{
cout<<"error"<<endl;
return -1;
}
system("pause");
return 0;
}
2,C#版:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication1
{
//日期类
class Date : IComparable
{
private String year;//年
private String month;//月
private String day;//日
public Date(String strYear,String strMonth,String strDay)
{
this.year = strYear;
this.month = strMonth;
this.day = strDay;
}
public String Year
{
get
{
return year;
}
}
public String Month
{
get
{
return month;
}
}
public String Day
{
get
{
return day;
}
}
public int CompareTo(object obj)
{
if (obj is Date)
{
Date otherDate = (Date)obj;
if (this.Year != otherDate.Year)
{
return this.Year.CompareTo(otherDate.Year);
}
else if (this.Month != otherDate.Month)
{
return this.Month.CompareTo(otherDate.Month);
}
else if (this.Day != otherDate.Day)
{
return this.Day.CompareTo(otherDate.Day);
}
else
return 0;
}
else
{
throw new ArgumentException("object is not a Date");
}
}
}
//日期文件
class DateContainer
{
private const String fileName = "D:\\abc.txt";
private ArrayList m_dates = new ArrayList();
public DateContainer()
{
}
public bool ProcessDate()
{//读数据
StreamReader din = File.OpenText(fileName);
String tmpLine;
while ((tmpLine = din.ReadLine()) != null)
{
//读一行数据
int firstPos = tmpLine.IndexOf('-');//第一个'-'
int lastPos = tmpLine.LastIndexOf('-');//第二个'-'
String strOne = tmpLine.Substring(0, 2);//第一个域
String strTwo = tmpLine.Substring(firstPos + 1, 2);//第二个域
String strThree = tmpLine.Substring(lastPos + 1, 2);//第三个域
int year, month, day;
year = ProcessField(strOne);
month = ProcessField(strTwo);
day = ProcessField(strThree);
if (IsValidRecord(year, month, day))
{//符合要求,保存此记录
Date date = new Date(strOne, strTwo, strThree);
m_dates.Add(date);
}
}
m_dates.Sort();
printDates();
return true;
}
public int ProcessField(String field)
{//处理各个域
if(field[0]=='0')
{
return field[1]-'0';
}
else
{
return (field[0]-'0')*10+(field[1]-'0');
}
}
public bool IsValidRecord(int first,int second ,int third)
{//是否合法记录
return (first+second+third)==15;
}
public void printDates()
{//遍历输出
foreach (Date date in m_dates)
{
System.Console.WriteLine("{0}年{1}月{2}日", date.Year, date.Month, date.Day);
}
}
}
class Program
{
static void Main(string[] args)
{
DateContainer container = new DateContainer();
container.ProcessDate();
}
}
}
3,java版:
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;
//日期类
class MyDate
{
private String year;//年
private String month;//月
private String day;//日
public MyDate(String strYear,String strMonth,String strDay)
{
this.year = strYear;
this.month = strMonth;
this.day = strDay;
}
public String getDay() {
return day;
}
public String getMonth() {
return month;
}
public String getYear() {
return year;
}
}
// 用于比较日期
class LessThan implements Comparator
{
public int compare(Object o1, Object o2)
{
MyDate date1 = (MyDate)o1;
MyDate date2 = (MyDate)o2;
if(date1.getYear()!=date2.getYear())
{
return date1.getYear().compareTo(date2.getYear());
}
else if (date1.getMonth() != date2.getMonth())
{
return date1.getMonth().compareTo(date2.getMonth());
}
else if (date1.getDay() != date2.getDay())
{
return date1.getDay().compareTo(date2.getDay());
}
else
return 0;
}
}
//日期文件
class DateContainer
{
private final String fileName = "D:\\abc.txt";
private ArrayList<MyDate> m_dates = new ArrayList();
public DateContainer()
{
}
@SuppressWarnings("unchecked")
public boolean ProcessDate()
{//读数据
FileReader fr = null;
BufferedReader br = null;
StringBuffer sBuffer = new StringBuffer();
try
{
try
{
fr = new FileReader(fileName);// 建立FileReader对象,并实例化为fr
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
br = new BufferedReader(fr);// 建立BufferedReader对象,并实例化为br
String tmpLine = br.readLine();// 从文件读取一行字符串
// 判断读取到的字符串是否不为空
while (tmpLine != null)
{
int firstPos = tmpLine.indexOf('-');//第一个'-'
int lastPos = tmpLine.lastIndexOf('-');//第二个'-'
String strOne = tmpLine.substring(0, 2);//第一个域
String strTwo = tmpLine.substring(firstPos + 1, lastPos);//第二个域
String strThree = tmpLine.substring(lastPos + 1, tmpLine.length());//第三个域
int year, month, day;
year = ProcessField(strOne);
month = ProcessField(strTwo);
day = ProcessField(strThree);
if (IsValidRecord(year, month, day))
{//符合要求,保存此记录
MyDate date = new MyDate(strOne, strTwo, strThree);
m_dates.add(date);
}
tmpLine = br.readLine();// 从文件中继续读取一行数据
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
br.close();// 关闭BufferedReader对象
if (fr != null)
fr.close();// 关闭文件
}
catch (IOException e)
{
e.printStackTrace();
}
}
Comparator comp = new LessThan();
Collections.sort(m_dates, comp);
printDates();
return true;
}
public int ProcessField(String field)
{//处理各个域
if(field.charAt(0)=='0')
{
return field.charAt(1)-'0';
}
else
{
return (field.charAt(0)-'0')*10+(field.charAt(1)-'0');
}
}
public boolean IsValidRecord(int first,int second ,int third)
{//是否合法记录
return (first+second+third)==15;
}
public void printDates()
{//遍历输出
for (MyDate date :m_dates)
{
System.out.println(date.getYear()+"年"+ date.getMonth()+"月"+ date.getDay()+"日");
}
}
}
public class DateDemo
{
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
DateContainer container = new DateContainer();
container.ProcessDate();
System.in.read();
}
}
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/05/09/1190537.html,如需转载请自行联系原作者