您的位置:首页 > 编程语言 > C语言/C++

C++第十五周【任务1】实现三角形的周长和面积的程序设计

2012-05-30 14:52 483 查看
/*

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生 

* All rights reserved.

* 文件名称:C++第十五周【任务1】                              

* 作    者:   李洪悬                              

* 完成日期:   2012  年  5  月 30 日

* 对任务及求解方法的描述部分

* 输入描述:三角形的三边长

* 问题描述:计算三角形的周长和面积

* 程序输出:三角形的周长和面积

*/

【任务1】在《窗口程序设计及MFC 启蒙》一文案例的基础上,为应用程序增加求周长的功能。在报告中展示主要的代码(至少包括自己定义的代码和包含通过在界面上设置产生的代码——理解文中“进一步的回味”部分。),以及两张用不同输入得到的运行结果的截图。

 

头文件程序代码:
class Triangle
{
public:
Triangle()
{
a=1;
b=1;
c=1;
}
Triangle(double x,double y,double z)
{
a=x;
b=y;
c=z;
}
double area(void);
double girth(void);

private:
double a,b,c;
};


源文件程序代码:
#include"stdafx.h"
#include<Cmath>
#include"MyTriangle.h"
double Triangle::area(void)
{
double s = (a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
double Triangle::girth(void)
{
double l = a+b+c;
return l;
}


额外代码:
void CtriangleDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here

UpdateData();
Triangle t1(m_a,m_b,m_c);
m_area=t1.area();
UpdateData(FALSE);
}

void CtriangleDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
UpdateData();
Triangle t1(m_a,m_b,m_c);
m_girth=t1.girth();
UpdateData(FALSE);
}


 

内部代码:
// triangleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "triangle.h"
#include "triangleDlg.h"
#include "MyTriangle.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

// Implementation
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// CtriangleDlg dialog

CtriangleDlg::CtriangleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CtriangleDlg::IDD, pParent)
, m_a(0)
, m_b(0)
, m_c(0)
, m_area(0)
, m_girth(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CtriangleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_a);
DDX_Text(pDX, IDC_EDIT2, m_b);
DDX_Text(pDX, IDC_EDIT3, m_c);
DDX_Text(pDX, IDC_EDIT4, m_area);
DDX_Text(pDX, IDC_EDIT5, m_girth);
}

BEGIN_MESSAGE_MAP(CtriangleDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CtriangleDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CtriangleDlg::OnBnClickedButton2)
END_MESSAGE_MAP()

// CtriangleDlg message handlers

BOOL CtriangleDlg::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)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
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 CtriangleDlg::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 CtriangleDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CtriangleDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}

void CtriangleDlg::OnBnClickedButton1() { // TODO: Add your control notification handler code here UpdateData(); Triangle t1(m_a,m_b,m_c); m_area=t1.area(); UpdateData(FALSE); } void CtriangleDlg::OnBnClickedButton2() { // TODO: Add your control notification handler code here UpdateData(); Triangle t1(m_a,m_b,m_c); m_girth=t1.girth(); UpdateData(FALSE); }


MFC输出结果:



 经验积累:

不错!不错!这次做的很顺手!内部代码很好!但只能看懂一点!再有MFC比黑方框好看多啦!

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐