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

📄 comtestview.cpp

📁 该程序是一个matlab与visual c++ 混合编程的例子
💻 CPP
字号:
// comtestView.cpp : implementation of the CComtestView class
//

#include "stdafx.h"
#include "comtest.h"
#include "mymatrix_idl.h"
#include "comtestDoc.h"
#include "comtestView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CComtestView

IMPLEMENT_DYNCREATE(CComtestView, CView)

BEGIN_MESSAGE_MAP(CComtestView, CView)
	//{{AFX_MSG_MAP(CComtestView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComtestView construction/destruction

CComtestView::CComtestView()
{
	// TODO: add construction code here

}

CComtestView::~CComtestView()
{
}

BOOL CComtestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CComtestView drawing

void CComtestView::OnDraw(CDC* pDC)
{
	CComtestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);


	long ai;



	int kk[8]={1,2,3,4,5,6,7,8},jj[8]={0,0,0,0,0,0,0,0};
double iVal=1.0;
HRESULT hres;
SAFEARRAY * psaiNew; 
SAFEARRAYBOUND aDim[1]; 
aDim[0].lLbound = 0; 
aDim[0].cElements = 8; 
// Equivalent to: Dim aiNew(1 To 8) As Integer.
psaiNew = SafeArrayCreate(VT_R8, 1, aDim);

long xMin = aDim[0].lLbound;
long xMax = xMin + (int)aDim[0].cElements - 1;

for (int x = xMin; x <= xMax; x++) {
    ai = x; 
	if (hres = SafeArrayPutElement(psaiNew, &ai, kk+x)) throw hres;     

	if (hres = SafeArrayGetElement(psaiNew, &ai, jj+x)) throw hres;


   
	//if (hres = SafeArrayGetElement(psaiNew, &ai, &iVal)) throw hres;
        // Equivalent to: aiInOut(x, y) = aiInOut(x, y) + 1.

	

    
}
CString str0;
  str0.Format("cholesky分解结果是:\n%d%d\n%d%d\n", 
        *(jj),*(jj+1),*(jj+2),*(jj+3)); 
  AfxMessageBox(str0);
	   if ((FAILED(CoInitialize(NULL))))
    {
        printf("CoInitialize failed.\n");
        exit(1);
    }

   Imyfactor factor;
   const IID IID_Imyfactor = {0xE4FFCF1E,0x9D05,0x4F45,{0xB4,0xA5,0x1F,0x2C,0xB0,0x8C,0x39,0x59}};
   const IID LIBID_mymatrix = {0x04E59AEF,0xEFEB,0x4838,{0x9E,0xF3,0x6B,0x27,0x4A,0xFE,0x7D,0xA3}};
   const CLSID CLSID_myfactor = {0xFFD6C319,0x5B35,0x4C75,{0x96,0x91,0x2F,0x83,0xCF,0xD8,0x0D,0xD3}};

	factor.CreateDispatch(CLSID_myfactor,NULL);	//创立接口
    COleDispatchDriver(); //连接(此句可以不写)
	VARIANT A,L,U,Q,R,C;
    COleSafeArray aa;
	CString str;
    double a[2][2]={{2.0,-5.0},{-1.0,2.0}};

	 CString str1,str2,str3;

  //任意变量
  VariantInit(&A); //初始化
  VariantInit(&L);
  VariantInit(&U);
  VariantInit(&Q);
  VariantInit(&R);
  VariantInit(&C);


VARIANT vararg[2];
   VariantInit(&vararg[0]);
   V_VT(&vararg[0]) = VT_I4; // 32-bit integer
   V_I4(&vararg[0]) = 5; // here's our 5! 

  A.vt=VT_ARRAY|VT_R8; //类型(数组,双精度型)
    L.vt=VT_ARRAY|VT_R8;
   //V_VT(&A)=VT_R8;
  V_R8(&A)=10.0;
  SAFEARRAYBOUND rgsabound[2]; 
  rgsabound[0].cElements=2; 
  rgsabound[0].lLbound=0; 
  rgsabound[1].cElements=2; 
  rgsabound[1].lLbound=0;
  //创立数组
  A.parray=SafeArrayCreate(VT_R8,2,rgsabound); //创立2维数组
  L.parray=SafeArrayCreate(VT_R8,2,rgsabound);
  U.parray=SafeArrayCreate(VT_R8,2,rgsabound);
  Q.parray=SafeArrayCreate(VT_R8,2,rgsabound);
  R.parray=SafeArrayCreate(VT_R8,2,rgsabound);

  //锁定数组
  SafeArrayLock(A.parray);
  SafeArrayLock(L.parray);
  SafeArrayLock(U.parray);

  long ll[2];
  for(int j=0;j<=1;j++)
  {
	  ll[0]=j;
	  for (int k=0;k<=1;k++)
	  {
		  ll[1]=k;
		  SafeArrayPutElement(A.parray,ll,a+2*j+k);
	  }
  }




  
  //数组传递数据
  //A.parray->pvData=a;
  //double c[2][2];
  factor.SetA(A);
  factor.ludecomp(2,&L,&U); 
  //factor.cholesky(1,&L); 
  //U=factor.GetA();
    for(j=0;j<=1;j++)
  {
	  ll[0]=j;
	  for (int k=0;k<=1;k++)
	  {
		  ll[1]=k;
		  SafeArrayGetElement(U.parray,ll,a+2*j+k);
	  }
  }
  //memcpy(a,L.parray->pvData,4*sizeof(double)); 

  str1.Format("cholesky分解结果是:\n%8.2f%10.2f\n%8.2f%10.2f\n", 
        a[0][0],a[0][1],a[1][0],a[1][1]); 
  AfxMessageBox(str1);
    SafeArrayUnlock(A.parray);
	    SafeArrayUnlock(L.parray);
			    SafeArrayUnlock(U.parray);
/*
  VariantClear(&L); 
  factor.ludecomp(2,&L,&U); 
memcpy(a,L.parray->pvData,4*sizeof(double)); 
memcpy(b,U.parray->pvData,4*sizeof(double)); 
str2.Format("LU分解结果\nL是:\n%8.2f%10.2f\n%8.2f%10.2f\nU是:\n%8.2f%10.2f\n%8.2f%10.2f\n", 
a[0][0],a[0][1],a[1][0],a[1][1],b[0][0],b[0][1],b[1][0],b[1][1]); 
AfxMessageBox(str2); 


factor.qrdecomp(2,&Q,&R); 
memcpy(a,Q.parray->pvData,4*sizeof(double)); 
memcpy(b,R.parray->pvData,4*sizeof(double)); 
str3.Format("QR分解结果\nQ是:\n%8.2f%10.2f\n%8.2f%10.2f\nR是:\n%8.2f%10.2f\n%8.2f%10.2f\n", 
a[0][0],a[0][1],a[1][0],a[1][1],b[0][0],b[0][1],b[1][0],b[1][1]); 
AfxMessageBox(str3); 
*/
  
  factor.DetachDispatch();
  factor.ReleaseDispatch(); 

CoUninitialize(); 

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CComtestView printing

BOOL CComtestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CComtestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CComtestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CComtestView diagnostics

#ifdef _DEBUG
void CComtestView::AssertValid() const
{
	CView::AssertValid();
}

void CComtestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CComtestDoc* CComtestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CComtestDoc)));
	return (CComtestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CComtestView message handlers

⌨️ 快捷键说明

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