MFC实现输入字符,输出摩斯电码
时间: 2024-02-16 21:00:01 浏览: 125
下面是一个简单的 MFC 程序,可以实现输入字符,输出其对应的摩斯电码,您可以参考一下:
```cpp
// MorseCodeDlg.h
#pragma once
#include <map>
class CMorseCodeDlg : public CDialogEx
{
public:
CMorseCodeDlg(CWnd* pParent = nullptr);
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MORSECODE_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX);
DECLARE_MESSAGE_MAP()
private:
std::map<char, CString> m_morseCodeMap;
CString MorseCodeTranslate(const CString& str);
public:
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedTranslateButton();
};
// MorseCodeDlg.cpp
#include "pch.h"
#include "MorseCode.h"
#include "MorseCodeDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CMorseCodeDlg::CMorseCodeDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MORSECODE_DIALOG, pParent)
{
m_morseCodeMap['A'] = ".-";
m_morseCodeMap['B'] = "-...";
m_morseCodeMap['C'] = "-.-.";
m_morseCodeMap['D'] = "-..";
m_morseCodeMap['E'] = ".";
m_morseCodeMap['F'] = "..-.";
m_morseCodeMap['G'] = "--.";
m_morseCodeMap['H'] = "....";
m_morseCodeMap['I'] = "..";
m_morseCodeMap['J'] = ".---";
m_morseCodeMap['K'] = "-.-";
m_morseCodeMap['L'] = ".-..";
m_morseCodeMap['M'] = "--";
m_morseCodeMap['N'] = "-.";
m_morseCodeMap['O'] = "---";
m_morseCodeMap['P'] = ".--.";
m_morseCodeMap['Q'] = "--.-";
m_morseCodeMap['R'] = ".-.";
m_morseCodeMap['S'] = "...";
m_morseCodeMap['T'] = "-";
m_morseCodeMap['U'] = "..-";
m_morseCodeMap['V'] = "...-";
m_morseCodeMap['W'] = ".--";
m_morseCodeMap['X'] = "-..-";
m_morseCodeMap['Y'] = "-.--";
m_morseCodeMap['Z'] = "--..";
m_morseCodeMap['0'] = "-----";
m_morseCodeMap['1'] = ".----";
m_morseCodeMap['2'] = "..---";
m_morseCodeMap['3'] = "...--";
m_morseCodeMap['4'] = "....-";
m_morseCodeMap['5'] = ".....";
m_morseCodeMap['6'] = "-....";
m_morseCodeMap['7'] = "--...";
m_morseCodeMap['8'] = "---..";
m_morseCodeMap['9'] = "----.";
m_morseCodeMap['.'] = ".-.-.-";
m_morseCodeMap[','] = "--..--";
m_morseCodeMap['?'] = "..--..";
m_morseCodeMap['\''] = ".----.";
m_morseCodeMap['!'] = "-.-.--";
m_morseCodeMap['/'] = "-..-.";
m_morseCodeMap['('] = "-.--.";
m_morseCodeMap[')'] = "-.--.-";
m_morseCodeMap['&'] = ".-...";
m_morseCodeMap[':'] = "---...";
m_morseCodeMap[';'] = "-.-.-.";
m_morseCodeMap['='] = "-...-";
m_morseCodeMap['+'] = ".-.-.";
m_morseCodeMap['-'] = "-....-";
m_morseCodeMap['_'] = "..--.-";
m_morseCodeMap['\"'] = ".-..-.";
m_morseCodeMap['$'] = "...-..-";
m_morseCodeMap['@'] = ".--.-.";
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMorseCodeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMorseCodeDlg, CDialogEx)
ON_BN_CLICKED(IDC_TRANSLATE_BUTTON, &CMorseCodeDlg::OnBnClickedTranslateButton)
END_MESSAGE_MAP()
BOOL CMorseCodeDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
return TRUE;
}
CString CMorseCodeDlg::MorseCodeTranslate(const CString& str)
{
CString result;
for (int i = 0; i < str.GetLength(); ++i)
{
char ch = str.GetAt(i);
if (m_morseCodeMap.find(ch) != m_morseCodeMap.end())
{
result += m_morseCodeMap[ch];
result += ' '; // 摩尔斯电码之间用空格隔开
}
}
return result;
}
void CMorseCodeDlg::OnBnClickedTranslateButton()
{
CString str;
GetDlgItemText(IDC_INPUT_EDIT, str);
CString result = MorseCodeTranslate(str);
SetDlgItemText(IDC_OUTPUT_EDIT, result);
}
```
在对话框界面中放置两个编辑框和一个按钮,分别是输入编辑框、输出编辑框和翻译按钮。然后在对话框类中定义一个 `m_morseCodeMap` 成员变量,用来存储字符和摩尔斯电码的对应关系。在构造函数中初始化该变量。在 `MorseCodeTranslate` 方法中,通过遍历输入字符串的每个字符,查找其对应的摩尔斯电码,并将结果字符串返回。在 `OnBnClickedTranslateButton` 方法中,获取输入编辑框的内容,调用 `MorseCodeTranslate` 方法进行翻译,将结果显示在输出编辑框中。
阅读全文