📄 statistic.cpp
字号:
#pragma warning (disable:4786)
// 标准库头文件
#include <string>
#include <vector>
#include <list>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
class info
{
public:
info(const string &a,
const string &b,
const string &c,
const string &d):strTime(a), strType(b), strIP(c), strQuantity(d){}
string strTime;
string strType;
string strIP;
string strQuantity;
bool operator < (const info &m)const {
return strIP< m.strIP;
}
};
vector <info> listinfo;
//vector <info>::iterator listit;
void main(int argc, char ** argv)
{
string file_name;
cout << "please enter file name: ";
cin >> file_name;
ifstream infile( file_name.c_str(), ios::in );
ofstream outfile ("liuran.txt", ios_base::binary);
// const string ip = "IP:Port\t\t\t";
// const string start = "Start\t\t\t";
// const string end = "Stop\t\t\t";
// const string Throughput = "Throughput\t";
// const string arv = "Arverage\r\n";
// outfile.write(ip.c_str(), ip.size());
// outfile.write(start.c_str(), start.size());
// outfile.write(end.c_str(), end.size());
// outfile.write(Throughput.c_str(), Throughput.size());
// outfile.write(arv.c_str(), arv.size());
outfile.close();
outfile.open("liuran.txt", ios_base::app);
if ( !infile ) {
cerr << "oops! unable to open file "
<< file_name << " -- bailing out!\n";
exit( - 1 );
}
else
cout << "\n";
unsigned int lineNo = 1;
unsigned int i = 0;
string textline;
string strLastTime;
while ( getline( infile, textline, '\n' ))
{
cout << lineNo << endl;
lineNo++;
if (textline.find("BEAR") != -1 && (textline.find("ACPT") != -1 || textline.find("QUIT") != -1))
{
strLastTime = textline.substr(1, 19);
string strTime = textline.substr(1, 19);
textline = textline.erase(0, 45);
string strType = textline.substr(0, 9);
textline = textline.erase(0, 10);
string strIP = textline.substr(0, textline.find_first_of("\t"));
string strQuantity = textline.substr(textline.find_last_of("\t") + 1, textline.size());
info classinfo(strTime, strType, strIP, strQuantity);
//vecinfo.push_back(classinfo);
if (strType.find("ACPT") != -1)
{
listinfo.push_back(classinfo);
}
else if (strType.find("QUIT") != -1)
{
for (vector <info>::iterator listit = listinfo.begin();
listit != listinfo.end();
listit++)
{
if (!(*(listit)).strIP.compare(strIP))
{
i++;
cout << i << endl;
//////////////////////////process start///////////////////////////
unsigned int starthour = atoi((*(listit)).strTime.substr(11, 12).c_str());
unsigned int startmin = atoi((*(listit)).strTime.substr(14, 15).c_str());
unsigned int startsec = atoi((*(listit)).strTime.substr(17, 18).c_str());
string strIPTemp = (*(listit)).strIP;
string strTimeTemp = (*(listit)).strTime;
unsigned int endhour = atoi(strTime.substr(11, 12).c_str());
unsigned int endmin = atoi(strTime.substr(14, 15).c_str());
unsigned int endsec = atoi(strTime.substr(17, 18).c_str());
unsigned int totlesec = 0;
if (starthour <= endhour)
{
totlesec = endhour * 3600 + endmin * 60 + endsec
- starthour * 3600 - startmin * 60 - startsec;
}
else if (starthour > endhour)
{
totlesec = (endhour + 24) * 3600 + endmin * 60 + endsec
- starthour * 3600 - startmin * 60 - startsec;
}
unsigned int arv = 0;
if (totlesec != 0)
{
arv = atoi(strQuantity.c_str()) * 8 / totlesec / 1000;
}
char buf[20];
string strarv = itoa(arv, buf, 10);
char buf2[20];
string strTotleSec = itoa(totlesec, buf2, 10);
strIPTemp += "\t";
outfile.write(strIPTemp.c_str(), strIPTemp.size());
strTimeTemp += "\t";
outfile.write(strTimeTemp.c_str(), strTimeTemp.size());
string strTime2Temp = strLastTime;
strTime2Temp += "\t";
outfile.write(strTime2Temp.c_str(), strTime2Temp.size());
string strQuantityTemp = strQuantity + "B\t";
outfile.write(strQuantityTemp.c_str(), strQuantityTemp.size());
strTotleSec += "s\t";
outfile.write(strTotleSec.c_str(), strTotleSec.size());
strarv += "Kbps\n";
outfile.write(strarv.c_str(), strarv.size());
//////////////////////////process end ///////////////////////////
listinfo.erase(listit);
break;
}
}
}
}
}
//////////////////////////////process the vector remains/////////////////////////////////
unsigned int lasthour = atoi(strLastTime.substr(11, 12).c_str());
unsigned int lastmin = atoi(strLastTime.substr(14, 15).c_str());
unsigned int lastsec = atoi(strLastTime.substr(17, 18).c_str());
cout << lasthour << ":" << lastmin << ":" << lastsec << endl;
for (vector <info>::iterator listit = listinfo.begin();
listit != listinfo.end();
listit++)
{
unsigned int starthour = atoi((*(listit)).strTime.substr(11, 12).c_str());
unsigned int startmin = atoi((*(listit)).strTime.substr(14, 15).c_str());
unsigned int startsec = atoi((*(listit)).strTime.substr(17, 18).c_str());
string strIPTemp = (*(listit)).strIP;
string strTimeTemp = (*(listit)).strTime;
unsigned int totlesec = 0;
if (starthour <= lasthour)
{
totlesec = lasthour * 3600 + lastmin * 60 + lastsec
- starthour * 3600 - startmin * 60 - startsec;
}
else if (starthour > lasthour)
{
totlesec = (lasthour + 24) * 3600 + lastmin * 60 + lastsec
- starthour * 3600 - startmin * 60 - startsec;
}
char buf[20];
string strTotleSec = itoa(totlesec, buf, 10);
strIPTemp += "\t";
outfile.write(strIPTemp.c_str(), strIPTemp.size());
strTimeTemp += "\t";
outfile.write(strTimeTemp.c_str(), strTimeTemp.size());
string strTime2Temp = strLastTime;
strTime2Temp += "\t";
outfile.write(strTime2Temp.c_str(), strTime2Temp.size());
string strQuantityTemp = "0B\t";
outfile.write(strQuantityTemp.c_str(), strQuantityTemp.size());
strTotleSec += "s\t";
outfile.write(strTotleSec.c_str(), strTotleSec.size());
string strarv = "0Kbps\t";
outfile.write(strarv.c_str(), strarv.size());
string strFlag = "U\n";
outfile.write(strFlag.c_str(), strFlag.size());
}
outfile.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -