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

📄 debug7dlg.cpp

📁 大学本科的课程设计
💻 CPP
字号:
// debug7Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "debug7.h"
#include "debug7Dlg.h"
#include <sstream>
#include <fstream.h>
#include <string>
#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_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()

/////////////////////////////////////////////////////////////////////////////
// CDebug7Dlg dialog

CDebug7Dlg::CDebug7Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDebug7Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDebug7Dlg)
	m_RFileName = _T("");
	m_FileType = _T("");
	m_FileSize = _T("");
	m_Notes = _T("");
	m_Blanks = _T("");
	m_Lines = _T("");
	m_Codes = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDebug7Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDebug7Dlg)
	DDX_CBString(pDX, IDC_FILEPATH, m_RFileName);
	DDV_MaxChars(pDX, m_RFileName, 100);
	DDX_Text(pDX, IDC_FILETYPE, m_FileType);
	DDV_MaxChars(pDX, m_FileType, 100);
	DDX_Text(pDX, IDC_FILESIZE, m_FileSize);
	DDV_MaxChars(pDX, m_FileSize, 100);
	DDX_Text(pDX, IDC_NOTES, m_Notes);
	DDV_MaxChars(pDX, m_Notes, 100);
	DDX_Text(pDX, IDC_BLANKS, m_Blanks);
	DDV_MaxChars(pDX, m_Blanks, 100);
	DDX_Text(pDX, IDC_LINES, m_Lines);
	DDV_MaxChars(pDX, m_Lines, 100);
	DDX_Text(pDX, IDC_CODES, m_Codes);
	DDV_MaxChars(pDX, m_Codes, 100);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDebug7Dlg, CDialog)
	//{{AFX_MSG_MAP(CDebug7Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SCAN, OnScan)
	ON_BN_CLICKED(IDSTAT, OnStat)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDebug7Dlg message handlers

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

void CDebug7Dlg::OnScan() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CFileDialog filedlg(true);

	static char BASED_CODE szFilter[]//设置文件类型过滤
	= "C++ Files (.c;.cpp;.cxx;.tli;.h;.tlh)\0*.c;*.cpp;*.h\0\
	C++ Source Files(.c;.cpp;.cxx;.tli)\0*.c; *.cpp\0\
	C++ Include Files(.h;.hpp;.hxx;.inl;.tlh)\0*.h\0\
	Java Source Files(.java)\0*.java\0\
	PHP Source Files(.php)\0*.php\0\
	Text File(.txt)\0*.txt\0\
	All Files (*.*)\0*.*\0\0";

	filedlg.m_ofn.lpstrFilter = szFilter;//加入文件打开对话框;

	if(filedlg.DoModal()==IDOK)
	{
		m_RFileName=filedlg.GetPathName();
		UpdateData(false);
	}
}

void CDebug7Dlg::OnStat() 
{
	// TODO: Add your control notification handler code here
	LPCTSTR pFileName;
	CFile file;

	char s[1024];
	std::string s1,s2;
	CString path;
	int i,j,k,Lines,Blanks,Notes,Codes,FileSize;
	bool flag1=false,flag2=false;
	if(m_RFileName=="")
	{
		MessageBox("必须输入文件名",MB_OK);
		return;
	}
	pFileName=(LPCTSTR)m_RFileName;
	TRY
	{
		file.Open(pFileName,CFile::modeRead);
	}
	
	CATCH(CFileException,e)
	{
#ifdef _DEBUG
		afxDump<<"File could not be opened"<<e->m_cause<<"\n";
#endif
	}
	END_CATCH
	//求文件大小和文件类型
	path=file.GetFilePath();
	m_FileType=file.GetFileName();
	i=0;j=0;
	while(i<m_FileType.GetLength() && m_FileType.GetAt(i)!='.')i++;

	if(m_FileType.GetAt(i)!='.')
	{
		MessageBox("请给文件名加后缀!",MB_OK);
		file.Close(); 
		return;
	}
	i++;
	while(i<m_FileType.GetLength())
	{
		s[j++]=m_FileType.GetAt(i++);
	}
	s[j]='\0';
	strcat(s,"  文件");
	m_FileType=s;
	m_FileType.MakeUpper();
	FileSize=file.GetLength();
	itoa(FileSize,s,10);
	m_FileSize=s;
	m_FileSize+="字节";
	file.Close();
	ifstream cin(path);
	//file.SeekToBegin();

	Lines=Blanks=Notes=Codes=0;

	//开始统计
	//std::istringstream istr(ReadBuf);//字符串输入流
	
	while(memset(s,0,sizeof(s)),cin.getline(s,1024,'\n'))
	{
		//MessageBox(s);
		Lines++;
		if(strlen(s)==0) Blanks++;
		i=j=k=0;
		while(k<strlen(s)-1)
		{
			if(flag1)
			{
				s1=s;
				i=s1.find("*/",k);
				if(i) 
				{
					flag1=false;
					Notes++;
					k=s1.find(";",k);
					if(k<i) Codes++;
				}
			}
			else if(flag2)//在'\n'此之前有'//'
			{
				flag2=false;
				Notes++;
				Codes++;
				break;
			}
			else
			{
				s1=s2=s;
				i=s1.find("/*",k);
				j=s2.find("//",k);
				if(i>=0 && j>=0)// '//' 与 '/*' 兼有
				{
					if(i<j)// '/*'在前
					{
						flag1=true;
						k=i+2;
					}
					else // '//'在前
					{
						flag2=true;
						k=j+2;
					}
				}
				else if(i>=0)//只有 '/*'
				{
					flag1=true;
					k=i+2;
				}
				else if(j>=0)//只有 '//'
				{
					flag2=true;
					k=j+2;
				}
				else //没有注释
				{
					if(k<strlen(s)-1)
					Codes++;
					break;
				}
			}
		}	
	}// 如果到达文件结尾则中止循环

	itoa(Lines,s,10);
	m_Lines=s;
	m_Lines+="行";

	itoa(Blanks,s,10);
	m_Blanks=s;
	i=Blanks*100/Lines;
	itoa(i,s,10);
	m_Blanks+="行(";
	m_Blanks+=s;
	m_Blanks+="%)";

	itoa(Notes,s,10);
	m_Notes=s;
	i=Notes*100/Lines;
	itoa(i,s,10);
	m_Notes+="块(";
	m_Notes+=s;
	m_Notes+="%)";

	itoa(Codes,s,10);
	m_Codes=s;
	i=Codes*100/Lines;
	itoa(i,s,10);
	m_Codes+="行(";
	m_Codes+=s;
	m_Codes+="%)";

	UpdateData(false);
}

⌨️ 快捷键说明

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