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

📄 genedlg.cpp

📁 用于产生正弦波查表文件里的数据
💻 CPP
字号:
// geneDlg.cpp : implementation file
//

#include "stdafx.h"
#include "gene.h"
#include "geneDlg.h"
#include "math.h"
#include "DlgHlp.h"
#include "DlgAb.h"


#define PI 3.1415926

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGeneDlg dialog

CGeneDlg::CGeneDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGeneDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGeneDlg)
	m_code1 = _T("when X\"");
	m_code2 = _T("\" => data_reg<=X\"");
	m_code3 = _T("\"\;");
	m_sinmax = 255;
	m_sinmin = 0;
	m_sinnum = 256;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGeneDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGeneDlg)
	DDX_Control(pDX, IDC_EDIT_NUM, m_editnum);
	DDX_Control(pDX, IDC_EDIT_MIN, m_editmin);
	DDX_Control(pDX, IDC_EDIT_MAX, m_editmax);
	DDX_Control(pDX, IDC_EDIT3, m_edit3);
	DDX_Control(pDX, IDC_EDIT2, m_edit2);
	DDX_Control(pDX, IDC_EDIT1, m_edit1);
	DDX_Text(pDX, IDC_EDIT1, m_code1);
	DDX_Text(pDX, IDC_EDIT2, m_code2);
	DDX_Text(pDX, IDC_EDIT3, m_code3);
	DDX_Text(pDX, IDC_EDIT_MAX, m_sinmax);
	DDX_Text(pDX, IDC_EDIT_MIN, m_sinmin);
	DDX_Text(pDX, IDC_EDIT_NUM, m_sinnum);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGeneDlg, CDialog)
	//{{AFX_MSG_MAP(CGeneDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_HELP, OnHelp)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGeneDlg message handlers

BOOL CGeneDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGeneDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGeneDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGeneDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGeneDlg::OnOK() 
{
//	retrieve the data from EditBox including code1, code2, code3
//	sinnum, sinmax, sinmin
	UpdateData(TRUE);
//output the data to text file, first we get the filepath of the current execute file
	CString filepath;
	filepath=MY_CURRENT_PATH();
	CString m_filename="Nigel";
	CString m_headline_p,m_dataline_p,m_tempdata;
	m_dataline_p.Format("");
	if (m_sinmax<m_sinmin)
	{
		AfxMessageBox("最大值小于最小值,请重新输入!");
		return;
	}
	if (m_sinnum<1)
	{
		AfxMessageBox("请输入正确的点数!");
		return;
	}
	double m_sinavr,m_sinpeak,m_doublenum;
	m_sinavr=(m_sinmax+m_sinmin)/2;
	m_sinpeak=(m_sinmax-m_sinmin)/2;
	m_doublenum=m_sinnum;
	int i,m_last;
	double m_sin,m_data;
	for (i=0;i<m_sinnum;i++)
	{
		CString temp_No,temp_VALUE;
		m_sin=sin(i*2.000*PI/m_doublenum);
		m_data=m_sin*m_sinpeak+m_sinavr;
		m_last=(int)m_data;
		m_tempdata.Format("\t\t%s%x%s%x%s\r\n",m_code1,i,m_code2,m_last,m_code3);
//		m_tempdata.Format("\t\twhen X\"%x\" => data_reg<=X\"%x\"\;\r\n", i,m_last);
		m_dataline_p=m_dataline_p+m_tempdata;
	}
	SAVE_TO_TEXT(filepath,m_filename,m_dataline_p);

	AfxMessageBox("代码生成成功!");	
	CDialog::OnOK();
}

void CGeneDlg::SAVE_TO_TEXT(CString m_filepath,CString m_filename,CString m_text)
{
	CFile temp_file;
	m_filepath=m_filepath+m_filename+".txt";
	temp_file.Open(m_filepath,CFile::modeCreate|CFile::modeWrite);
//	temp_file.SeekToBegin();
	temp_file.Write(m_text,m_text.GetLength());
	temp_file.Close();
}

CString CGeneDlg::Short2String(unsigned short data)
{
	unsigned short m_temp,tempdate_1,tempdate_2;
	CString s_temp,result="";
	for (int i=0;i<4;i++)
	{
		tempdate_1=0xF000>>(i*4);
		tempdate_2=0x1000>>(i*4);
		m_temp=data&tempdate_1;
		m_temp=m_temp/tempdate_2;
		switch(m_temp)
		{
		case 0x0:s_temp="0";
			break;
		case 0x1:s_temp="1";
			break;		
		case 0x2:s_temp="2";
			break;		
		case 0x3:s_temp="3";
			break;		
		case 0x4:s_temp="4";
			break;			
		case 0x5:s_temp="5";
			break;			
		case 0x6:s_temp="6";
			break;			
		case 0x7:s_temp="7";
			break;			
		case 0x8:s_temp="8";
			break;			
		case 0x9:s_temp="9";
			break;			
		case 0xA:s_temp="A";
			break;	
		case 0xB:s_temp="B";
			break;	
		case 0xC:s_temp="C";
			break;	
		case 0xD:s_temp="D";
			break;
		case 0xE:s_temp="E";
			break;
		case 0xF:s_temp="F";
			break;
		default:AfxMessageBox("error occured");
		    break;
		}
		result=result+s_temp;
	}
	return result;
}

CString CGeneDlg::MY_CURRENT_PATH()
{
	CString tempPath;
	char exeFullPath[MAX_PATH]; 
	GetModuleFileName(NULL,exeFullPath,MAX_PATH); 
	tempPath.Format("%s",exeFullPath); 
	int sstart=0;
	int pathend;
	for (int i=0;i<100;i++)
	{		
		sstart=tempPath.Find("\\",sstart+1);
		if (sstart<0)
		{
			exeFullPath[pathend+1]='\0';
			break;
		}
		pathend=sstart;
	}
//	exeFullPath[FilePath.GetLength()-8]='\0'; 
	tempPath.Format("%s",exeFullPath); 
	return tempPath;
}

void CGeneDlg::OnHelp() 
{
	CDlgHlp dlg_hlp;
	dlg_hlp.DoModal();	
}

void CGeneDlg::OnAbout() 
{
	CDlgAb dlg_ab;
	dlg_ab.DoModal();	
}

⌨️ 快捷键说明

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