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

📄 2inputdlg.cpp

📁 VC MATLAB 混编示例 ppWizard has created this matlab调用 application for you. This application not only
💻 CPP
字号:
// 2InputDlg.cpp : implementation file
//

#include "stdafx.h"
#include "matlab调用.h"
#include "2InputDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// C2InputDlg dialog

int GetWordPos(int order,char *m_buffer) //order is 0 based
{
	BOOL bBlank=TRUE;
	int  i;
	int  iWordNum=-1;
	for(i=0;i<lstrlen(m_buffer);i++)
	{
		if(m_buffer[i]==' '||m_buffer[i]==0x0d||m_buffer[i]==0x0a)
		{
			bBlank=TRUE;
			continue;
		}
		if(bBlank==TRUE&&!(m_buffer[i]==' '||m_buffer[i]==0x0d||m_buffer[i]==0x0a))
		{
			iWordNum++;
			bBlank=FALSE;
			if(iWordNum==order)
			return i;
		}
		
	}
	return -1;
}
void GetSubWord(int order,char *m_buffer,char *pszSubWord) //order is 0 based
{
	int iIndex=GetWordPos(order,m_buffer);
	int i,j;
	j=0;
	for(i=iIndex,j=0;m_buffer[i]!=' '&&m_buffer[i]!=0x0d&&m_buffer[i]!=0x0a;i++)
	{
		pszSubWord[j]=m_buffer[i];
		j++;
	}
	pszSubWord[j]='\0';
}
void GetArray(double *dArray,char *str,int row,int colum)
{
	int i,j;
	char pszNum[50];
	for(i=0;i<row;i++)
		for(j=0;j<colum;j++)
		{
			GetSubWord(i*colum+j,str,pszNum);
			dArray[i*colum+j]=atof(pszNum);
		}
}
C2InputDlg::C2InputDlg(CWnd* pParent /*=NULL*/)
	: CDialog(C2InputDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(C2InputDlg)
	m_A = _T("");
	m_B = _T("");
	m_Result = _T("");
	//}}AFX_DATA_INIT
}


void C2InputDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(C2InputDlg)
	DDX_Control(pDX, ID_RESULT, m_WndResult);
	DDX_Control(pDX, IDC_EDIT2, m_EditB);
	DDX_Control(pDX, IDC_EDIT1, m_EditA);
	DDX_Control(pDX, IDOK, m_OkButton);
	DDX_Control(pDX, ID_CACULATE, m_Caculate);
	DDX_Text(pDX, IDC_EDIT1, m_A);
	DDX_Text(pDX, IDC_EDIT2, m_B);
	DDX_Text(pDX, ID_RESULT, m_Result);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(C2InputDlg, CDialog)
	//{{AFX_MSG_MAP(C2InputDlg)
	ON_BN_CLICKED(ID_CACULATE, OnCaculate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// C2InputDlg message handlers

void C2InputDlg::OnCaculate() 
{
	// TODO: Add your control notification handler code here
	int i,j;
	int iRowA,iColumA;
	int iRowB,iColumB;
	CIntArray ColumArrayA,ColumArrayB;
	BOOL bStartRow=FALSE;
	BOOL bEndWord=TRUE;
	CString str;
	CString strResult;
	mxArray *prhs[2];
	double *ArrayA,*ArrayB,*ArrayC;
	mxArray *A,*B,*AT,*BT,*C,*CT;
	iRowA=iColumA=iRowB=iColumB=0;
	//-----------------------------------------------------------------------------
	UpdateData(TRUE);
	//------get row,colum of A,B---------------------------------------------------
	if(m_A.GetLength()==0)
	{
		MessageBox("please input A");
		return;
	}
	if(m_B.GetLength()==0)
	{
		MessageBox("please input B");
		return;
	}
	TRACE("a.len=%d\n",m_A.GetLength());
	TRACE("b.len=%d\n",m_B.GetLength());
	for(i=0;i<m_A.GetLength();i++)
	{
		
		if(m_A.GetAt(i)!=' '&&m_A.GetAt(i)!='\n')
			break;
	}
	if(i==m_A.GetLength())
	{
		MessageBox("A is empty ,please input A");
		return;
	}
	for(i=0;i<m_B.GetLength();i++)
	{
		if(m_B.GetAt(i)!=' '&&m_B.GetAt(i)!='\n')
			break;
	}
	if(i==m_B.GetLength())
	{
		MessageBox("B is empty ,please input B");
		return;
	}
	m_A.Insert(0,'\n');
	m_B.Insert(0,'\n');
	if(m_A.GetAt(m_A.GetLength()-1)!='\n')
		m_A=m_A+'\n';
	if(m_B.GetAt(m_B.GetLength()-1)!='\n')
		m_B=m_B+'\n';
	//----------------------------------------------------获取A的行数列数---------
	for(i=0;i<m_A.GetLength();i++)
	{
		char s;
		s=m_A.GetAt(i);
		if(m_A.GetAt(i)==0x0a)
		{
			bStartRow=FALSE;
			continue;
		}
		if(bStartRow==FALSE&&m_A.GetAt(i)!=' '&&m_A.GetAt(i)!=0x0d)
		{
			bStartRow=TRUE;
			iRowA++;
			iColumA=1;
			bEndWord=FALSE;
			ColumArrayA.Add(iColumA);
			continue;
		}
		if(bStartRow)
		{
			if(m_A.GetAt(i)==' ')
			{
				bEndWord=TRUE;
				continue;
			}
			if(bEndWord)
			{
				iColumA++;
				bEndWord=FALSE;
				ColumArrayA.SetAt(iRowA-1,iColumA);
			}
		}
	}
	//------------------------------------------------------获取B的行数列数-----------------------
	for(i=0;i<m_B.GetLength();i++)
	{
		if(m_B.GetAt(i)==0x0a)
		{
			bStartRow=FALSE;
			continue;
		}
		if(bStartRow==FALSE&&m_B.GetAt(i)!=' '&&m_B.GetAt(i)!=0x0d)//开始新行
		{
			bStartRow=TRUE;
			iRowB++;
			iColumB=1;
			bEndWord=FALSE;
			ColumArrayB.Add(iColumB);
			continue;
		}
		if(bStartRow)
		{
			if(m_B.GetAt(i)==' ')
			{
				bEndWord=TRUE;
				continue;
			}
			if(bEndWord)
			{
				iColumB++;
				bEndWord=FALSE;
				ColumArrayB.SetAt(iRowB-1,iColumB);
			}
		}
	}
	
	for(i=0;i<ColumArrayA.GetSize();i++)
	{
		if(iColumA!=ColumArrayA.GetAt(i))
		{
			MessageBox("not every row the same colums in A,\ninput  A again!");
			return;
		}
	}
	for(i=0;i<ColumArrayB.GetSize();i++)
	{
		if(iColumB!=ColumArrayB.GetAt(i))
		{
			MessageBox("not every row has the same colums in B,\ninput Bagain!");
			return;
		}
	}
	//---------------------------------------------------------------
	ArrayA=new double[iRowA*iColumA];
	ArrayB=new double[iRowB*iColumB];
	GetArray(ArrayA,(LPSTR)(LPCTSTR)m_A,iRowA,iColumA);
	GetArray(ArrayB,(LPSTR)(LPCTSTR)m_B,iRowB,iColumB);
	//---------------------------------------------------------------
	A=mxCreateDoubleMatrix(iRowA,iColumA,mxREAL);
	B=mxCreateDoubleMatrix(iRowB,iColumB,mxREAL);

	AT=mxCreateDoubleMatrix(iColumA,iRowA,mxREAL);
	BT=mxCreateDoubleMatrix(iColumB,iRowB,mxREAL);

	memcpy((void *)mxGetPr(AT), (void *)ArrayA, iRowA*iColumA*sizeof(double));
	memcpy((void *)mxGetPr(BT), (void *)ArrayB, iRowB*iColumB*sizeof(double));
	mlxTranspose(1,&A,1,&AT);
	mlxTranspose(1,&B,1,&BT);
	//---------------------------------------------------------------
	switch(m_iIndex)
	{
	case 0:
		if(iColumA!=iColumB||iRowA!=iRowB)
		{
			MessageBox("A 和 B 的维数不匹配!\n请重新输入!");
			break;
		}
		C=mxCreateDoubleMatrix(iRowA,iColumA,mxREAL);
		CT=mxCreateDoubleMatrix(iColumA,iRowA,mxREAL);
		prhs[0]=A;
		prhs[1]=B;
		mlxPlus(1,&C,2, prhs);
		mlxTranspose(1,&CT,1,&C);//从CT里面顺序读取
		ArrayC=new double[iRowB*iColumB];
		memcpy((void *)ArrayC,(void *)mxGetPr(CT) , iRowB*iColumB*sizeof(double));
		strResult.Empty();
		for(i=0;i<iRowA;i++)
		{
			for(j=0;j<iColumA;j++)
			{
				str.Format("%-10.6f ",ArrayC[i*iColumA+j]);
				strResult=strResult+str;
			}
			strResult=strResult+'\n';
		}
		m_WndResult.SetWindowText(strResult);
		delete []ArrayC;
		mxDestroyArray(C);
		mxDestroyArray(CT);
		break;
	case 1:
		if(iColumA!=iColumB||iRowA!=iRowB)
		{
			MessageBox("A 和 B 的维数不匹配!\n请重新输入!");
			break;
		}
		C=mxCreateDoubleMatrix(iRowA,iColumA,mxREAL);
		CT=mxCreateDoubleMatrix(iColumA,iRowA,mxREAL);
		prhs[0]=A;
		prhs[1]=B;
		mlxMinus(1,&C,2, prhs);
		mlxTranspose(1,&CT,1,&C);//从CT里面顺序读取
		ArrayC=new double[iRowB*iColumB];
		memcpy((void *)ArrayC,(void *)mxGetPr(CT) , iRowB*iColumB*sizeof(double));
		strResult.Empty();
		for(i=0;i<iRowA;i++)
		{
			for(j=0;j<iColumA;j++)
			{
				str.Format("%-10.6f ",ArrayC[i*iColumA+j]);
				strResult=strResult+str;
			}
			strResult=strResult+'\n';
		}
		m_WndResult.SetWindowText(strResult);
		delete []ArrayC;
		mxDestroyArray(C);
		mxDestroyArray(CT);
		break;
	case 2:
		if(iColumA!=iRowB)
		{
			MessageBox("A 和 B 的维数不匹配!\n请重新输入!");
			break;
		}
		C=mxCreateDoubleMatrix(iRowA,iColumB,mxREAL);
		CT=mxCreateDoubleMatrix(iColumB,iRowA,mxREAL);
		prhs[0]=A;
		prhs[1]=B;
		mlxMtimes(1,&C,2, prhs);
		mlxTranspose(1,&CT,1,&C);//从CT里面顺序读取
		ArrayC=new double[iRowA*iColumB];
		memcpy((void *)ArrayC,(void *)mxGetPr(CT) , iRowA*iColumB*sizeof(double));
		strResult.Empty();
		for(i=0;i<iRowA;i++)
		{
			for(j=0;j<iColumB;j++)
			{
				str.Format("%-10.6f ",ArrayC[i*iColumB+j]);
				strResult=strResult+str;
			}
			strResult=strResult+'\n';
		}
		m_WndResult.SetWindowText(strResult);
		delete []ArrayC;
		mxDestroyArray(C);
		mxDestroyArray(CT);
		break;
	case 3:
		if(iColumA!=iRowB||iColumA!=iRowA)
		{
			MessageBox("A 和 B 的维数不匹配!\n请重新输入!");
			break;
		}
		C=mxCreateDoubleMatrix(iRowA,iColumB,mxREAL);
		CT=mxCreateDoubleMatrix(iColumB,iRowA,mxREAL);
		prhs[0]=A;
		prhs[1]=B;
		mlxMldivide(1,&C,2, prhs);
		mlxTranspose(1,&CT,1,&C);//从CT里面顺序读取
		ArrayC=new double[iRowA*iColumB];
		memcpy((void *)ArrayC,(void *)mxGetPr(CT) ,iRowA*iColumB*sizeof(double));
		strResult.Empty();
		for(i=0;i<iRowA;i++)
		{
			for(j=0;j<iColumB;j++)
			{
				str.Format("%-10.6f ",ArrayC[i*iColumB+j]);
				strResult=strResult+str;
			}
			strResult=strResult+'\n';
		}
		
		m_WndResult.SetWindowText(strResult);
		delete []ArrayC;
		mxDestroyArray(C);
		mxDestroyArray(CT);
		break;
	default:
		break;
	}
	mxDestroyArray(A);
	mxDestroyArray(B);
	mxDestroyArray(AT);
	mxDestroyArray(BT);
	delete []ArrayA;
	delete []ArrayB;
}

BOOL C2InputDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	switch(m_iIndex)
	{
	case 0:
		SetWindowText("执行加法");
		break;
	case 1:
		SetWindowText("执行减法");
		break;
	case 2:
		SetWindowText("执行乘法");
		break;
	case 3:
		SetWindowText("执行左除");
		break;
	default:
		break;
	}
	return TRUE;
}

⌨️ 快捷键说明

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