⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sindia.cpp

📁 《面向对象程序设计实用教程》一书的源码
💻 CPP
字号:
// SinDia.cpp : implementation file
//

#include "stdafx.h"
#include "cge.h"
#include "SinDia.h"
#include "math.h"
#include "fft.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSinDia dialog


CSinDia::CSinDia(CWnd* pParent /*=NULL*/)
	: CDialog(CSinDia::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSinDia)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CSinDia::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSinDia)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSinDia, CDialog)
	//{{AFX_MSG_MAP(CSinDia)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
	ON_BN_CLICKED(IDC_RADIO5, OnRadio5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSinDia message handlers

void CSinDia::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CDC *pDC=&dc;	
	//CDC *pDC=GetDC();		
	pDC->MoveTo(startx,starty);
	pDC->LineTo(startx,starty+sinheight);//画到坐标原点
	pDC->LineTo(startx+sinwideth,starty+sinheight);	  
	
	f_wnin(1);

	f_dcline(RGB(255,0,0));

	/*
	static int xy; //CPaintDC dc(this); 
	xy++;
	CString s;
	s.Format("%d",xy);
	pDC->TextOut(150,150,s);
	*/
	

	
}

BOOL CSinDia::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	startx=40;
	starty=60;
	sinheight=150;
	sinwideth=430;	
	maxout = 0 ;
	maxout_y = 0 ;
	dtf_w = 128;
	//len = 8192;
	out_len = len/2;	
	lk=0;	
	wnkey = 1;
	m_nFlowXY = -100;
	m_pKey = false;
		
	CheckRadioButton(IDC_RADIO4,IDC_RADIO5,IDC_RADIO4);	
	CheckRadioButton(IDC_RADIO1,IDC_RADIO3,IDC_RADIO1);	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSinDia::OnRadio1() 
{
	
	f_dcline(RGB(192,192,192));
	f_wnin(1);
	f_dcline(RGB(255,0,0));	
		
}

void CSinDia::OnRadio2() 
{
	f_dcline(RGB(192,192,192));
	f_wnin(2);
	f_dcline(RGB(255,255,0));	
}

void CSinDia::f_dcline(int rgb)
{
	CDC *pDC= GetDC();	
	CPen pen;
	pen.CreatePen(PS_SOLID, 1, rgb);	
	CPen* pold = pDC->SelectObject(&pen);
	pDC->MoveTo(startx,starty+sinheight);

	for(int i=0; i<lk; i++) 
	{				
	   pDC->LineTo(startx+i*bx, starty+sinheight-by*outlk[i]);	

	}
	pDC->SelectObject(pold);
	pen.DeleteObject();


}

void CSinDia::OnRadio3() 
{
	f_dcline(RGB(192,192,192));
	f_wnin(5);
	f_dcline(RGB(0,255,255));	

}

void CSinDia::f_wnin(int wkey)
{
	float *in = new float[len];  
	float *outs = new float[len];	
	lk = 0;
	maxout = 0;
	int m = 0;
	int n;
	int x;
	n = len;
	if(wkey == 1)
	{
		
		for(x=0; x<len; x++)
		{
			in[x] = wnin[x];
		}   
	
	
	}
	if(wkey==2)
	{
	
		for(x=0; x<len; x++)
		{
			in[x] = 0.5*(1.0+cos(3.1415926*((x-m)*2.0/(n-m)-1))) * wnin[x];
		}   
	
	
	}
	if(wkey==5)
	{
		
		for(x=0; x<len; x++)
		{
			in[x] = (0.54 + 0.46*cos(3.1415926*((x-m)*2.0/(n-m)-1))) * wnin[x];
		}   
	
	}	
	PowerSpectrum(len, in, outs);  //outs 能量普输出  
	for( int i=0; i<out_len; i++) 
	{	
	  if(!m_pKey)
	  {
		  outs[i] = sqrt(outs[i]); //振幅普		
	  }
	  if(outs[i]>maxout) 
	  {
		  maxout = outs[i];
		  k0 = i;	
	  } 	  
	  
	}

	int kb = k0 - dtf_w;
	int ke = k0 + dtf_w;
	if (kb<=0)
	  for(i=0;i<k0;i++)
	  {
		lk++;
		outlk[lk] = outs[i];
	  }
	else
	  for(i=kb;i<k0;i++)
	  {
		lk++;
		outlk[lk] = outs[i];
	  }
  
	if (ke>=out_len)
	  for(i=k0;i<out_len;i++)
	  {
		lk++;
		outlk[lk] = outs[i];
	  }
	else
	  for(i=k0;i<ke;i++)
	  {
		lk++;
		outlk[lk] = outs[i];
	  }   

	delete []in;
	delete []outs;
	if(wkey == 1)
	{
		maxout_y = maxout;
	}
//	by = sinheight*1.0/maxout;	
	by = sinheight*1.0/maxout_y;	
	bx = sinwideth*1.0/lk;	
	
	f0 = k0/m_dtf/len;	
	w0 = 2*3.1415926*f0;
	//f = k/m_dtf/len;	

	CString s;
	s.Format("%.6f",f0);
	GetDlgItem(IDC_EDIT1)->SetWindowText(s);
	s.Format("%.3e",maxout);
	GetDlgItem(IDC_EDIT2)->SetWindowText(s);
	SetWindowText(m_Fangs);

}

void CSinDia::OnMouseMove(UINT nFlags, CPoint point) 
{
		if(point.x<startx||point.x>startx+sinwideth||point.y<starty||point.y>starty+sinheight)
		{
			return ;
		}
		CDC *pDC = GetDC();
		CPen m_ScrollPen;
		m_ScrollPen.CreatePen(PS_SOLID, 1, RGB(0,255,0));
		CPen* oldpen = pDC->SelectObject(&m_ScrollPen);
		pDC->SetROP2(R2_XORPEN);
		pDC->MoveTo(m_nFlowXY, starty);
		pDC->LineTo(m_nFlowXY, starty+sinheight-2);	
		CBrush brush;
		CRect d_rect(startx-30,starty-30,startx+sinwideth+30,50);
		brush.CreateSolidBrush(RGB(192,192,192));
		pDC->FillRect(d_rect, &brush);
		CString s;
		int k;
		float f;
		if(k0-dtf_w<=0)
		{
			k = 0;
		}else
		{
			k = k0-dtf_w;
		}
		
		k += (point.x - startx) / bx;
		f = k/m_dtf/len;	
		s.Format("%.4f", f );		
		pDC->TextOut(point.x-30, starty-30, s);

		m_nFlowXY = point.x;
		pDC->MoveTo(point.x, starty);
		pDC->LineTo(point.x, starty+sinheight-2);	
		pDC->SetROP2(R2_COPYPEN);
		pDC->SelectObject(oldpen);
		
		m_ScrollPen.DeleteObject();
		brush.DeleteObject();
	
	CDialog::OnMouseMove(nFlags, point);
}

void CSinDia::OnRadio4() 
{
	f_dcline(RGB(192,192,192));
	m_pKey = false;
	f_wnin(1);
	f_dcline(RGB(255,0,0));	
	CheckRadioButton(IDC_RADIO1,IDC_RADIO3,IDC_RADIO1);
	
}

void CSinDia::OnRadio5() 
{
	f_dcline(RGB(192,192,192));
	m_pKey = true;
	f_wnin(1);
	f_dcline(RGB(255,0,0));	
	CheckRadioButton(IDC_RADIO1,IDC_RADIO3,IDC_RADIO1);
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -