分析一个月的短信

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;

ifstream in( "data.txt");
ofstream out("out.txt");

typedef struct Phone {
string phoneno;
int count;
}Phone;

vector <Phone> phoneSet;

typedef struct _tagDataType {//一行项作为一个对象来处理
string  phoneno;  //电话号码
string  date;   //日期(暂不考虑)
string  type;   //类型为出或进
double  cost;  //费用
} DataType, *PDataType; //

vector <DataType> vecSet;

void ReadDataSet( )
{
string str;

while( !in.eof() ) {//直到文件末尾
DataType dt;     //创建一个输出数据的对象
memset( &dt, 0, sizeof( dt ) );  //设置其中值为0
str.empty();  
in >> dt.phoneno >> dt.date >> dt.type >> dt.cost;
vecSet.push_back( dt );  //将一行处理后的记录写入到向量中   
}

}
class PhoneAdapter:public unary_function<Phone,bool>
{
public:
string phoneno;
explicit PhoneAdapter(string argNo){phoneno=argNo;}
bool operator ()(const Phone& phone)
{
if (this->phoneno==phone.phoneno) {
return true;
}
else
return false;
}
};

class comparer
{
public:
bool operator()(const Phone & va1, const Phone & va2){
return va1.count > va2.count;
}

};


void main( )
{
double sum=0.0;
double avg=0.0;
if ( !in ) {
cout << "Error" << endl;
}
if ( !out ) {
cout << "Error" << endl;
}
string str;
if (!in.fail()) {
while( !in.eof() ) {
getline(in,str,'\n');
if ( str.size() != 0 ) {
int index = str.find( ' ' );
string tag = str.substr( 0, index ) ;
if(tag.compare("短信")==0)
{
ReadDataSet( );
}
}
}

}
for ( int i = 0; i < vecSet.size(); i++ ) {
cout << vecSet[i].phoneno <<endl;
Phone pt;
pt.count=0;
pt.phoneno=vecSet[i].phoneno;
std::vector<Phone>::iterator pos;
pos = find_if(phoneSet.begin(),phoneSet.end(),PhoneAdapter(vecSet[i].phoneno));
if (pos != phoneSet.end()) {
pos->count++;
}
else
{
pt.count=1;
phoneSet.push_back(pt);

}

sum+=vecSet[i].cost;       
}
sort(phoneSet.begin(),phoneSet.end(),comparer());
for( int n = 0; n < phoneSet.size(); n++){
out << phoneSet[n].phoneno <<" " << phoneSet[n].count <<endl;
}
cout <<phoneSet.size() <<endl;
cout <<vecSet.size() <<endl;
cout << sum << endl;
in.close();

}

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

posted @ 2009-08-14 08:33  莫忆往西  阅读(133)  评论(0)    收藏  举报