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

📄 guassdlg.cpp

📁 高斯消去法解线性方程组
💻 CPP
字号:
// guassDlg.cpp : implementation file
//

#include "stdafx.h"
#include "guass.h"
#include "guassDlg.h"
#include "Cpp1.h"
#include "Cpp2.h"

#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_DIALOG1 };
	CListBox	m_List;
	//}}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)
	virtual void OnOK();
	afx_msg void OnSelchangeList1();
	//}}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)
	DDX_Control(pDX, IDC_LIST1, m_List);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()*/

/////////////////////////////////////////////////////////////////////////////
// CGuassDlg dialog

CGuassDlg::CGuassDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGuassDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGuassDlg)
	m_no = _T("");
	m_xs = _T("");
	m_cs = _T("");
	m_nSex = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGuassDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGuassDlg)
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Text(pDX, IDC_EDIT_n, m_no);
	DDX_Text(pDX, IDC_EDIT3, m_xs);
	DDX_Text(pDX, IDC_EDIT_2n, m_cs);
	DDX_Radio(pDX, IDC_RADIO1, m_nSex);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGuassDlg, CDialog)
	//{{AFX_MSG_MAP(CGuassDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_DOUBLECLICKED(IDC_RADIO1, OnDoubleclickedRadio1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGuassDlg message handlers

BOOL CGuassDlg::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 CGuassDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		
	}
	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 CGuassDlg::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 CGuassDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGuassDlg::OnOK() 
{	
	// TODO: Add extra validation here
	int i=0,n,j,t,m;
	double *a,*b;
	char c[10];
	
	UpdateData();	
    if(m_no.IsEmpty())
	{
		MessageBox("方程组的阶数不能为空");
		return;
	}
	if(m_xs.IsEmpty())
	{
		MessageBox("方程组的系数不能为空");
		return;
	}
	if(m_cs.IsEmpty())
	{
		MessageBox("方程组的常数不能为空");
		return;
	}    
    
	//m_no(方程组的阶数)为CString类型,要将其转化为整型赋给n
	n=atoi(m_no);
	//根据n的值,决定指针a,b申请的空间大小
	if(m_nSex==0)//如果选中了第一个单选按钮即选择高斯消元法解方程组
		a=(double *)malloc(n*n*sizeof(double));
	else if(m_nSex==1)//如果选中的是第二个单选按钮即追赶法解三对角线性方程组
		a=(double *)malloc((3*n-2)*sizeof(double));
	b=(double *)malloc(n*sizeof(double));
    
	int strLength = m_xs.GetLength() + 1;//获取系数文本框的字符串长度
	char *s = new char[strLength];//char指针s申请大小为strLength的字符空间
	strncpy(s, m_xs, strLength);//将CString类型的变量m_xs转化为char*型并赋给s
	m=0;
	for(i=0;i<strLength-1;i++)
	{ 
		j=0;
		t=0;
		//取输入的字符,判断是否为数字或负号或小数点
		if(s[i]>='0'&&s[i]<='9'||s[i]=='-'||s[i]=='.')
		{
			//while循环取出连续的数据符号放入字符数组c中
			while(s[i]>='0'&&s[i]<='9'||s[i]=='-'||s[i]=='.')
			{
				c[j]=s[i];
				if(s[i]=='.')//记录小数点的数目
					t++;
				i++;
				j++;
			}
			if(t>1)//如果小数点的书目超过了一个,则弹出错误提示
			{
				MessageBox("输入小数点有误");
				return;
			}
			c[j]=0;
			a[m]=atof(c);
			CString d;
			d.Format("%lf",a[m]);
			MessageBox(d);
			m++;
		}
		else if(s[i]==' ')//如果遇到空格,就跳过continue之后到循环结束的语句
			continue;
		else //否则就是非法的输入
		{
			MessageBox("输入中包含非法字符");
			return;
		}		
	}
	
	if(m_nSex==0)
	{//如果选中了第一个单选按钮即选择高斯消元法解方程组
		if(m!=n*n)
		{
			MessageBox("系数数目应为"+m_no+"*"+m_no+"个");
			return;
		}
	}
	else if(m_nSex==1)//如果选择用追赶法解三对角方程组
		if(m!=3*n-2)
		{
			int no;
			no=3*n-2;
			CString str;
			str.Format("%d",no);
			MessageBox("系数数目应为"+str+"个");
			return;
		}
    
	int strLen= m_cs.GetLength() + 1;
	char *p = new char[strLen];

	strcpy(p,m_cs);
//	strncpy(p, m_cs, strLen);
	m=0;
	for(i=0;i<strLen;i++)
	{ 
		j=0;
		t=0;
		if(p[i]>='0'&&p[i]<='9'||p[i]=='-'||p[i]=='.'||p[i]==' ')
		{
			while(p[i]>='0'&&p[i]<='9'||p[i]=='-'||p[i]=='.')
			{
				c[j]=p[i];
				if(p[i]=='.')
					t++;
				i++;
				j++;
			}
			if(t>1)
			{
				MessageBox("输入小数点有误");
				return;
			}
			c[j]=0;//取一个完整的数后,要在末尾添加\0表示结束!!
			b[m]=atof(c);
			m++;	
		}
		else
		{
			MessageBox("输入中包含非法字符");
			return;
		}
	}
	if(m!=n)
	{
		MessageBox("常数数目应为"+m_no+"个");
		return;
	}
    
	if(m_nSex==0)//如果选中了第一个单选按钮即选择高斯消元法解方程组
		gauss(n,a,b);
	else if(m_nSex==1)//如果选择用追赶法解三对角方程组
		treede(n,a,b);
	CString str;
	if(m_List.GetCount()>0)
		m_List.ResetContent();
	for(i=0;i<n;i++)
	{
		str.Format("x(%d)=%e\n",i,b[i]);
		m_List.AddString(str);
	}

	free(s);
	free(p);
	free(a);   
	free(b);

//	CDialog::OnOK();
}

/*
void CAboutDlg::OnOK() 
{
	// TODO: Add extra validation here
	CDialog::OnOK();
}

void CAboutDlg::OnSelchangeList1() 
{
	
	// TODO: Add your control notification handler code here
	//	if(IDOK!=dlg.DoModal()) return;	
}
*/

void CGuassDlg::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	
}

void CGuassDlg::OnDoubleclickedRadio1() 
{
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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