- // Pipe KWIC.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include<stdlib.h>
- 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 );//shift线程
- int _tmain(int argc, _TCHAR* argv[])
- {
- fstream inf("C://in.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;
- vector<HANDLE> vHANDLE;
- while(inf.getline(read,512)) //按行读取数据
- {
- 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(int i=0;i<vHANDLE.size();i++)//等待所有线程结束
- {
- WaitForSingleObject(vHANDLE[i],INFINITE);
- }
- qsort(out,0,out.size()-1);//数据都到达后,开始排序
- inf.close();
- fstream outf("C://out.txt",ios::out);//输出为C:/out.txt
- if(!outf)
- {
- cerr<<"C://out.txt不能打开"<<endl;
- return 1;
- }
- for(int i=0;i<out.size();i++)
- outf<<out[i]<<"/n";
- cout<<"Success"<<endl;
- return 0;
- }
- 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 a, string b)
- {
- for(int i=0;i<a.length();i++)
- {
- if(a.at(i)>=65&&a.at(i)<=90)
- a.at(i)=a.at(i)+32;
- }
- for(int i=0;i<b.length();i++)
- {
- if(b.at(i)>=65&&b.at(i)<=90)
- b.at(i)=b.at(i)+32;
- }
- return a.compare(b);
- //如果a>b返回正数,如果a<b,返回负数,相等返回0;
- }
- 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;
- }
Pipe KWIC
最新推荐文章于 2023-11-01 15:36:01 发布