?? trainstation.cpp
字號:
// trainStation.cpp : 定義控制臺應用程序的入口點。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <cmath>
#include <atlbase.h>
#include <atlapp.h> // for CTempBuffer
#include <atlenc.h>
#include <atlconv.h>
using namespace std;
struct Station
{
char* bStation;
char* eStation;
char* mStation;
char* strLen;
char* trainNum;
};
struct FileHeader
{
char* strStation;
int filePos;
int rowsNum;
};
string Unicode2Utf8(const wchar_t* pcszToConvert)
{
ATL::CTempBuffer<CHAR, _WTL_STACK_ALLOC_THRESHOLD> buff;
int nLen = ::lstrlenW(pcszToConvert);
if (! nLen)
return string();
int nBuffSize = nLen * 3 + 1; // 發現 * 2 + 1 buffer 大小可能不夠!
CHAR* szUnicodeReturn = buff.Allocate(nBuffSize);
if (szUnicodeReturn != NULL)
{
// WideCharToMultiByte does not support UTF8 in Win98! ::WideCharToMultiByte(CP_UTF8, 0, iBf, nInLen, aBf, nInLen * 3 + 1, NULL, NULL);
int nCnt = ATL::AtlUnicodeToUTF8(pcszToConvert, nLen, szUnicodeReturn, nBuffSize);
if (nCnt != 0)
{
return string(szUnicodeReturn, nCnt);
}
}
return string();
}
void Acsii2Utf8(string& strUtf,const char* strChange)
{
wchar_t* temp=new wchar_t;
int charLen=::MultiByteToWideChar(CP_ACP,0,strChange,-1,NULL,0);
::MultiByteToWideChar(CP_ACP,0,strChange,-1,temp,charLen);
strUtf=Unicode2Utf8(temp);
}
void initStation( Station& fileRecords)
{
fileRecords.bStation=new char;
fileRecords.eStation=new char;
fileRecords.mStation=new char;
fileRecords.strLen=new char;
fileRecords.trainNum=new char;
}
void initFileHeader(FileHeader& indexof)
{
indexof.strStation=new char;
indexof.rowsNum=0;
indexof.filePos=0;
}
int countRowsNum(int posFile,int rowsNum)
{
bool flgPos=true;
bool flgNum=true;
int posLen;
int numLen;
for(int e=1;e<100;e++)
{
if(flgPos==false&&flgNum==false)
{
break;
}
int intOperon=int(pow(10.0,e));
if(flgPos)
{
if(posFile/intOperon==0)
{
flgPos=false;
posLen=e;
}
}
if(flgNum)
{
if(rowsNum/intOperon==0)
{
flgNum=false;
numLen=e;
}
}
}
return numLen+posLen;
}
int _tmain(int argc, _TCHAR* argv[])
{
list<Station> listStation;
list<Station>::iterator next;
/*char* name="周行行";
wchar_t* outstr=new wchar_t;
USES_CONVERSION;
outstr=A2W(name);*/
list<FileHeader> listIndexof;
list<FileHeader>::iterator indexofNext;
int rowsNum=1;
ifstream fileRead("f://a.txt",ios_base::in);
int n=0;
while(!fileRead.eof())
{
n++;
Station Records;
initStation(Records);
fileRead>>Records.bStation>>Records.eStation>>Records.mStation>>Records.strLen>>Records.trainNum;
cout<<Records.bStation<<Records.eStation<<Records.mStation<<Records.strLen<<Records.trainNum<<endl;
if(strcmp(Records.bStation," ")!=0)
{
listStation.push_back(Records);
}
}
fileRead.close();
FileHeader indexof;
int fileLen=0;
int indexofFileLen=0;
for(next=listStation.begin();next!=listStation.end();next++)//算法
{
if(rowsNum==1)
{
initFileHeader(indexof);
strcpy(indexof.strStation,next->eStation);
}
if(strcmp(indexof.strStation,next->eStation)==0)
{
indexof.rowsNum+=1;
//indexof.filePos=fileLen;
}
else
{
int intLen=countRowsNum(indexof.filePos,indexof.rowsNum);
indexofFileLen+=strlen(indexof.strStation)+intLen+4;
listIndexof.push_back(indexof);
initFileHeader(indexof);
strcpy(indexof.strStation,next->eStation);
indexof.rowsNum+=1;
//indexof.filePos=(rowsNum-1)*(strlen(next->bStation)+strlen(next->eStation)+strlen(next->mStation)+strlen(next->strLen)+strlen(next->trainNum)+6);
indexof.filePos=fileLen;
}
fileLen+=strlen(next->bStation)+strlen(next->eStation)+strlen(next->mStation)+strlen(next->strLen)+strlen(next->trainNum)+6;
//cout<<fileLen<<endl;
rowsNum++;
}
indexofFileLen+=strlen(indexof.strStation)+countRowsNum(indexof.filePos,indexof.rowsNum)+4;
listIndexof.push_back(indexof);
//cout<<fileLen;
ofstream fileWrite("f://yes.txt",ios_base::out|ios_base::app);
fileWrite<<indexofFileLen<<'\n';
for(indexofNext=listIndexof.begin();indexofNext!=listIndexof.end();indexofNext++)
{
cout<<indexofNext->strStation<<" "<<indexofNext->filePos<<" "<<indexofNext->rowsNum<<endl;
fileWrite<<indexofNext->strStation<<" "<<indexofNext->filePos<<" "<<indexofNext->rowsNum<<'\n';
}
for(next=listStation.begin();next!=listStation.end();next++)
{
fileWrite<<next->bStation<<" "<<next->eStation<<" "<<next->mStation<<" "<<next->strLen<<" "<<next->trainNum<<'\n';
}
fileWrite.close();
cin.get();
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -