📄 wfun.cpp
字号:
}
BOOL CalculateFONTSize(LPLOGFONT plf,BOOL PrintOrScreen)
{
CDC PDC,*pDc;
TEXTMETRIC tm;
CFont Fon,*old;
pDc=&PDC;
BOOL Ret=FALSE;plf->lfWidth=0;
if(Fon.CreateFontIndirect(plf)==TRUE)
{
if(PrintOrScreen==FALSE)AfxGetApp()->CreatePrinterDC(PDC);//创建与打印设相关的DC
else
{
pDc=AfxGetApp()->GetMainWnd()->GetDC();
}
if(pDc->m_hDC!=NULL)
{
old=pDc->SelectObject(&Fon);
pDc->GetTextMetrics(&tm);
pDc->SelectObject(old);
plf->lfHeight=tm.tmHeight;
plf->lfWidth=tm.tmAveCharWidth;
Ret=TRUE;
}
Fon.DeleteObject();
if(PrintOrScreen==FALSE)PDC.DeleteDC();
else AfxGetApp()->GetMainWnd()->ReleaseDC(pDc);
}
return Ret;
}
static char BASED_CODE szFilter_AllFile[] = "全部文件 (*.*) \0*.*\0\0 ";
int FindFileNameOpreation(BOOL LoadOrSave,//为打开或另存
LPSTR asc, //带缓冲区的指针
LPSTR szFilter, //文件过滤器,NULL时为全部文件
int Allow_OutFileNumber,//大于1时,允许打开或另存多文件
CString *FileName_List,//多文件名时个文件名指针列表指针,NULL时只允许单文件
int MaxBufferLength)//asc 最大长度
{
int i;
POSITION pos;
if((i=strlen(asc))>0)
{
if(asc[i-1]=='\\'||asc[i-1]==':')asc[0]='\0';
}
if(szFilter==NULL)szFilter=szFilter_AllFile;
if(FileName_List==NULL)Allow_OutFileNumber=1;
if(Allow_OutFileNumber>8192||Allow_OutFileNumber<1)Allow_OutFileNumber=1;
CFileDialog opf(LoadOrSave,NULL,asc);
if(Allow_OutFileNumber==1)opf.m_ofn.Flags |=OFN_HIDEREADONLY;
else
{
opf.m_ofn.Flags |=(OFN_HIDEREADONLY | OFN_FILEMUSTEXIST| OFN_ALLOWMULTISELECT);
}
opf.m_ofn.nMaxFile=MaxBufferLength;
opf.m_ofn.lpstrFile = asc;
opf.m_ofn.lpstrFilter = szFilter;
if(opf.DoModal()==IDOK)
{
if(Allow_OutFileNumber==1)return 1;
else
{
pos=opf.GetStartPosition();
for(i=0;i<Allow_OutFileNumber&&pos!=NULL;i++)
{
FileName_List[i]=opf.GetNextPathName(pos);
}
return i;
}
}
else return(0);
}
BOOL TestTheFileExist(LPCTSTR filename,
unsigned int minByteN,//小于此最小长度,则识为不存在
BOOL OnOff)//不存在时可报警
{
CFile f;
unsigned int l;
CString err;
if(f.Open(filename,CFile::modeRead)==TRUE)
{
l=f.SeekToEnd();
f.Close();
if(l>minByteN)return(TRUE);
if(OnOff==TRUE)
{
MessageBeep(0xffffffff);
err.Format(_T("此文件 <%s> 长度为 %u 小于 %u"),filename,l,minByteN);
(AfxGetApp()->m_pMainWnd)->MessageBox((LPCTSTR)err,NULL,MB_ICONWARNING|MB_OK);
}
return(FALSE);
}
if(OnOff==TRUE)
{
MessageBeep(0xffffffff);
err.Format(_T("此文件 <%s> 不存在"),filename);
(AfxGetApp()->m_pMainWnd)->MessageBox((LPCTSTR)err,NULL,MB_ICONWARNING|MB_OK);
}
return(FALSE);
}
void DialogSetColor(COLORREF &Color)
{
CColorDialog *Colorcp=new CColorDialog(Color);
if(Colorcp->DoModal()==IDOK)
{
Color=Colorcp->GetColor();
}
delete Colorcp;
}
BOOL RunDosProgram(CWnd *lpW,LPCTSTR lpCmdLine,UINT uCmdShow)
{
UINT ReCode;
ReCode=WinExec(lpCmdLine,uCmdShow);
if(ReCode<32)
{
if(system(lpCmdLine)==0)return TRUE;//system是不推荐的方法.
MessageBeep(0xffffffff);
if(ReCode==0)
{
lpW->MessageBox(_T("The system is out of memory or resources!"));
}
else if(ReCode==ERROR_BAD_FORMAT)
{
lpW->MessageBox(_T("The .EXE file is invalid \r\n(non-Win32 .EXE or error in .EXE image)!"));
}
else if(ReCode==ERROR_FILE_NOT_FOUND)
{
lpW->MessageBox(_T("The specified file was not found!"));
}
else if(ReCode==ERROR_PATH_NOT_FOUND)
{
lpW->MessageBox(_T("The specified path was not found!"));
}
return FALSE;
}
return TRUE;
}
BOOL RunWinProgram(LPCTSTR lpAplication,LPTSTR lpCmdLine)
{
PROCESS_INFORMATION proInfo;
STARTUPINFO StartInfo;
return CreateProcess(lpAplication,// pointer to name of executable module
lpCmdLine,// pointer to command line string
NULL, // process security attributes
NULL, // thread security attributes
FALSE, // handle inheritance flag
0, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&StartInfo, // pointer to STARTUPINFO
&proInfo);
}
void SequenceArrange(int *lpSequence,//序列的排列,适于短序
int Length,//序列长度
int Mode,//排序方式
int *lpIndexOriginal,//当前序索引原序
int *lpIndexCurrent)//原序索引当前序
{
if(Length<2)return;
int i,j,n;
if(lpIndexOriginal!=NULL||lpIndexCurrent!=NULL)
{
int *ListCp=new int[Length];//内容是当前序索引原序
int *ListArrge=new int[Length];//原序索引当前序
for(i=0;i<Length;i++)ListCp[i]=ListArrge[i]=i;
//排队
for(i=0;i<Length-1;i++)
{
for(j=i+1;j<Length;j++)
{
if((lpSequence[j]<lpSequence[i]&&Mode==SEQUENCE_ARRANGE_ASCENDING)||//从小到大
(lpSequence[j]>lpSequence[i]&&Mode==SEQUENCE_ARRANGE_DESCENDING))//从大到小
{
n=ListCp[i];//和序列同时交换才能保证其对原序的索引
ListCp[i]=ListCp[j];
ListCp[j]=n;
ListArrge[ListCp[i]]=i;
ListArrge[ListCp[j]]=j;
n=lpSequence[i];
lpSequence[i]=lpSequence[j];
lpSequence[j]=n;
}
}
}
if(lpIndexOriginal!=NULL)MoveMemory(lpIndexOriginal,ListCp,Length*sizeof(int));
if(lpIndexCurrent!=NULL)MoveMemory(lpIndexCurrent,ListArrge,Length*sizeof(int));
delete ListCp;
delete ListArrge;
}
else
{
for(i=0;i<Length-1;i++)
{
for(j=i+1;j<Length;j++)
{
if((lpSequence[j]<lpSequence[i]&&Mode==SEQUENCE_ARRANGE_ASCENDING)||//从小到大
(lpSequence[j]>lpSequence[i]&&Mode==SEQUENCE_ARRANGE_DESCENDING))//从大到小
{
n=lpSequence[i];
lpSequence[i]=lpSequence[j];
lpSequence[j]=n;
}
}
}
}
}
CString DistributionOut(int *lpD,int MaxL,int BgTab,int Mode,LPCTSTR lpAsc)
{
CString str,s;
int i,j,bg,ed,Max,p,nHuff,Huff[8];
double a,a1,a2,All,Me,Var,Em,p1,p2;
for(i=0,bg=-1,ed=0,All=0,Max=0;i<MaxL;i++)//去掉两回事头的零
{
if(lpD[i]!=0)
{
if(bg==-1)bg=i;
ed=i;
All+=lpD[i];//求总的统计量
if(lpD[i]>Max)Max=lpD[i];//求最大值
}
}
if(bg<0||All<=0)return s;
if(lpAsc!=NULL)
{
str=lpAsc;
}
if(Mode&_OUT_HUFFMAN_CODING_SEAT)
{
for(i=bg,nHuff=0,p=0,p1=0.5,p2=0.5;i<=ed&&nHuff<8;i++)
{
p+=lpD[i];
if((p/All)>=p1)
{
Huff[nHuff++]=i;
p2/=2;
p1+=p2;
}
}
if(nHuff>0)
{
for(j=0;j<nHuff;j++)
{
s.Format(_T("%4d "),Huff[j]);str+=s;
}
s.Format(_T("\r\n"));str+=s;
}
}
if(Mode&_OUT_ME_AND_VAR)
{
for(i=bg,Me=0;i<=ed;i++)
{//求均值
Me+=((i+BgTab)*lpD[i]/All);
}
s.Format(_T("\r\n均值:%12.6f"),Me);str+=s;
for(i=bg,Var=0;i<=ed;i++)
{//求方差
Var+=((i+BgTab-Me)*(i+BgTab-Me)*lpD[i]/All);
}
Var=sqrt(Var);
s.Format(_T(" 方差:%12.6f"),Var);str+=s;
for(i=bg,Em=0;i<=ed;i++)
{//求方差
if((i+BgTab)>0)Em+=((i+BgTab)*lpD[i]/All);
else Em+=(-(i+BgTab)*lpD[i]/All);
}
s.Format(_T(" 平均偏移:%12.6f"),Em);str+=s;
}
if((Mode&_NOT_OUT_PROPABILITY)==0)
{//输出概率
for(i=bg;i<=ed;i++)
{//输出统计分布
s.Format(_T("\r\n(%5d)|"),i+BgTab);str+=s;
j=lpD[i]*80/Max;
for(;j>0;j--)str+=_T("-");
a=lpD[i]*100;a/=All;
if(Mode&_OUT_HUFFMAN_CODING_SEAT&&nHuff>0)
{
s.Format(_T("|"));str+=s;
for(j=0;j<nHuff;j++)
{
if(i==Huff[j])
{
for(;j>=0;j--)str+=_T("+");
break;
}
}
s.Format(_T("%4d(%4f%%)"),lpD[i],a);str+=s;
}
else
{
s.Format(_T("|%d(%4f%%)"),lpD[i],a);str+=s;
}
}
str+=_T("\r\n");
}
if(Mode&_OUT_DISTRIBUTION)
{//输出分布
for(i=bg,p=0;i<=ed;i++)
{//输出统计分布
s.Format(_T("\r\n(%5d)|"),i+BgTab);str+=s;
p+=lpD[i];
j=(int)(p*80/All);
for(;j>0;j--)str+=_T("-");
a=p*100;a/=All;
a1=lpD[i]*100;a1/=All;
if(i>bg){a2=(lpD[i-1]-lpD[i])*100;a2/=lpD[i-1];}
else a2=1;
s.Format(_T("|%5f%%(%5f%%,%5f%%)"),a,a1,a2);str+=s;
}
str+=_T("\r\n");
}
if(Mode&_OUT_PROPABILITY_LIST)
{//输出概率列表
s.Format(_T("\r\n(%5d)\r\n"),bg+BgTab);str+=s;
for(i=bg;i<=ed;i++)
{//输出统计分布列表
s.Format(_T("%d "),lpD[i]);str+=s;
}
str+=_T("\r\n");
}
return str;
}
CString DistributionOut(double *lpD,int MaxL,int BgTab,int Mode,LPCTSTR lpAsc)
{
CString str,s;
int i,j,bg,ed,nHuff,Huff[8];
double a,a1,a2,All,Me,Var,Em,Max,p,p1,p2;
for(i=0,bg=-1,ed=0,All=0,Max=0;i<MaxL;i++)//去掉两回事头的零
{
if(lpD[i]!=0)
{
if(bg==-1)bg=i;
ed=i;
All+=lpD[i];//求总的统计量
if(lpD[i]>Max)Max=lpD[i];//求最大值
}
}
if(lpAsc!=NULL)
{
str=lpAsc;
}
if(Mode&_OUT_HUFFMAN_CODING_SEAT)
{
for(i=bg,nHuff=0,p=0,p1=0.5,p2=0.5;i<=ed&&nHuff<8;i++)
{
p+=lpD[i];
if((p/All)>=p1)
{
Huff[nHuff++]=i;
p2/=2;
p1+=p2;
}
}
if(nHuff>0)
{
for(j=0;j<nHuff;j++)
{
if(j<nHuff-1){s.Format(_T("%4d %d "),Huff[j],Huff[j+1]-Huff[j]);str+=s;}
else{s.Format(_T("%4d "),Huff[j]);str+=s;}
}
s.Format(_T("\r\n"));str+=s;
}
}
if(Mode&_OUT_ME_AND_VAR)
{
for(i=bg,Me=0;i<=ed;i++)
{//求均值
Me+=((i+BgTab)*lpD[i]/All);
}
s.Format(_T("\r\n均值:%12.6f"),Me);str+=s;
for(i=bg,Var=0;i<=ed;i++)
{//求方差
Var+=((i+BgTab-Me)*(i+BgTab-Me)*lpD[i]/All);
}
Var=sqrt(Var);
s.Format(_T(" 方差:%12.6f"),Var);str+=s;
for(i=bg,Em=0;i<=ed;i++)
{//求方差
if((i+BgTab)>0)Em+=((i+BgTab)*lpD[i]/All);
else Em+=(-(i+BgTab)*lpD[i]/All);
}
s.Format(_T(" 平均偏移:%12.6f"),Em);str+=s;
}
if((Mode&_NOT_OUT_PROPABILITY)==0)
{//输出概率
for(i=bg;i<=ed;i++)
{//输出统计分布
s.Format(_T("\r\n(%5d)|"),i+BgTab);str+=s;
j=(int)(lpD[i]*80/Max);
for(;j>0;j--)str+=_T("-");
a=lpD[i]*100;a/=All;
if(Mode&_OUT_HUFFMAN_CODING_SEAT&&nHuff>0)
{
s.Format(_T("|"));str+=s;
for(j=0;j<nHuff;j++)
{
if(i==Huff[j])
{
for(;j>=0;j--)str+=_T("+");
break;
}
}
s.Format(_T("%4f(%4f%%)"),lpD[i],a);str+=s;
}
else
{
s.Format(_T("|%4f(%4f%%)"),lpD[i],a);str+=s;
}
}
str+=_T("\r\n");
}
if(Mode&_OUT_DISTRIBUTION)
{
for(i=bg,p=0;i<=ed;i++)
{//输出统计分布
s.Format(_T("\r\n(%5d)|"),i+BgTab);str+=s;
p+=lpD[i];
j=(int)(p*80/All);
for(;j>0;j--)str+=_T("-");
a=p*100;a/=All;
a1=lpD[i]*100;a1/=All;
if(i>bg){a2=(lpD[i-1]-lpD[i])*100;a2/=lpD[i-1];}
else a2=1;
s.Format(_T("|%5f%%(%5f%%,%5f%%)"),a,a1,a2);str+=s;
}
str+=_T("\r\n");
}
if(Mode&_OUT_PROPABILITY_LIST)
{//输出概率列表
s.Format(_T("\r\n(%5d)\r\n"),bg+BgTab);str+=s;
for(i=bg;i<=ed;i++)
{//输出统计分布列表
s.Format(_T("%6f "),lpD[i]);str+=s;
}
str+=_T("\r\n");
}
return str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -