//200090513_polymerge.cpp
//ES
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <iomanip.h>
#include <string.h>
#include <stdlib.h>
#define n 100
typedef int elem;
typedef struct
{
FILE *f;
elem buffer;
}tape;
int size=sizeof(elem);
int ss(int x, int y)
{
return x-y;
}
//mo tape
void opentape(tape &X, char *filename)
{
X.f=fopen(filename, "rb");
fread(&X.buffer,size, 1,X.f);
}
//doc tape
void readtape(tape &X, elem &x)
{
memcpy(&x,&X.buffer,size);
fread(&X.buffer,size,1,X.f);
}
//Chep 1 phan tu
void copyitemp(tape &X, tape &Y, int &EOR, elem &x, int (*comp)(elem, elem))
{
readtape(X,x);
fwrite(&x,size,1,Y.f);
EOR=feof(X.f)||comp(x,X.buffer)>0;
}
//Chep 1 run
void copyrun(tape &X, tape &Y, elem &x, int (*comp)(elem, elem))
{
int EOR;
do
{
copyitemp(X,Y,EOR,x,comp);
}while(!EOR);
}
//Chon file dich
void filedich(int a[], int d[], int &level, int &j)
{
int z,i;
if(d[j]<d[j+1])
j++;
else
{
if(d[j]==0)
{
z=a[n-2];//??
for(i=0; i<n-2; i++)
{
d[i]=a[i+1]+z-a[i];
a[i]=a[i+1]+z;
}
d[n-2]=z-a[n-2];
a[n-2]=z;
level++;
}
j=0;
}
d[j]--;
}
void polyphase(char *filename, int (*comp)(elem, elem))
{
int a[n], d[n], t[n], ta[n], mx, k, i, j,level,tt,dt,z,EOR;
elem last[n], x;
char s[n][20],s1[20];
tape f[n],F;
for(i=0; i<n; i++)
{
strcpy(s[i],"TMP.");
itoa(i,s1,10);
strcat(s[i],s1);
}
//------Phan phoi ban dau--------------
opentape(F,filename);
for(i=0; i<n-1; i++)
{
f[i].f=fopen(s[i],"wb");
a[i]=d[i]=1;
}
d[n-1]=0;
level=1;
j=0;
do
{
filedich(a,d,level,j);
copyrun(F,f[j],last[j],comp);
}while(!feof(F.f) && (j<n-2));
while(!feof(F.f))
{
filedich(a,d,level,j);
if(comp(last[j],F.buffer)>0)
copyrun(F,f[j],last[j],comp);
else
{
copyrun(F,f[j],last[j],comp);
if(!feof(F.f))
copyrun(F,f[j],last[j],comp);
else
d[j]++;
}
}
fclose(F.f);
for(i=0; i<n-1; i++)
fclose(f[i].f);
//----------------Tron-------
for(i=0; i<n; i++) //mo file f1,f2...f(n-1) de doc
t[i]=i;//luu y: t[i] chu k phai f[i]
for(i=0; i<n-1; i++) //mo du 1 ten cung k sao
opentape(f[i],s[i]);
//lap //cho den khi level =0
do
{
//mo f(tn) de ghi//sai, mo f[ti]
f[t[i]].f=fopen(s[t[i]],"wb");//???
//z=a[n-1]
z=a[n-2];
//lap //cho den khi z=0;
do
{
//k=0;//ghi nhan so run that
k=0;
//voi i=1..n-1 Thuc hien:
for(i=0; i<n-1; i++)
{
//-neu d[i]>0: d[i]=d[i]-1
//nguoc lai: {k=k+1;ta[k]=ti//ta la mang co run dang tron
if(d[i]>0)
d[i]--;
else
ta[k++]=t[i];//ta[k++]=t[i]//??
}
//neu k=0 thi: d[n]=d[n]+1;
if(k==0)
d[n-1]++;
/*nguoc lai:
-lap cho den khi k=0;*/
else
{
do
{
//min(fta1^,fta2^..ftak^):ftamx^
mx=0;
for(i=1; i<k; i++)
if(comp(f[ta[mx]].buffer,f[ta[i]].buffer)>0)
mx=i;
//Chep 1 phan tu tu ta[mx] vao t[n](EOR)
copyitemp(f[ta[mx]],f[t[n-1]],EOR,x,comp);
/*Neu EOR thi: tamx=tak
k=k-1*/
if(EOR)
ta[mx]=ta[--k];//????
}while(k>0);
//--------=> Tron xong 1 bo run---------
z--;
}
}while(z>0);
//dong ftn-1,ftn
fclose(f[t[n-2]].f);
fclose(f[t[n-1]].f);
//-----------------Chuyen muc--------------
z=a[n-2];
tt=t[n-1];
dt=d[n-1];
//voi i=n-1...1 thuc hien
for(i=n-2;i>=0;i--)
{
a[i+1]=a[i]-z;
t[i+1]=t[i];
d[i+1]=d[i];
}
a[0]=z;
t[0]=tt;
d[0]=dt;
level--;
//mo ft1 de doc
opentape(f[t[0]],s[t[0]]);
}while(level>0);
//mo F de ghi
F.f=fopen(filename,"wb");
opentape(f[t[0]],s[t[0]]);//hoi nay la file dich, gio doi lai la file dau
//Chep 1 run tu ft1 vao F
copyrun(f[t[0]],F,x,comp);
//dong ft1-ftn-1
fclose(F.f);
for(i=0; i<n-1; i++)
fclose(f[t[i]].f);
//don dep
for(i=0; i<n; i++)
remove(s[i]);
}
//tao file ngau nhien
void taofile(char *filename, int h)
{
srand((unsigned)time(NULL));
FILE *f=fopen(filename, "wb");
int i,x;
for(i=0; i<h; i++)
{
x=rand()%1000;
fwrite(&x, size,1,f);
}
fclose(f);
}
//in file
void infile(char *filename)
{
FILE *f= fopen(filename, "rb");
int x;
fread(&x,size,1,f);
while(!feof(f))
{
cout<<x<<" ";
fread(&x,size,1,f);
}
fclose(f);
}
void main()
{
int m;
cout<<"polyphase program"
<<"\nCompleted by Nguyen Thu Huong 20090514"
<<"\n---------------******----------------\n";
cout<<"Nhap so phan tu can tron: ";
cin >>m;
taofile("poly.txt",m);
infile("poly.txt");
cout<<"\n------------------------\n";
cout<<endl;
polyphase("poly.txt",ss);
infile("poly.txt");
cout<<endl;
}
Thứ Tư, 27 tháng 5, 2009
Polyphase Merge(2)
Đây là bài cài đặt của bạn Vũ Âu, mà cũng thật có duyên, mình vô tình vô nhầm 3 cái blog khác nhau của bạn ấy. Bài này dựa sát theo bài cài đặt của thầy Long, mời các bạn xem qua.
Đăng ký:
Đăng Nhận xét (Atom)
Bài đăng phổ biến
-
(ictexpress.com) – Không cần phân vùng ổ cứng, không sợ mất dữ liệu, cài đặt Ubuntu chỉ với 1 click chuột, gỡ bỏ dễ dàng đó là ưu điểm mà ...
-
Đối với bất kỳ dự án nào, việc xây dựng Data Access Layer (DAL) luôn là công việc tốn nhiều công sức và thời gian nhất, nhất là khi phải là...
-
UBND TP.HCM vừa có văn bản xin phép Thủ tướng Chính phủ, Bộ Văn hóa Thể thao Du lịch cho phép bắn pháo hoa tầm cao tại 2 điểm dịp tết Dương ...
-
ThinkTank Đây là game mini đồ họa 3D đẹp mắt, chơi solo và đặc biệt là chơi online, dung lượng nhẹ 9.2 MB link: http://www.mediafire.com/?wb...
-
Đã 29 năm trôi qua kể từ ngày 30/8/1982, ngày hệ thống liên lạc bằng thư điện tử (email) được cấp bản quyền. Hiện có khoảng 3,1 tỉ tà...
-
"Copy/Paste functionality has been disabled." là cái thông báo mà khá nhiều bạn bị dính, lỗi ở đâu? Virus? hay registry lỗi? máy m...
-
Cái Tên sẽ đi theo suốt cuộc đời của người mang nó, ảnh hưởng trực tiếp đến tương lai về sau… Vì vậy các bậc làm cha làm mẹ với bao hoài b...
-
The smart way to learn Office Word 2007one step at a time! Work at your own pace through the easy numbered steps, practice files on CD, hel...
-
No Serial & No Activation Needed | Extracted Size: 3.47GB | Compressed Files for Download 916MB | Genuine version | FULLY UPDATABLE wi...
-
Bạn nhấp vào đường link này để cài đặt ứng dụng và làm theo hướng dẫn bên dưới. Installation and Usage Instructions Installation 1. Install...
Không có nhận xét nào:
Đăng nhận xét