📄 operational researchdlg.cpp
字号:
// Operational ResearchDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Operational Research.h"
#include "Operational ResearchDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAXM 100
#define MAXN 100
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int MMAX=0; //矩阵存储
int NMAX=0;
int Matrix[MAXM][MAXN]={0};
int List[MAXN]={0};
int tList[MAXN]={0};
int UMAX=0;
int tUMAX=0;
int max=0;
int min=0;
struct moreCond{ //特别条件
int manA;
int manB;
int cc;
int value;
} cond[100];
int condNum=0;
CString condString="";
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()
/////////////////////////////////////////////////////////////////////////////
// COperationalResearchDlg dialog
COperationalResearchDlg::COperationalResearchDlg(CWnd* pParent /*=NULL*/)
: CDialog(COperationalResearchDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(COperationalResearchDlg)
// 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);
}
void COperationalResearchDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COperationalResearchDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(COperationalResearchDlg, CDialog)
//{{AFX_MSG_MAP(COperationalResearchDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COperationalResearchDlg message handlers
BOOL COperationalResearchDlg::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 COperationalResearchDlg::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 COperationalResearchDlg::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 COperationalResearchDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void COperationalResearchDlg::OnButton1() //确认项目个数、候选人数
{
// TODO: Add your control notification handler code here
CString temp;
CString str="\r\n";
int i;
int j;
GetDlgItem(IDC_EDIT1)->GetWindowText(temp);
MMAX=atoi(temp);
GetDlgItem(IDC_EDIT2)->GetWindowText(temp);
NMAX=atoi(temp);
str+=" ";
for(i=0;i<NMAX;i++){
temp.Format(" 候选人%2d ",i+1);
str+=temp;
}
str+="\r\n\r\n";
for(i=0;i<MMAX;i++){
temp.Format("项目%c%c ",i<=25?' ':'A'+i/26-1,'A'+i%26);
str+=temp;
for(j=0;j<NMAX;j++){
temp.Format("| 0 ");
str+=temp;
}
str+="\r\n";
if(i<MMAX-1){
str+=" ";
for(j=0;j<NMAX;j++){
temp.Format("-----------");
str+=temp;
}
str+="\r\n";
}
}
SetDlgItemText(IDC_EDIT3,str);
}
void COperationalResearchDlg::OnButton3() //添加特别条件
{
// TODO: Add your control notification handler code here
CString temp;
GetDlgItem(IDC_EDIT4)->GetWindowText(temp);
cond[condNum].manA=atoi(temp);
GetDlgItem(IDC_EDIT5)->GetWindowText(temp);
cond[condNum].manB=atoi(temp);
GetDlgItem(IDC_EDIT6)->GetWindowText(temp);
cond[condNum].value=atoi(temp);
cond[condNum].cc=((CButton *)GetDlgItem(IDC_RADIO1))->GetCheck();
temp.Format(" 候选人%2d和候选人%2d一起能%s%d分",cond[condNum].manA,cond[condNum].manB,cond[condNum].cc==1?"加":"减",cond[condNum].value);
condString+=temp;
condString+="\r\n";
SetDlgItemText(IDC_EDIT7,condString);
condNum++;
}
void Check(){ //检查特别条件
int state,i;
for (int k=0;k<condNum;k++){
state=0;
for(i=0;i<MMAX;i++){
if(cond[k].manA==tList[i]) state++;
if(cond[k].manB==tList[i]) state++;
}
if(state==2){
if(cond[k].cc==1) tUMAX+=cond[k].value;
else tUMAX-=cond[k].value;
}
}
}
void Swap(int& a, int& b) //交换函数
{
int temp=a;
a=b;
b=temp;
}
//递归求排列
void Perm1(int templist[], int k, int m)
{
int i;
int j;
if (k==m) {
tUMAX=0;
for(j=0;j<min;j++){
tUMAX+=Matrix[j][tList[j]-1];
}
Check();
if(tUMAX>UMAX) {
UMAX=tUMAX;
for(j=0;j<min;j++)
List[j]=tList[j];
}
}
else //
for (i=k; i <= m; i++) {
Swap (templist[k],templist[i]);
Perm1 (templist,k+1,m);
Swap (templist[k],templist[i]);
}
}
void Perm2(int templist[], int k, int m)
{
int i;
int j;
if (k==m) {
tUMAX=0;
for(j=0;j<max;j++){
if(tList[j]<=min) tUMAX+=Matrix[j][tList[j]-1];
}
Check();
if(tUMAX>UMAX) {
UMAX=tUMAX;
for(j=0;j<max;j++){
if(tList[j]<=min) List[j]=tList[j];
else List[j]=0;
}
}
}
else //
for (i=k; i <= m; i++) {
Swap (templist[k],templist[i]);
Perm2 (templist,k+1,m);
Swap (templist[k],templist[i]);
}
}
void Perm3(int templist[], int k, int m)
{
int i;
int j;
if (k==m) {
tUMAX=0;
for(j=0;j<min;j++){
tUMAX+=Matrix[j][tList[j]-1];
}
Check();
if(tUMAX<UMAX) {
UMAX=tUMAX;
for(j=0;j<min;j++)
List[j]=tList[j];
}
}
else //
for (i=k; i <= m; i++) {
Swap (templist[k],templist[i]);
Perm3 (templist,k+1,m);
Swap (templist[k],templist[i]);
}
}
void Perm4(int templist[], int k, int m)
{
int i;
int j;
if (k==m) {
tUMAX=0;
for(j=0;j<max;j++){
if(tList[j]<=min) tUMAX+=Matrix[j][tList[j]-1];
}
Check();
if(tUMAX<UMAX) {
UMAX=tUMAX;
for(j=0;j<max;j++){
if(tList[j]<=min) List[j]=tList[j];
else List[j]=0;
}
}
}
else //
for (i=k; i <= m; i++) {
Swap (templist[k],templist[i]);
Perm4 (templist,k+1,m);
Swap (templist[k],templist[i]);
}
}
void COperationalResearchDlg::OnButton4() //最大运筹
{
CString temp,str="\r\n";
char s[40000];
GetDlgItem(IDC_EDIT3)->GetWindowText(temp);
sprintf(s,"%s",temp);
int i=-1,j=0,num=0,st=0;
for(int k=0;s[k]!=0;k++){
if(s[k]==' ') continue;
if(!(s[k]>='0'&&s[k]<='9')){
if(st==1){
Matrix[i][j]=num;
j++;
num=0;
if(j==NMAX) {j=0;i++;}
}
st=0;
}
else{
st=1;
num=num*10+s[k]-'0';
}
}
if(st==1) Matrix[i][j]=num;
//计算
max=MMAX>NMAX?MMAX:NMAX;
min=MMAX<NMAX?MMAX:NMAX;
st=MMAX>NMAX?0:1; //0 项目多 1 候选人数多
for(i=0;i<max;i++)
tList[i]=i+1;
if(st) Perm1(tList,0,max-1);
else Perm2(tList,0,max-1);
for(i=0;i<MMAX;i++){
temp.Format("项目 %c%c : 候选人 %d",i<=25?' ':'A'+i/26-1,'A'+i%26,List[i]);
str+=temp;
str+="\r\n";
}
SetDlgItemText(IDC_EDIT9,str);
temp.Format("%d",UMAX);
SetDlgItemText(IDC_EDIT10,temp);
}
void COperationalResearchDlg::OnButton5() //最小运筹
{ CString temp,str="\r\n";
UMAX=1000000;
char s[40000];
GetDlgItem(IDC_EDIT3)->GetWindowText(temp);
sprintf(s,"%s",temp);
int i=-1,j=0,num=0,st=0;
for(int k=0;s[k]!=0;k++){
if(s[k]==' ') continue;
if(!(s[k]>='0'&&s[k]<='9')){
if(st==1){
Matrix[i][j]=num;
j++;
num=0;
if(j==NMAX) {j=0;i++;}
}
st=0;
}
else{
st=1;
num=num*10+s[k]-'0';
}
}
if(st==1) Matrix[i][j]=num;
//计算
max=MMAX>NMAX?MMAX:NMAX;
min=MMAX<NMAX?MMAX:NMAX;
st=MMAX>NMAX?0:1;//0 项目多
for(i=0;i<max;i++)
tList[i]=i+1;
if(st) Perm3(tList,0,max-1);
else Perm4(tList,0,max-1);
for(i=0;i<MMAX;i++){
temp.Format("项目 %c%c : 候选人 %d",i<=25?' ':'A'+i/26-1,'A'+i%26,List[i]);
str+=temp;
str+="\r\n";
}
SetDlgItemText(IDC_EDIT9,str);
temp.Format("%d",UMAX);
SetDlgItemText(IDC_EDIT10,temp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -