📄 kwic_pipe.cpp
字号:
// kwic_pipe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include<stdlib.h>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
vector<string> out;
HANDLE zwMutex=CreateMutex( //建立临界区
NULL, // default security attributes
FALSE, // initially not owned
NULL);;
void swap(vector<string> &A ,int i,int j);
int comp( string a, string b) ;
void qsort(vector<string> &A,int i,int j);
int partition(vector<string> &A,int l,int r,string& pivot);
DWORD WINAPI ShiftThread(LPVOID );//移动线程
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
fstream inf("C:\\kwic.txt",ios::in);//输入为C:\in.txt
if(!inf)
{
cerr<<"C:\\in.txt 打开失败"<<endl;
return 1;
}
char *read=new char[512];//readbuffer
HANDLE hThread;
DWORD dwThreadId;
int i;
vector<HANDLE> vHANDLE;
CString sTmp;
std::string str;
//input
while(inf.getline(read,512)) //按行读取数据
{
//Circular shift
str=read;
sTmp=str.c_str();
sTmp.TrimRight();
sTmp.TrimLeft();
str=sTmp.GetBuffer(0);
//read=str;
sprintf(read,"%s",sTmp);
hThread=CreateThread(NULL,0, ShiftThread,(LPVOID)read,0,&dwThreadId); //启动线程,移动
if(hThread==NULL)
{
cout<<"CreateThread() failed: "<<GetLastError()<<'\n';
return 1;
}
vHANDLE.push_back(hThread);
read=new char[512];
}
for(i=0;i<vHANDLE.size();i++)//等待所有线程结束
{
WaitForSingleObject(vHANDLE[i],INFINITE);
}
//Alphabetizer,按照字母表排序
qsort(out,0,out.size()-1);//开始排序
inf.close();
//output,输出
fstream outf("C:\\pipeout.txt",ios::out);//输出为C:\out.txt
if(!outf)
{
cerr<<"C:\\out.txt 打开失败"<<endl;
return 1;
}
for(i=0;i<out.size();i++)
outf<<out[i]<<"\n";
cout<<"Success"<<endl;
return 0;
}
return nRetCode;
}
void qsort(vector<string> &A,int i,int j)
{
if(j<=i)
return;
int pivotindex=(i+j)/2;
swap(A,pivotindex,j);
int k=partition(A,i-1,j,A[j]);
swap(A,k,j);
qsort(A,i,k-1);
qsort(A,k+1,j);
}
int partition(vector<string> &A,int l,int r,string& pivot)
{
do
{
while(comp(A[++l],pivot)<0);
while((r!=0)&&comp(A[--r],pivot)>0);
swap(A,l,r);
}while(l<r);
swap(A,l,r);
return l;
}
int comp(string s1, string s2)
{
int i;
s1.erase(remove_if(s1.begin(),s1.end(),ptr_fun(::isspace)),s1.end());
s2.erase(remove_if(s2.begin(),s2.end(),ptr_fun(::isspace)),s2.end());
for(i=0;i<s1.length();i++)
{
if(s1.at(i)>=65 && s1.at(i)<=90)
s1.at(i)=s1.at(i)+32;
}
for(i=0;i<s2.length();i++)
{
if(s2.at(i)>=65 && s2.at(i)<=90)
s2.at(i)=s2.at(i)+32;
}
return s1.compare(s2);
}
void swap(vector<string> &A ,int i,int j)
{
string temp=A[i];
A[i]=A[j];
A[j]=temp;
}
DWORD WINAPI ShiftThread(LPVOID lpParam)
{
string read=(char *)lpParam;
delete lpParam;
vector<string>inVector;
int i=0;
string str="";
while(read[i]!='\0')
{
if(read[i]==' ')
{
inVector.push_back(str);
str="";
}
else
{
str+=read[i];
}
i++;
}
inVector.push_back(str);
for(int j=0;j<inVector.size();j++)
{
string temp="";
for(int k=inVector.size()-1-j;k<inVector.size();k++)
{
temp+=inVector[k]+" ";
}
for(int m=0;m<inVector.size()-1-j;m++)
{
if(m!=inVector.size()-j-2)
temp+=inVector[m]+" ";
else
temp+=inVector[m];
}
WaitForSingleObject(zwMutex,INFINITE);
out.push_back(temp);
ReleaseMutex(zwMutex);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -