1、课程设计报告摩尔斯电码发声器课程设计报告Morse电码发声器 程序方案:0-9和A-Z的摩尔斯电码表如下图所示:将0-9和A-Z的摩尔斯电码制作为一个36行6列的二维数组,如下图所示当输入0-9或A-Z之间的字符时,程序会根绝具体的字符,从摩尔斯电码表的二维数组中选出对应该字符的一行数组输出,作为该字符的摩尔斯电码。用一个字节的二进制数代表一个字符的摩尔斯电码,左起第一个1为起始标志(起始标志及之前的0无效)。起始标志后的0表示点。起始标志后的1表示划。如:A 的报文信息 0000 0101B 的报文信息 0001 1000C 的报文信息 0001 1010按此规律建立一个报文信息表,报文信息
2、表要按 09 、AZ的顺序排列,发送时先查表找字符对应的报文信息,左移报文信息找起始标志,(起始标志及之前的0不发声),然后调用点划子程序发点划声响,发送一个字符循环8次则自动结束。报文信息表同样为一个36行6列的二维数组,如下图所示:系统细节方案:点划间隔子程序是通过调用window.h库中的Beep函数是系统发出声音。程序收到字符后,根据字符对应的摩尔斯电码表中的数组,输出该字符的摩尔斯电码,并且查找到该字符的01报文信息表,通过两个循环,调用点划子程序,完成电码的发音功能。子程序及其流程图:点声音程序代码: 点声音程序流程图int dot()/点声音 Beep(7000,200); re
3、turn 0;划声音程序代码: 划声音程序流程图int dash()/划声音 Beep(7000,600); return 0;点划之间间隔程序代码:int space()/电码间隔声音 Beep(0,400); return 0; 主程序详解:程序设计初期,用C+编写了一个控制台应用,先编写了一个电码编译的子程序:int tra(char c)/电码编译 if(c=48&c=57) coutc的摩尔斯电码是:mc-48endl; for(i=0;i8;i+) if(nc-48i=1) j=i+1; break; for(i=j;i=65&c=90) coutc的摩尔斯电码是:mc-65+10
4、endl; for(i=0;i8;i+) if(nc-65+10i=1) j=i+1; break; for(i=j;i8;i+) if(nc-65+10i=0) dot(); if(nc-65+10i=1) dash(); space(); else if(c57&c90) coutc输入格式错误,不是0-9或A-Z之间的字符endl; return 0;电码编译子程序流程图:在主程序中利用fstream库中的ifstream文件流又编写了一段可以打开文件的语句,主程序代码如下:int main()/主程序 while(1) cout请选择输入字符或者文件路径ch; if(ch=c)/输入字
5、符进行编括译 cout请输入0-9或A-Z之间的字符c; tra(c);/调用编码函 Sleep(500);/设置读取字符延迟 else if(ch=f)/输入文件路径进行编译 cout请输入文件路径filename; ifstream infile(filename,ios:in); if(!infile) cerr打开文件错误endl; while(infile.get(c) tra(c); Sleep(500);/设置读取字符延迟 else cout输入格式错误endl; 主程序流程图编译运行的结果:程序设计后期在windows窗体应用中创建项目,建立一个窗体,在窗体中完成字符、文件的打
6、开、编译功能,在窗体中添加控件,将之前的程序写入各个控件的动作中,完成程序的功能,程序运行结果如下图:在textBox1中输入字符,程序将textBox1中的字符存入指向字符数组的指针中,再根绝数组中字符,找到相对应的报文信息进行编译发声。利用openFileDialog打开文件,利用richTextBox中的LoadFile方法加载文件,再将richTextBox中的字符存入数组指针中,利用指针循环自加对数组指针中的字符进行编译发声。Morse电码主程序自动生成的cpp:/ morse.cpp: 主项目文件#include stdafx.h#include Form1.husing name
7、space morse;STAThreadAttributeint main(array args) / 在创建任何控件之前启用 Windows XP 可视化效果 Application:EnableVisualStyles(); Application:SetCompatibleTextRenderingDefault(false); / 创建主窗口并运行它 Application:Run(gcnew Form1(); return 0;Form1.h库中的源代码:#pragma once#includenamespace morse using namespace System; usin
8、g 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: F
9、orm1(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:Wind
10、ows: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:Window
11、s: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 D
12、esigner 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:Butto
13、n(); 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-lab
14、el2 = (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-
15、AutoSize = true; this-label1-Location = System:Drawing:Point(51, 32); this-label1-Name = Llabel1; this-label1-Size = System:Drawing:Size(65, 12); this-label1-TabIndex = 0; this-label1-Text = L请输入字符; / / textBox1 / this-textBox1-Location = System:Drawing:Point(53, 49); this-textBox1-Name = LtextBox1;
16、 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:Dr
17、awing:Point(315, 55); this-listBox1-Name = LlistBox1; this-listBox1-Size = System:Drawing:Size(120, 184); this-listBox1-TabIndex = 2; / / button1 / this-button1-Location = System:Drawing:Point(67, 132); this-button1-Name = Lbutton1; this-button1-Size = System:Drawing:Size(75, 23); this-button1-TabIn
18、dex = 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
19、label3; this-label3-Size = System:Drawing:Size(89, 12); this-label3-TabIndex = 6; this-label3-Text = L输入的字符是:; / / label4 / this-label4-AutoSize = true; this-label4-Location = System:Drawing:Point(138, 95); this-label4-Name = Llabel4; this-label4-Size = System:Drawing:Size(0, 12); this-label4-TabInd
20、ex = 7; / / openFileDialog1 / this-openFileDialog1-FileName = LopenFileDialog1; / / richTextBox1 / this-richTextBox1-Location = System:Drawing:Point(12, 256); this-richTextBox1-Name = LrichTextBox1; this-richTextBox1-Size = System:Drawing:Size(213, 132); this-richTextBox1-TabIndex = 8; this-richText
21、Box1-Text = L; / / button3 / this-button3-Location = System:Drawing:Point(315, 256); this-button3-Name = Lbutton3; 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:Ev
22、entHandler(this, &Form1:button3_Click); / / label2 / this-label2-AutoSize = true; this-label2-Location = System:Drawing:Point(313, 32); this-label2-Name = Llabel2; this-label2-Size = System:Drawing:Size(29, 12); this-label2-TabIndex = 11; this-label2-Text = L编码; / / label5 / this-label5-AutoSize = t
23、rue; this-label5-Location = System:Drawing:Point(55, 238); this-label5-Name = Llabel5; this-label5-Size = System:Drawing:Size(0, 12); this-label5-TabIndex = 12; / / button4 / this-button4-Location = System:Drawing:Point(315, 312); this-button4-Name = Lbutton4; this-button4-Size = System:Drawing:Size
24、(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 = Lbutton6; this-button6-Si
25、ze = 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 = Lbutton2; 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); /
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1