课程设计报告--摩尔斯电码发声器
——Morse电码发声器
学号,01091230 姓名,张江涛
学号,01091248 姓名,陆圣鹏
学号,01091282 姓名,彭 力
程序
方案
气瓶 现场处置方案 .pdf气瓶 现场处置方案 .doc见习基地管理方案.doc关于群访事件的化解方案建筑工地扬尘治理专项方案下载
:
0-9和A-Z的摩尔斯电码
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
如下图所示:
将0-9和A-Z的摩尔斯电码制作为一个36行6列的二维数组,如下图所示
当输入0-9或A-Z之间的字符时,程序会根绝具体的字符,从摩尔斯电码表的二维数组中选出对应该字符的一行数组输出,作为该字符的摩尔斯电码。
用一个字节的二进制数代表一个字符的摩尔斯电码,左起第一个1为起始标志(起始标志及之前的0无效)。
起始标志后的0表示点。
起始标志后的1表示划。
如:
A 的报文信息 0000 0101
B 的报文信息 0001 1000
C 的报文信息 0001 1010
按此规律建立一个报文信息表,报文信息表要按 0—9 、A—Z的顺序排列,发送时先查表找字符对应的报文信息,左移报文信息找起始标志,(起始标志及之前的0不发声),然后调用点划子程序发点划声响,发送一个字符循环8次则自动结束。报文信息表同样为一个36行6列的二维数组,如下图所示:
系统细节方案:
点划间隔子程序是通过调用window.h库中的Beep
函
关于工期滞后的函关于工程严重滞后的函关于工程进度滞后的回复函关于征求同志党风廉政意见的函关于征求廉洁自律情况的复函
数是系统发出声音。程
序收到字符后,根据字符对应的摩尔斯电码表中的数组,输出该字符的摩尔斯电
码,并且查找到该字符的01报文信息表,通过两个循环,调用点划子程序,完
成电码的发音功能。
子程序及其流程图:
点声音程序代码: 点声音程序流程图 int dot()//点声音
开始 {
Beep(7000,200);
return 0;
} Beep(7000,200)
点声音
返回0值
结束
划声音程序代码: 划声音程序流程图 int dash()//划声音
{ 开始
Beep(7000,600);
return 0;
}Beep(7000,600)
划声音
返回0值
结束
点划之间间隔程序代码:
开始 int space()//电码间隔声音
{
Beep(0,400);
0; returnBeep(0,400) } 点划之间间隔
返回0值
结束
主程序详解:
程序设计初期,用C++编写了一个控制台应用,先编写了一个电码编译的子程序:
tra(char c)//电码编译 int
{
if(c>=48&&c<=57)
{
cout<
=65&&c<=90)
{
cout<57&&c<65)||c>90)
{
输入格式错误,不是0-9或A-Z之间的字符"<>ch;
if(ch=='c')//输入字符进行编括译
{
cout<<"请输入0-9或A-Z之间的字符"<>c;
tra(c);//调用编码函
Sleep(500);//设置读取字符延迟
}
}
else if(ch=='f')//输入文件路径进行编译
{
cout<<"请输入文件路径"<>filename;
ifstream infile(filename,ios::in);
if(!infile)
{
打开文件错误"< ^args) {
// 在创建任何控件之前启用 Windows XP 可视化效果
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// 创建主窗口并运行它
Application::Run(gcnew Form1());
return 0;
}
Form1.h库中的源代码:
#pragma once
#include
namespace morse {
namespace System; using
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
///
/// Form1 摘要
///
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
protected:
///
/// 清理所有正在使用的资源
///
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::ListBox^ listBox1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Button^ button6;
private: System::Windows::Forms::Button^ button2;
private:
///
/// 必需的设计器变量
///
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
///
/// 设计器支持所需的方法不要
/// 使用代码编辑器修改此方法的内容
///
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->openFileDialog1 = (gcnew
System::Windows::Forms::OpenFileDialog());
this->richTextBox1 = (gcnew
System::Windows::Forms::RichTextBox());
this->button3 = (gcnew System::Windows::Forms::Button());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->button4 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(51, 32);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(65, 12);
this->label1->TabIndex = 0;
>label1->Text = L"请输入字符"; this-
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(53, 49);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 21);
this->textBox1->TabIndex = 1;
this->textBox1->KeyDown += gcnew
System::Windows::Forms::KeyEventHandler(this,
&Form1::textBox1_KeyDown);
//
// listBox1
//
this->listBox1->FormattingEnabled = true;
this->listBox1->ItemHeight = 12;
this->listBox1->Location = System::Drawing::Point(315, 55);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing::Size(120, 184);
this->listBox1->TabIndex = 2;
//
// button1
//
this->button1->Location = System::Drawing::Point(67, 132);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 4;
this->button1->Text = L"发声";
this->button1->UseVisualStyleBackColor = true;
this->button1->MouseClick += gcnew System::Windows::Forms::MouseEventHandler(this,
&Form1::button1_MouseClick);
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(53, 95);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(89, 12);
this->label3->TabIndex = 6;
this->label3->Text = L"输入的字符是:";
//
// label4
//
>label4->AutoSize = true; this-
this->label4->Location = System::Drawing::Point(138, 95);
this->label4->Name = L"label4";
>label4->Size = System::Drawing::Size(0, 12); this-
this->label4->TabIndex = 7;
//
// openFileDialog1
//
this->openFileDialog1->FileName = L"openFileDialog1";
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(12,
256);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(213, 132);
this->richTextBox1->TabIndex = 8;
this->richTextBox1->Text = L"";
//
// button3
//
this->button3->Location = System::Drawing::Point(315, 256);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(74, 25);
this->button3->TabIndex = 9;
this->button3->Text = L"打开文件";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(313, 32);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(29, 12);
this->label2->TabIndex = 11;
this->label2->Text = L"编码";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(55, 238);
this->label5->Name = L"label5";
>label5->Size = System::Drawing::Size(0, 12); this-
this->label5->TabIndex = 12;
//
// button4
//
this->button4->Location = System::Drawing::Point(315, 312);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(75, 23);
this->button4->TabIndex = 13;
this->button4->Text = L"编码";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// button6
//
this->button6->Location = System::Drawing::Point(315, 364);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(75, 23);
this->button6->TabIndex = 15;
this->button6->Text = L"发声";
this->button6->UseVisualStyleBackColor = true;
this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(453, 55);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 16;
this->button2->Text = L"清空";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(539, 416);
this->Controls->Add(this->button2);
this->Controls->Add(this->button6);
>Controls->Add(this->button4); this-
this->Controls->Add(this->label5);
this->Controls->Add(this->label2);
>Controls->Add(this->button3); this-
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->button1);
this->Controls->Add(this->listBox1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"摩尔斯电码发声器";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
int dot()//点声音
{
Beep(7000,200);
return 0;
}
int dash()//划声音
{
Beep(7000,600);
return 0;
}
int space()//电码间隔声音
{
Beep(0,400);
return 0;
}
private: System::Void textBox1_KeyDown(System::Object^ sender,
System::Windows::Forms::KeyEventArgs^ e) //输入字符进行编码
{
const char m[36][6]=//摩尔斯电码表括
{
{"-----"},{".----"},{"..---"},{"...--"},{"....-"},{"....."},
{"-...."},{"--..."},{"---.."},{"----."},{".-"},{"-..."},
{"-.-."},{"-.."},{"."},{"..-."},{"--."},{"...."},
".."},{".---"},{"-.-"},{".-.."},{"--"},{"-."}, {
{"---"},{".--."},{"--.-"},{".-."},{"..."},{"-"},
{"..-"},{"...-"},{".--"},{"-..-"},{"-.--"},{"--.."}
};
if(e->KeyCode==Keys::Enter)
{
label4->Text=textBox1->Text;
for(char*
ch=(char*)(void*)Marshal::StringToHGlobalAnsi(textBox1->Text);*ch;ch+
+)
{
if(*ch>=48&&*ch<=57)
listBox1->Items->Add(gcnew String(m[*ch-48]));
else if(*ch>=65&&*ch<=90)
listBox1->Items->Add(gcnew String(m[*ch-65+10]));
else
listBox1->Items->Add("字符格式错误");
}
}
};
private: System::Void button1_MouseClick(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) //输入字符发出电码声
{
const int n[36][8]=//报文消息表
{
{0,0,1,1,1,1,1,1},{0,0,1,0,1,1,1,1},{0,0,1,0,0,1,1,1},{0,0,1,0,0,0,1,1},{0,0,1,0,0,0,0,1},{0,0,1,0,0,0,0,0},
{0,0,1,1,0,0,0,0},{0,0,1,1,1,0,0,0},{0,0,1,1,1,1,0,0},{0,0,1,1,1,1,1,0},{0,0,0,0,0,1,0,1},{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,1,0},{0,0,0,0,1,1,0,0},{0,0,0,0,0,0,1,0},{0,0,0,1,0,0,1,0},{0,0,0,0,1,1,1,0},{0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0},{0,0,0,1,0,1,1,1},{0,0,0,0,1,1,0,1},{0,0,0,1,0,1,0,0},{0,0,0,0,0,1,1,1},{0,0,0,0,0,1,1,0},
{0,0,0,0,1,1,1,1},{0,0,0,1,0,1,1,0},{0,0,0,1,1,1,0,1},{0,0,0,0,1,0,1,0},{0,0,0,0,1,0,0,0},{0,0,0,0,0,0,1,1},
{0,0,0,0,1,0,0,1},{0,0,0,1,0,0,0,1},{0,0,0,0,1,0,1,1},{0,0,0,1,1,0,0,1},{0,0,0,1,1,0,1,1},{0,0,0,1,1,1,0,0}
};
(char* for
ch=(char*)(void*)Marshal::StringToHGlobalAnsi(textBox1->Text);*ch;ch++)
{
if(*ch>=48&&*ch<=57)
{
int i,j;
for(i=0;i<8;i++)
{
if(n[*ch-48][i]==1)
{
j=i+1;
break;
}
}
for(i=j;i<8;i++)
{
if(n[*ch-48][i]==0)
dot();//调用响铃点函
if(n[*ch-48][i]==1)
dash();//调用响铃划函
space();
}
}
else if(*ch>=65&&*ch<=90)
{
int i,j;
for(i=0;i<8;i++)
{
if(n[*ch-65+10][i]==1)
{
j=i+1;
break;
}
}
for(i=j;i<8;i++)
{
if(n[*ch-65+10][i]==0)
dot();//调用响铃点函数
if(n[*ch-65+10][i]==1)
dash();//调用响铃划函数
space();
}
}
Sleep(300);
}
}
private: System::Void button3_Click(System::Object^ sender,
System::EventArgs^ e) //输入文件打开
{
System::Windows::Forms::DialogResult dlg;
openFileDialog1->InitialDirectory="d:\\";//设置打开文件初始路径
openFileDialog1->Filter="写字板文件t(*.rtf)|*.rtf";
openFileDialog1->RestoreDirectory= true;
dlg=openFileDialog1->ShowDialog();
try
{
if(dlg==System::Windows::Forms::DialogResult::OK)
{
this->richTextBox1->LoadFile(openFileDialog1->FileName,RichTextBoxStr
eamType::RichText);
String^f=openFileDialog1->FileName;
label5->Text=f;
}
}
catch(System::ArgumentException^e)
{
MessageBox::Show(L"选择的不是rtf格式的文件~无效");
}
}
private: System::Void button4_Click(System::Object^ sender,
System::EventArgs^ e) //对文件内容进行编码
{
const char m[36][6]=//摩尔斯电码表
{
{"-----"},{".----"},{"..---"},{"...--"},{"....-"},{"....."},
{"-...."},{"--..."},{"---.."},{"----."},{".-"},{"-..."},
{"-.-."},{"-.."},{"."},{"..-."},{"--."},{"...."},
{".."},{".---"},{"-.-"},{".-.."},{"--"},{"-."},
{"---"},{".--."},{"--.-"},{".-."},{"..."},{"-"},
},{"...-"},{".--"},{"-..-"},{"-.--"},{"--.."} {"..-"
};
for(char*
*)(void*)Marshal::StringToHGlobalAnsi(richTextBox1->Text);*chch=(char
;ch++)
{
if(*ch>=48&&*ch<=57)
listBox1->Items->Add(gcnew String(m[*ch-48]));
else if(*ch>=65&&*ch<=90)
listBox1->Items->Add(gcnew
String(m[*ch-65+10]));
else
listBox1->Items->Add("字符格式错误");
}
}
private: System::Void button6_Click(System::Object^ sender,
System::EventArgs^ e) //对文件内容发出电码声响
{
const int n[36][8]=//报馈文消息表
{
{0,0,1,1,1,1,1,1},{0,0,1,0,1,1,1,1},{0,0,1,0,0,1,1,1},{0,0,1,0,0,0,1,
1},{0,0,1,0,0,0,0,1},{0,0,1,0,0,0,0,0},
{0,0,1,1,0,0,0,0},{0,0,1,1,1,0,0,0},{0,0,1,1,1,1,0,0},{0,0,1,1,1,1,1,
0},{0,0,0,0,0,1,0,1},{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,1,0},{0,0,0,0,1,1,0,0},{0,0,0,0,0,0,1,0},{0,0,0,1,0,0,1,
0},{0,0,0,0,1,1,1,0},{0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0},{0,0,0,1,0,1,1,1},{0,0,0,0,1,1,0,1},{0,0,0,1,0,1,0,
0},{0,0,0,0,0,1,1,1},{0,0,0,0,0,1,1,0},
{0,0,0,0,1,1,1,1},{0,0,0,1,0,1,1,0},{0,0,0,1,1,1,0,1},{0,0,0,0,1,0,1,
0},{0,0,0,0,1,0,0,0},{0,0,0,0,0,0,1,1},
{0,0,0,0,1,0,0,1},{0,0,0,1,0,0,0,1},{0,0,0,0,1,0,1,1},{0,0,0,1,1,0,0,
1},{0,0,0,1,1,0,1,1},{0,0,0,1,1,1,0,0}
};
for(char*
ch=(char*)(void*)Marshal::StringToHGlobalAnsi(richTextBox1->Text);*ch
;ch++)
{
if(*ch>=48&&*ch<=57)
{
int i,j;
for(i=0;i<8;i++)
{
if(n[*ch-48][i]==1)
{
j=i+1;
break;
}
}
for(i=j;i<8;i++)
{
if(n[*ch-48][i]==0)
dot();//调用响铃点函数
if(n[*ch-48][i]==1)
dash();//调用响铃划函数
space();
}
}
else if(*ch>=65&&*ch<=90)
{
int i,j;
for(i=0;i<8;i++)
{
if(n[*ch-65+10][i]==1)
{
j=i+1;
break;
}
}
for(i=j;i<8;i++)
{
if(n[*ch-65+10][i]==0)
dot();//调用响铃点函数
if(n[*ch-65+10][i]==1)
dash();//调用响铃划函数
space();
}
}
Sleep(300);
}
}
private: System::Void button2_Click(System::Object^ sender,
System::EventArgs^ e) //清空编码列表
{
listBox1->Items->Clear();
}
};
}