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

📄 lpprodlg.cpp

📁 运筹学单纯形算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// LPProDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LPPro.h"
#include "LPProDlg.h"
#include "DataShow.h"
#include <afxtempl.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
typedef struct _Value_R
{
	int index;
	double value;
}Value_R;

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()

/////////////////////////////////////////////////////////////////////////////
// CLPProDlg dialog

CLPProDlg::CLPProDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLPProDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLPProDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	strInputFile=_T("");
	m_Index_method=1;
}

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

BEGIN_MESSAGE_MAP(CLPProDlg, CDialog)
	//{{AFX_MSG_MAP(CLPProDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLPProDlg message handlers

BOOL CLPProDlg::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
	((CButton *)(GetDlgItem(IDC_RADIO1)))->SetCheck(1);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLPProDlg::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 CLPProDlg::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 CLPProDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLPProDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CButton * ptrButton;
	for( int count = IDC_RADIO1; count <= IDC_RADIO3; count++ )
	{
		ptrButton=(CButton *)GetDlgItem(count);
		if( ptrButton->GetCheck()== 1 ) m_Index_method =count-IDC_RADIO1+1;
	}
	CDataShow dlg;
	CString strShow;
	switch (m_Index_method)
	{
	case 1:
		strShow="单纯形法m*n (m= , n= )\r\n目标函数C=[]\r\n向量b=[]\r\n矩阵A=\r\n[]";
		break;
	case 2:
		strShow="对偶单纯形法m*n (m= , n= )\r\n目标函数C=[]\r\n向量b=[]\r\n矩阵A=\r\n[]";
		break;
	case 3:
		strShow="改进单纯形法m*n (m= , n= )\r\n目标函数C=[]\r\n向量b=[]\r\n矩阵A=\r\n[]";
		break;
	}
	dlg.m_strShow=strShow;
	if(dlg.DoModal()) this->strInputFile=dlg.m_strShow;
}

void CLPProDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CString strTmp=strInputFile;
	CString rString;	rString.Empty();
	int i=0;
	int Tag=0;
	int m_num=0,n_num=0;
	double * ptr_C_Array=NULL;
	double * ptr_b_Array=NULL;
	double * ptr_A_Matrix=NULL;
	CString str_VC=_T("");
	CString str_Vb=_T("");
	do
	{
		AfxExtractSubString(rString,strTmp,i);

		if( rString.Find("m*n") !=- 1 )
		{
			int nStart=rString.Find('(');
			int nEnd=rString.Find(')');
			CString str_V=rString.Mid(nStart+1,nEnd-nStart-1);
			
			CString str_m;
			AfxExtractSubString(str_m, str_V, 0, ',');
			str_m.TrimLeft();	 str_m.TrimLeft('m');	str_m.TrimLeft();
			str_m.TrimLeft('='); str_m.TrimLeft();	str_m.TrimRight();
			m_num=atoi(str_m.GetBuffer(5));

			CString str_n;
			AfxExtractSubString(str_n, str_V, 1, ',');
			str_n.TrimLeft();	 str_n.TrimLeft('n');	str_n.TrimLeft();
			str_n.TrimLeft('='); str_n.TrimLeft();	str_n.TrimRight();
			n_num=atoi(str_n.GetBuffer(5));
			
			if(n_num ==0 || n_num==0 ){
				MessageBox("n_num or m_num error! ","Input Error");
				return;
			}
			ptr_C_Array=new double[n_num];
			ptr_b_Array=new double[m_num];
			ptr_A_Matrix=new double[n_num*m_num];
		}
		else if( rString.Find('C')!=-1 && rString.Find('[')!=-1 && rString.Find(']')!=-1 )
		{
			int nStart=rString.Find('[');
			int nEnd=rString.Find(']');
			str_VC=rString.Mid(nStart+1,nEnd-nStart-1);
			int tmpindex=0;
			CString rStr;
			do{
				rStr.Empty();
				AfxExtractSubString(rStr,str_VC,tmpindex,',');
				rStr.TrimLeft();
				if(rStr.IsEmpty()) break;
				ptr_C_Array[tmpindex]=atof(rStr.GetBuffer(20));
				tmpindex++;
			}while(1);
			if( tmpindex != n_num ){
				MessageBox("n_num error in C_Array!","Input Error");
//				delete ptr_C_Array;
				delete ptr_b_Array;
				delete ptr_A_Matrix;
				return;
			}
		}
		else if(rString.Find('b')!=-1 && rString.Find('[')!=-1 && rString.Find(']')!=-1)
		{
			int nStart=rString.Find('[');
			int nEnd=rString.Find(']');
			str_Vb=rString.Mid(nStart+1,nEnd-nStart-1);
			str_Vb.TrimLeft();	str_Vb.TrimRight();
			int tmpindex=0;
			CString rStr;
			do{
				rStr.Empty();
				AfxExtractSubString(rStr,str_Vb,tmpindex,',');
				rStr.TrimLeft();
				if(rStr.IsEmpty()) break;
				ptr_b_Array[tmpindex]=atof(rStr.GetBuffer(20));
				tmpindex++;
			}while(1);
			if( tmpindex != m_num ){
				MessageBox("m_num error in b_Array!","Input Error");
				delete ptr_C_Array;
//				delete ptr_b_Array;
				delete ptr_A_Matrix;
				return;
			}
		}
		else if( rString.Find("矩阵")!=-1 ) Tag=1;
		else if( Tag && rString.Find(',') !=-1 )
		{
			if( Tag > m_num ){
				MessageBox("m_num error in Matrix!","Input Error");
				delete ptr_C_Array;
				delete ptr_b_Array;
//				delete ptr_A_Matrix;
				return; 
			}
			CString str_Matrix=rString;
			str_Matrix.TrimLeft();		str_Matrix.TrimLeft('[');
			str_Matrix.TrimRight();		str_Matrix.TrimRight(']');
			int tmpindex=0;
			CString rStr;
			do{
				rStr.Empty();
				AfxExtractSubString(rStr,str_Matrix,tmpindex,',');
				rStr.TrimLeft();
				if(rStr.IsEmpty()) break;
				ptr_A_Matrix[ (Tag-1)*n_num + tmpindex ]=atof(rStr.GetBuffer(20));
				tmpindex++;
			}while(1);
			if( tmpindex != n_num ){ 
				MessageBox("n_num error in Matrix!","Input Error");
				delete ptr_C_Array;
				delete ptr_b_Array;
//				delete ptr_A_Matrix;
				return;
			}
			Tag++;
		}
		i++;
	}while (!rString.IsEmpty());

	double * ptr_X_Array=new double[n_num];
	for(int icount=0; icount<n_num; icount++)	ptr_X_Array[icount]=0;
	double ret_Value;
	int value=0;
	switch (m_Index_method)
	{
	case 1://单纯形法
		ret_Value=LP_WorkOut(ptr_C_Array,ptr_b_Array,ptr_A_Matrix,m_num,n_num,ptr_X_Array,value);
		break;
	case 2://对偶单纯形法
		ret_Value=LD_WorkOut(ptr_C_Array,ptr_b_Array,ptr_A_Matrix,m_num,n_num,ptr_X_Array,value);
		break;
	case 3://改进单纯形法
		ret_Value=LG_WorkOut(ptr_C_Array,ptr_b_Array,ptr_A_Matrix,m_num,n_num,ptr_X_Array,value);
		break;
	}
	if(value==1){
		if(m_Index_method==1)
		{
			MessageBox("该线性规划为无界解!","线性规划解");
			this->strInputFile+="\r\n\r\n该线性规划解为:\r\n无界解!";
		}
		else if(m_Index_method==2)
		{
			MessageBox("该线性规划无可行解!","线性规划解");
			this->strInputFile+="\r\n\r\n该线性规划解为:\r\n无可行解!";
		}
	}
	else if(value==3)
	{
		//程序陷入循环
		MessageBox("该线性规划可能是陷入循环,请选择Bland法则再试一试!","线性规划解");
	}
	else
	{
		CString rStrTmp,strTmpNum;
		strTmpNum.Empty();	rStrTmp.Empty();
		for(int icount=0; icount<n_num; icount++)
		{
			rStrTmp.Format("%f,",ptr_X_Array[icount]);
			strTmpNum+=rStrTmp;
		}
		strTmpNum.TrimRight(',');
		strTmpNum="X=["+strTmpNum;
		strTmpNum+="]";

		if( value == 0)	strInputFile+="\r\n\r\n该线性规划解为:\r\n"+strTmpNum+"\r";
		else if( value == 2)strInputFile+="\r\n\r\n该线性规划解有无穷多,其中一个解为:\r\n"+strTmpNum+"\r";

		rStrTmp.Format("\nWorkOut=%f",ret_Value);
		strTmpNum+=rStrTmp;

		strInputFile+=rStrTmp;

		MessageBox(strTmpNum,"线性规划解");
	}

	delete ptr_C_Array;
	delete ptr_b_Array;
	delete ptr_A_Matrix;
	delete ptr_X_Array;
	return;	
}

void CLPProDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	
	
	CDataShow dlg;
	dlg.m_strShow=strInputFile;
	dlg.DoModal();
}

⌨️ 快捷键说明

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