블로그 이미지
Kanais
Researcher & Developer 퍼즐을 완성하려면 퍼즐 조각들을 하나 둘씩 맞춰나가야 한다. 인생의 퍼즐 조각들을 하나 둘씩 맞춰나가다 보면 인생이란 퍼즐도 완성되는 날이 오려나...?

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

05-03 16:16

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 5. 11. 11:23 Programming/MFC

작성날짜    : 2011-03-16


목차

  l      도형분석프로그램은?

l      Class Diagram

l      함수 설명

l      구현

 

 

도형분석프로그램은?

ComboBox에서 도형을 선택하고, 도형의 Left, Top, Right, Bottom 값을 입력한 후 도형의 면적을 구하는 프로그램이다.

 

Class Diagram

 


 

함수 설명

CFile 함수(자세한 사용방법은 추후에 수정하겠음)

MFC는 CFile (파생) 클래스를 통해 범용의 파일 입출력 기능을 제공한다. CFile 클래스는 그 자체로도 유용한 파일 입출력 기능을 제공하지만 파생 클래스에 공통의 인터페이스를 제공하는 중요한 역할도 한다.

CFile 클래스는 MFC 파일 관련 클래스의 최상위 클래스로, 저수준의 파일 입출력 기능을 제공한다. CFile 클래스가 제공하는 핵심적인 입출력 연산은 다음과 같다.

l  파일을 열거나 생성한다(Open)

l  데이터를 읽는다(Read)

l  데이터를 쓴다(Write)

l  입출력 위치를 변경한다(Seek)

l  파일을 닫는다(Close)

 

 

Dialog 


 

도형분석데모Dlg.cpp

헤더

#include "stdafx.h"

#include "도형분석데모.h"

#include "도형분석데모Dlg.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#endif


컨트롤 변수 정의

C도형분석데모Dlg::C도형분석데모Dlg(CWndpParent /*=NULL*/)

        : CDialog(C도형분석데모Dlg::IDDpParent)

        , fname(_T(""))

        , left(0)

        , top(0)

        , right(0)

        , bottom(0)

{

        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void C도형분석데모Dlg::DoDataExchange(CDataExchangepDX)

{

        CDialog::DoDataExchange(pDX);

        DDX_Text(pDXIDE_FNAMEfname);

        DDX_Control(pDXIDC_COMBOc_combo);

        DDX_Text(pDXIDE_LEFTleft);

        DDX_Text(pDXIDE_TOPtop);

        DDX_Text(pDXIDE_RIGHTright);

        DDX_Text(pDXIDE_BOTTOMbottom);

        DDX_Control(pDXIDL_LISTf_list);

}


OnInitDialog() - Dialog초기화

//리스트뷰에칼럼추가

        f_list.InsertColumn(0,TEXT("도형"),LVCFMT_CENTER,67);

        f_list.InsertColumn(1,TEXT("기타"),LVCFMT_LEFT,122);

        //콤보박스에추가

        c_combo.AddString(TEXT("사각형"));

        c_combo.AddString(TEXT("선"));

        c_combo.AddString(TEXT("원"));


OnBnClickedAdd()  추가 버튼 눌렀을 시 설정

        UpdateData(true);

        CString str;

        MyDocument *mDoc = GetDocument();

        if(!mDoc)

        {

               return;

        }

        switch(c_combo.GetCurSel())

        {

        case 0: str = "사각형";        break;

        case 1: str = "선";    break;

        case 2: str = "원";    break;

        default:MessageBox(TEXT("콤보박스를\n\n선택하세요."),TEXT("실"),MB_OK); return;

        }

        int j = mDoc->AddDiagram(c_combo.GetCurSel(),left,top,right,bottom);

        f_list.InsertItem(0,str);

        f_list.SetItemText(0,1,mDoc->GetDiagramInfo(j));


OnBnClickedCre()  생성 버튼 클릭 시 설정

// TODO: 여기에컨트롤알림처리기코드를추가합니다.

        UpdateData(true);

        f_list.DeleteAllItems();

        MyDocument *hDoc = GetDocument();   

        WCHAR sFname[100];

        wsprintf(sFname,TEXT("%s.txt"),fname);

        hDoc->FileCreate(sFname);

        UpdateData(false);


OnBnClickedSave()  저장 버튼 클릭 시 설정

UpdateData(true);

        MyDocument *hDoc = GetDocument();

        WCHAR sFname[100];

        wsprintf(sFname,TEXT("%s.txt"),fname);

        hDoc->FileSave(sFname);

        UpdateData(false);


OnBnClickedSel()  선택 버튼 클릭 시 설정

UpdateData(true);

        f_list.DeleteAllItems();

        MyDocument *hDoc = GetDocument();

        WCHAR sFname[100];

        int buf;

        CString str;

        wsprintf(sFname,TEXT("%s.txt"),fname);

        buf = hDoc->FileLoad(sFname);

 

        c_combo.SetCurSel(buf);

        //c_combo.FindString(buf,str);

        switch(buf)

        {

        case 0: str = "사각형";        break;

        case 1: str = "선";    break;

        case 2: str = "원";           break;

        defaultreturn;

        }

        if(!hDoc)

        {

               return;

        }

        int j = hDoc->AddDiagram(c_combo.GetCurSel(),left,top,right,bottom);

        f_list.InsertItem(0,str);

        f_list.SetItemText(0,0,str);

        f_list.SetItemText(0,1,hDoc->GetDiagramInfo(j));

        UpdateData(false);



Diagram.h

 

class Diagram

{

public:

        Diagram(void);

        virtual ~Diagram(void);

        virtual CString ToString();

        virtual void Serialize(CFile *cf);

        virtual int GetDgmnum();

        virtual double GetArea();

        virtual double GetLine();

};

 

RectangleDiagram.cpp

 

헤더

#include "StdAfx.h"

#include "RectangleDiagram.h"


ToString()

double area = GetArea();

        WCHAR msg[256];

        swprintf(msg,TEXT("면적: %.3f"),area);

        return CString(msg);


GetArea()  면적 구하는 함수

int height = GetHeight(); //높이

        int width = GetWidth();  //

        return height*width;


GetHeight()  높이 구하는 함수

        int height = 0;

        height = bottom - top;

        if(bottom<top)

        {

               height = height * -1;

        }

        return height;


GetWidth()  넓이 구하는 함수

        int width = 0;

        width = right - left;

        if(right<left)

        {

               width = width * -1;

        }

        return width;

 


 

EllipseDiagram.cpp

 

헤더

#include "StdAfx.h"

#include "EllipseDiagram.h"

 


ToString()

        double area = GetArea();

        WCHAR msg[256];

       

        CString str;

        str.Format(TEXT("%.3f"),area); //str.Format(TEXT(""),area)는float나double을String형으로바꿔준다.

 

        wsprintf(msg,TEXT("면적: %s"),str);

        //swprintf(msg,TEXT("타원면적: %.3f"),area);

        return CString(msg);


GetArea()  면적 구하는 함수

        int height = GetHeight();

        int width = GetWidth();

        return height*width*3.14/4;


GetHeight()  높이 구하는 함수

        int height = 0;

        height = bottom - top;

        if(height < 0)

        {

               height = height * -1;

        }

        return height;


GetWidth()  넓이 구하는 함수

        int width = 0;

        width = right - left;

        if(width < 0)

        {

               width = width * -1;

        }

        return width;

 


 

LineDiagram.cpp

 

헤더

#include "StdAfx.h"

#include "LineDiagram.h"

#include <math.h>


ToString()

        double line = GetLine();

        WCHAR msg[256];

        swprintf(msg,TEXT("길이: %.3f"),line);

        return CString(msg);


GetLine()  선 길이 구하는 함수

double line;

        line = sqrt(pow(GetWidth(),(double)2)+pow(GetHeight(),(double)2));

        return line;


GetWidth()  넓이 구하는 함수

        int width = 0;

        width = right - left;

        if(right<left)

        {

               width = width*-1;

        }

        return width;

 


GetHeight()  높이 구하는 함수

int height = 0;

        height = bottom - top;

        if(bottom<top)

        {

               height = height*-1;

        }

        return height;

 

 

MyDocument.cpp

 

헤더

#include "stdafx.h"

#include "도형분석데모.h"

#include "MyDocument.h"


AddDiagram(int num,int left,int top,int right,int bottom)

        Diagram *diagram = 0;

        switch(num)

        {

        case 0: diagram = new RectangleDiagram(num,left,top,right,bottom); break;

        case 1: diagram = new LineDiagram(num,left,top,right,bottom); break;

        case 2: diagram = new EllipseDiagram(num,left,top,right,bottom);  break;

        }

        if(!diagram)

        {

               return 0;

        }

        serial++;

        diagrams[serial] = diagram;

        return serial;


GetDiagramInfo(int index)

        Diagram *diagram = diagrams[index];

        if(diagram == 0)

        {

               return CString("");

        }

        return diagram->ToString();


SetFileName(CString _fname)

        CFile file;

        file.Open(fname,CFile::modeCreate);

        file.Close();


FileSave(CString fname)

        CFile *cf = new CFile(fname,CFile::modeWrite);

        int buf[20];

        double d_buf[20];

        map<int,Diagram *>::iterator seek;

        seek = diagrams.begin();

        for( ; seek != diagrams.end(); seek++)

        {

               if(seek->first)

               {

                       *buf = seek->second->GetDgmnum();

                       if(seek->second->GetArea())

                       {

                              *d_buf = seek->second->GetArea();

                       }

                       else

                       {

                              *d_buf = seek->second->GetLine();

                       }

                       cf->Write(buf, 20);

                       cf->Write(d_buf,20);

               }

        }

        cf->Close();


FileLoad(CString fname)

        CFile *cf = new CFile(fname,CFile::modeRead);

       

        UINT buflen = (UINT)cf->GetLength();

        char *buf = new char[buflen];

        int f_buf = atoi(buf);        // 문자형을정수형으로변환

        cf->Read(&f_bufbuflen);

       

        cf->Close();

        return f_buf;



posted by Kanais