Pipe KWIC

  1. // Pipe KWIC.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <iostream>
  6. #include <fstream>   
  7. #include <string>   
  8. #include <vector>   
  9. #include<stdlib.h> 
  10. using namespace std;
  11. vector<string> out; 
  12. HANDLE zwMutex=CreateMutex(  //建立临界区
  13.         NULL,              // default security attributes
  14.         FALSE,             // initially not owned
  15.         NULL);; 
  16. void swap(vector<string> &A ,int i,int j);
  17. int comp( string a, string b) ;
  18. void qsort(vector<string> &A,int i,int j);
  19. int partition(vector<string> &A,int l,int r,string& pivot);
  20. DWORD WINAPI ShiftThread(LPVOID );//shift线程
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23.     fstream inf("C://in.txt",ios::in);//输入为C:/in.txt
  24.     if(!inf)
  25.     {
  26.         cerr<<"C://in.txt不能打开"<<endl;
  27.         return 1;
  28.     }
  29.     char *read=new char[512];//readbuffer
  30.     HANDLE hThread;
  31.     DWORD dwThreadId;
  32.     
  33.     vector<HANDLE> vHANDLE;
  34.     while(inf.getline(read,512)) //按行读取数据
  35.     {
  36.         
  37.         hThread=CreateThread(NULL,0, ShiftThread,(LPVOID)read,0,&dwThreadId); //启动线程,移位
  38.         if(hThread==NULL)
  39.         {
  40.             cout<<"CreateThread() failed: "<<GetLastError()<<'/n';
  41.             return 1;
  42.         }
  43.         vHANDLE.push_back(hThread);
  44.         read=new char[512];
  45.         
  46.     }
  47.     
  48.     
  49.     for(int i=0;i<vHANDLE.size();i++)//等待所有线程结束
  50.     {
  51.         WaitForSingleObject(vHANDLE[i],INFINITE);
  52.     }
  53.     
  54.     
  55.     qsort(out,0,out.size()-1);//数据都到达后,开始排序
  56.     inf.close();
  57.     fstream outf("C://out.txt",ios::out);//输出为C:/out.txt
  58.     if(!outf)
  59.     {
  60.         cerr<<"C://out.txt不能打开"<<endl;
  61.         return 1;
  62.     }
  63.     for(int i=0;i<out.size();i++)
  64.         outf<<out[i]<<"/n";
  65.     cout<<"Success"<<endl;
  66.     return 0;
  67. }
  68. void qsort(vector<string> &A,int i,int j)
  69. {
  70.     if(j<=i)
  71.         return;
  72.     int pivotindex=(i+j)/2;
  73.     swap(A,pivotindex,j);
  74.     int k=partition(A,i-1,j,A[j]);
  75.     swap(A,k,j);
  76.     qsort(A,i,k-1);
  77.     qsort(A,k+1,j);
  78. }
  79. int partition(vector<string> &A,int l,int r,string& pivot)
  80. {
  81.     do
  82.     {
  83.         while(comp(A[++l],pivot)<0);
  84.         while((r!=0)&&comp(A[--r],pivot)>0);
  85.         swap(A,l,r);
  86.     }while(l<r);
  87.     swap(A,l,r);
  88.     return l;
  89. }
  90. int comp(string a, string b) 
  91.     for(int i=0;i<a.length();i++)
  92.     {
  93.         if(a.at(i)>=65&&a.at(i)<=90)
  94.             a.at(i)=a.at(i)+32;
  95.     }
  96.     for(int i=0;i<b.length();i++)
  97.     {
  98.         if(b.at(i)>=65&&b.at(i)<=90)
  99.             b.at(i)=b.at(i)+32;
  100.     }
  101.     return a.compare(b);
  102.       //如果a>b返回正数,如果a<b,返回负数,相等返回0; 
  103. }  
  104. void swap(vector<string> &A ,int i,int j)
  105. {
  106.     string temp=A[i];
  107.     A[i]=A[j];
  108.     A[j]=temp;
  109. }
  110. DWORD WINAPI ShiftThread(LPVOID lpParam)
  111. {
  112.     string read=(char *)lpParam;
  113.     delete lpParam;
  114.     vector<string>inVector;
  115.     int i=0;
  116.     string str="";
  117.     
  118.     while(read[i]!='/0')
  119.     {
  120.         if(read[i]==' ')
  121.         {
  122.             inVector.push_back(str);
  123.             str="";
  124.         }
  125.         else
  126.         {
  127.             str+=read[i];
  128.             
  129.         }
  130.         
  131.         i++;
  132.     }
  133.     inVector.push_back(str);
  134.         
  135.     for(int j=0;j<inVector.size();j++)
  136.     {
  137.         
  138.         string temp="";
  139.         
  140.         for(int k=inVector.size()-1-j;k<inVector.size();k++)
  141.         {
  142.             temp+=inVector[k]+" ";
  143.         }
  144.         for(int m=0;m<inVector.size()-1-j;m++)
  145.         {
  146.             if(m!=inVector.size()-j-2)
  147.                 temp+=inVector[m]+" ";
  148.             else
  149.                 temp+=inVector[m];
  150.             
  151.         
  152.         }
  153.         WaitForSingleObject(zwMutex,INFINITE);  
  154.         out.push_back(temp);
  155.         ReleaseMutex(zwMutex);
  156.         
  157.         
  158.     }
  159.     
  160.     return 0;
  161. }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值