实验1 数字波形的生成
设计图:
源码:
namespace WindowsFormsApp1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea4 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea5 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea6 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Series series6 = new System.Windows.Forms.DataVisualization.Charting.Series();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.inputA = new System.Windows.Forms.TextBox();
this.inputB = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.outputF = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
this.label4 = new System.Windows.Forms.Label();
this.operators = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(615, 78);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(360, 60);
this.button1.TabIndex = 0;
this.button1.Text = "开始计算(非逻辑只运算A)";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(62, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(17, 18);
this.label1.TabIndex = 1;
this.label1.Text = "A";
//
// inputA
//
this.inputA.Location = new System.Drawing.Point(115, 25);
this.inputA.Name = "inputA";
this.inputA.Size = new System.Drawing.Size(245, 28);
this.inputA.TabIndex = 2;
//
// inputB
//
this.inputB.Location = new System.Drawing.Point(797, 29);
this.inputB.Name = "inputB";
this.inputB.Size = new System.Drawing.Size(245, 28);
this.inputB.TabIndex = 4;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(744, 33);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(17, 18);
this.label2.TabIndex = 3;
this.label2.Text = "B";
//
// outputF
//
this.outputF.Location = new System.Drawing.Point(115, 95);
this.outputF.Name = "outputF";
this.outputF.Size = new System.Drawing.Size(361, 28);
this.outputF.TabIndex = 6;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(62, 99);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(17, 18);
this.label3.TabIndex = 5;
this.label3.Text = "F";
//
// chart1
//
chartArea4.Name = "A";
chartArea5.Name = "B";
chartArea6.Name = "F";
this.chart1.ChartAreas.Add(chartArea4);
this.chart1.ChartAreas.Add(chartArea5);
this.chart1.ChartAreas.Add(chartArea6);
legend2.Name = "Legend1";
legend2.Title = "图例";
this.chart1.Legends.Add(legend2);
this.chart1.Location = new System.Drawing.Point(65, 154);
this.chart1.Name = "chart1";
this.chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
series4.ChartArea = "A";
series4.IsValueShownAsLabel = true;
series4.Legend = "Legend1";
series4.Name = "A";
series5.ChartArea = "B";
series5.IsValueShownAsLabel = true;
series5.Legend = "Legend1";
series5.Name = "B";
series6.ChartArea = "F";
series6.IsValueShownAsLabel = true;
series6.Legend = "Legend1";
series6.Name = "F";
this.chart1.Series.Add(series4);
this.chart1.Series.Add(series5);
this.chart1.Series.Add(series6);
this.chart1.Size = new System.Drawing.Size(952, 388);
this.chart1.TabIndex = 7;
this.chart1.Text = "chart1";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(447, 35);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(62, 18);
this.label4.TabIndex = 8;
this.label4.Text = "运算符";
//
// operators
//
this.operators.Location = new System.Drawing.Point(533, 30);
this.operators.Name = "operators";
this.operators.Size = new System.Drawing.Size(130, 28);
this.operators.TabIndex = 9;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1093, 554);
this.Controls.Add(this.operators);
this.Controls.Add(this.label4);
this.Controls.Add(this.chart1);
this.Controls.Add(this.outputF);
this.Controls.Add(this.label3);
this.Controls.Add(this.inputB);
this.Controls.Add(this.label2);
this.Controls.Add(this.inputA);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "与或非逻辑运算";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox inputA;
private System.Windows.Forms.TextBox inputB;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox outputF;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox operators;
}
}
程序逻辑实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string a = inputA.Text;
int[] a1 = new int[a.Length];
string b = inputB.Text;
int[] b1 = new int[b.Length];
int[] m = new int[a.Length];
string c = null;
bool flag = true;
int[] vs = new int[a.Length];
string o = operators.Text;
int mark = 0;
if (o.Equals("&") || o.Equals("与")) mark = 1;
else if (o.Equals("|") || o.Equals("或")) mark = 2;
else if (o.Equals("~") || o.Equals("非")) mark = 3;
else if (o.Equals("异或")) mark = 4;
else if (o.Equals("同或")) mark = 5;
if(mark == 3)
{
for(int i = 0; i < a.Length; i++)
{
a1[i] = a[i] == '1' ? 1 : 0;
if (a[i] != '0' && a[i] != '1')
{
flag = false;
break;
}
else
{
a1[i] = a[i] == '1' ? 1 : 0;
m[i] = a[i] == '1' ? 0 : 1;
}
}
for(int i = 0; i < m.Length; i++)
sb.Append(m[i]);
c = flag == true ? sb.ToString() : "非逻辑运算时,请勿在A中输入无关字符";
}
else
{
if(a.Length != b.Length)
{
c = "字符串长度不相等,请重新输入";
flag = false;
}
else
{
for(int i = 0; i < a.Length; i++)
{
a1[i] = a[i] == '1' ? 1 : 0;
b1[i] = b[i] == '1' ? 1 : 0;
if((a[i] != '1' && a[i] != '0') || (b[i] != '1' && b[i] != '0'))
{
flag = false;
break;
}
else
{
switch (mark)
{
case 1:
if (a[i] == '1' && b[i] == '1') m[i] = 1;
else m[i] = 0; break;
case 2:
if (a[i] == '1' || b[i] == '1') m[i] = 1;
else m[i] = 0; break;
case 4:
m[i] = a[i] == b[i] ? 1 : 0; break;
case 5:
m[i] = a[i] == b[i] ? 0 : 1; break;
}
}
}
for (int i = 0; i < m.Length; i++)
sb.Append(m[i]);//形参是啥类型都行,string都行
c = flag == true ? sb.ToString() : "请勿输入无关字符";
}
}
/*
if (a.Length != b.Length && mark != 3)
c = "字符串长度不相等,请重新输入";
else
{
for (int i = 0; i < a.Length; i++)
{
a1[i] = a[i] == '1' ? 1 : 0;
b1[i] = b[i] == '1' ? 1 : 0;
if (a[i] != '1' && a[i] != '0')
{
flag = false;
break;
}
else if (b[i] != '1' && b[i] != '0')
{
if(mark != 3)
{
flag = false;
break;
}
if(mark == 3)
{
if (a[i] == '1') m[i] = 0;
else m[i] = 1;
continue;
}
}
else
{
switch (mark)
{
case 1: if (a[i] == '1' && b[i] == '1') m[i] = 1;
else m[i] = 0;break;
case 2:
if (a[i] == '1' || b[i] == '1') m[i] = 1;
else m[i] = 0; break;
case 4:
m[i] = a[i] == b[i] ? 1 : 0; break;
case 5:
m[i] = a[i] == b[i] ? 0 : 1; break;
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < m.Length; i++)
{
sb.Append(m[i]);//形参是啥类型都行,string都行
}
c = flag == true ? sb.ToString() : "请勿输入无关字符";
if(mark == 3)
{
for(int i = 0; i < a.Length; i++)
{
if (a[i] != '1' && a[i] != '0')
{
c = "非逻辑运算时,请勿在A中输入无关字符";
break;
}
else c = sb.ToString();
}
}
}
*/
outputF.Text = c;
if(flag == false)
{
chart1.Series[0].Points.DataBindY(vs);
chart1.Series[1].Points.DataBindY(vs);
chart1.Series[2].Points.DataBindY(vs);
}
else if(flag == true && mark == 3)
{
chart1.Series[0].Points.DataBindY(a1);
chart1.Series[1].Points.DataBindY(vs);
chart1.Series[2].Points.DataBindY(m);
}
else
{
chart1.Series[0].Points.DataBindY(a1);
chart1.Series[1].Points.DataBindY(b1);
chart1.Series[2].Points.DataBindY(m);
}
}
}
}
实验2 电子钢琴
设计图
程序逻辑实现代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 电子钢琴
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Console.Beep(131, 250);
}
private void button2_Click(object sender, EventArgs e)
{
Console.Beep(147, 250);
}
private void button3_Click(object sender, EventArgs e)
{
Console.Beep(165, 250);
}
private void button4_Click(object sender, EventArgs e)
{
Console.Beep(175, 250);
}
private void button5_Click(object sender, EventArgs e)
{
Console.Beep(196, 250);
}
private void button6_Click(object sender, EventArgs e)
{
Console.Beep(220, 250);
}
private void button7_Click(object sender, EventArgs e)
{
Console.Beep(247, 250);
}
private void button8_Click(object sender, EventArgs e)
{
Console.Beep(262, 250);
}
private void button9_Click(object sender, EventArgs e)
{
Console.Beep(296, 250);
}
private void button10_Click(object sender, EventArgs e)
{
Console.Beep(330, 250);
}
private void button11_Click(object sender, EventArgs e)
{
Console.Beep(349, 250);
}
private void button12_Click(object sender, EventArgs e)
{
Console.Beep(392, 250);
}
private void button13_Click(object sender, EventArgs e)
{
Console.Beep(440, 250);
}
private void button14_Click(object sender, EventArgs e)
{
Console.Beep(494, 250);
}
private void button15_Click(object sender, EventArgs e)
{
Console.Beep(523, 250);
}
private void button16_Click(object sender, EventArgs e)
{
Console.Beep(587, 250);
}
private void button17_Click(object sender, EventArgs e)
{
Console.Beep(659, 250);
}
private void button18_Click(object sender, EventArgs e)
{
Console.Beep(698, 250);
}
private void button20_Click(object sender, EventArgs e)
{
Console.Beep(784, 250);
}
private void button19_Click(object sender, EventArgs e)
{
Console.Beep(880, 250);
}
private void button21_Click(object sender, EventArgs e)
{
Console.Beep(988, 250);
}
private void button22_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Console.Beep(500 + 100 * i, 500);
}
}
private void button23_Click(object sender, EventArgs e)
{
string music = Music.Text;
if (music.Equals("天空之城"))
{
}
else if (music.Equals("欢乐颂"))
{
JiePai.Text = "2222222222222";
YinDiao.Text = "7712217655677";
YinJie.Text = " ++++ ";
}
else if (music.Equals("皎洁的笑颜"))
{
JiePai.Text = "2222222222222222222222222222222";
YinDiao.Text = "3535354535353512353535455545352";
YinJie.Text = "+ + + + + + + +++ + + + + + + +";
}
string jiepai = JiePai.Text;
string yindiao = YinDiao.Text;
string yinjie = YinJie.Text;
int[] a = new int[jiepai.Length];
int[] b = new int[yindiao.Length];
if (a.Length != b.Length ||
a.Length != yinjie.Length ||
b.Length != yinjie.Length)
MessageBox.Show("长度不一致,请重新输入", "出现错误",
MessageBoxButtons.OKCancel);
else
{
Boolean flag = true;
for (int i = 0; i < a.Length; i++)
{
a[i] = Convert.ToInt32(jiepai[i] - 48);
b[i] = Convert.ToInt32(yindiao[i] - 48);
if (yinjie[i] != '+' && yinjie[i] != ' ' && yinjie[i] != '-')
{
MessageBox.Show("有非法字符,请重新输入", "出现错误",
MessageBoxButtons.OKCancel);
flag = false;
break;
}
}
if (flag)
{
for (int i = 0; i < a.Length; i++)
{
if (yinjie[i] == '-')
{
switch (b[i])
{
case 1: Console.Beep(131, 250 * a[i]); break;
case 2: Console.Beep(147, 250 * a[i]); break;
case 3: Console.Beep(165, 250 * a[i]); break;
case 4: Console.Beep(175, 250 * a[i]); break;
case 5: Console.Beep(196, 250 * a[i]); break;
case 6: Console.Beep(220, 250 * a[i]); break;
case 7: Console.Beep(247, 250 * a[i]); break;
}
}
else if (yinjie[i] == ' ')
{
switch (b[i])
{
case 1: Console.Beep(262, 250 * a[i]); break;
case 2: Console.Beep(296, 250 * a[i]); break;
case 3: Console.Beep(330, 250 * a[i]); break;
case 4: Console.Beep(349, 250 * a[i]); break;
case 5: Console.Beep(392, 250 * a[i]); break;
case 6: Console.Beep(440, 250 * a[i]); break;
case 7: Console.Beep(494, 250 * a[i]); break;
}
}
else if (yinjie[i] == '+')
{
switch (b[i])
{
case 1: Console.Beep(523, 250 * a[i]); break;
case 2: Console.Beep(587, 250 * a[i]); break;
case 3: Console.Beep(659, 250 * a[i]); break;
case 4: Console.Beep(698, 250 * a[i]); break;
case 5: Console.Beep(784, 250 * a[i]); break;
case 6: Console.Beep(880, 250 * a[i]); break;
case 7: Console.Beep(988, 250 * a[i]); break;
}
}
}
}
}
}
}
}
实验3 函数信号发生器
设计图
源码
namespace 函数信号发生器
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.panel1 = new System.Windows.Forms.Panel();
this.A = new System.Windows.Forms.TextBox();
this.f = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
this.textBox1 = new System.Windows.Forms.TextBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.A);
this.panel1.Controls.Add(this.f);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.button4);
this.panel1.Controls.Add(this.button3);
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(995, 135);
this.panel1.TabIndex = 0;
//
// A
//
this.A.Location = new System.Drawing.Point(821, 97);
this.A.Name = "A";
this.A.Size = new System.Drawing.Size(135, 28);
this.A.TabIndex = 7;
//
// f
//
this.f.Location = new System.Drawing.Point(821, 56);
this.f.Name = "f";
this.f.Size = new System.Drawing.Size(135, 28);
this.f.TabIndex = 6;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(725, 100);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 18);
this.label2.TabIndex = 5;
this.label2.Text = "功放(倍)";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(725, 59);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 18);
this.label1.TabIndex = 4;
this.label1.Text = "倍频(倍)";
//
// button4
//
this.button4.Location = new System.Drawing.Point(548, 51);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(142, 74);
this.button4.TabIndex = 3;
this.button4.Text = "三角波";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(370, 51);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(142, 74);
this.button3.TabIndex = 2;
this.button3.Text = "方波";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(207, 51);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(142, 74);
this.button2.TabIndex = 1;
this.button2.Text = "cos";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(41, 53);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(142, 71);
this.button1.TabIndex = 0;
this.button1.Text = "sin";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel2
//
this.panel2.Controls.Add(this.chart1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(0, 135);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(995, 492);
this.panel2.TabIndex = 1;
//
// chart1
//
chartArea1.AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.False;
chartArea1.AxisX.MajorGrid.Enabled = false;
chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
this.chart1.Location = new System.Drawing.Point(0, 0);
this.chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series1.Name = "Series1";
series2.ChartArea = "ChartArea1";
series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series2.Name = "Series2";
series3.ChartArea = "ChartArea1";
series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series3.Name = "Series3";
series4.ChartArea = "ChartArea1";
series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series4.Name = "Series4";
this.chart1.Series.Add(series1);
this.chart1.Series.Add(series2);
this.chart1.Series.Add(series3);
this.chart1.Series.Add(series4);
this.chart1.Size = new System.Drawing.Size(995, 492);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(222, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(551, 28);
this.textBox1.TabIndex = 8;
this.textBox1.Text = "初始值:周期100,幅值4 若没有输入,倍频和功放均默认为1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(995, 627);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox A;
private System.Windows.Forms.TextBox f;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}
程序逻辑实现代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 函数信号发生器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//清空当前的作图
public void PointsClear()
{
for (int i = 0; i < chart1.Series.Count; i++)
{
chart1.Series[i].Points.Clear();
}
}
//尝试写出的String转double的代码(当然现在不用了。。。。。)
/*
//将String转换为double格式
public double stringToDouble(String str)
{
bool negative = false;
double sum = 0.0d;
bool pointAppear = false;
int count = 1;//用来记录小数点后几位
if (str == null || str.Equals(""))
{
throw new Exception("重新输入字符串");
}
for (int i = 0; i < str.Length; i++)
{
//判断第一个字符是不是负号
if (i == 0 && (str[0] == '-' || str[0] == '+'))
{
if (str[i] == '-')
negative = true;
}
else
{
//判断是否为小数点
if (pointAppear == false && i != 0 && str[i] == '.')
{
pointAppear = true;
}
else if (str[i] >= '0' && str[i] <= '9')
{
if (i == 0 && str[i] == '0')
{
if (str[i + 1] != '.')
throw new Exception("不合法字符串1");
}
if (i == 1 && str[i] == '0' && (str[0] == '-' || str[0] == '+'))
{
if (str[i + 1] != '.')
throw new Exception("不合法字符串2");
}
//整数部分
if (pointAppear == false)
{
sum = sum * 10 + (str[i] - '0');
}
else
{
sum = sum + Math.Pow(0.1, count) * (str[i] - '0');
count++;
}
}
else
{
throw new Exception("不合法字符串3");
}
}
}
return negative == true ? sum * (-1) : sum;
}
*/
private void button1_Click(object sender, EventArgs e)
{
double frequence = (f.Text == "") ? 1 : double.Parse(f.Text);
double magnification = (A.Text == "") ? 1 : double.Parse(A.Text);
PointsClear();
for (int i = 0; i < 1000; i++)
{
double pointValue = magnification * Math.Sin(i * Math.PI / 180.0 * frequence);
chart1.Series[0].Points.AddXY(i, pointValue);
chart1.Series[0].BorderWidth = 5;
chart1.Series[0].Color = Color.Red;
chart1.ChartAreas[0].AxisX.Title = "x";
chart1.ChartAreas[0].AxisY.Title = "y = f(x)";
}
}
private void button2_Click(object sender, EventArgs e)
{
double frequence = (f.Text == "") ? 1 : double.Parse(f.Text);
double magnification = (A.Text == "") ? 1 : double.Parse(A.Text);
PointsClear();
for (int i = 0; i < 1000; i++)
{
double pointValue = magnification * Math.Cos(i * Math.PI / 180.0 * frequence);
chart1.Series[1].Points.AddXY(i, pointValue);
chart1.Series[1].BorderWidth = 5;
chart1.Series[1].Color = Color.Blue;
}
}
private void button3_Click(object sender, EventArgs e)
{
double frequence = (f.Text == "") ? 1 : double.Parse(f.Text);
double magnification = (A.Text == "") ? 1 : double.Parse(A.Text);
PointsClear();
double T = 100 / frequence;//周期
for (int i = 0; i < 1000; i++)
{
double pointValue = i % T <= T / 2 ? 5 : -5;
chart1.Series[2].Points.AddXY(i, pointValue * magnification);
chart1.Series[2].BorderWidth = 5;
chart1.Series[2].Color = Color.Green;
}
}
private void button4_Click(object sender, EventArgs e)
{
double frequence = (f.Text == "") ? 1 : double.Parse(f.Text);
double magnification = (A.Text == "") ? 1 : double.Parse(A.Text);
PointsClear();
double T = 100;
double k = 8 / T;
int count = 0;
double y;
for (int i = 1; i <= 1000 * frequence; i++)
{
y = (i % T <= T / 2) ? (k * i - 8.0 * count) : (8.0 * count - k * i);
if ((i + T / 2) % T == 0.0) count++;
chart1.Series[3].Points.AddXY(i, y * magnification);
chart1.Series[3].BorderWidth = 5;
chart1.Series[3].Color = Color.Yellow;
}
}
}
}
最终效果
函数信号发生器
实验4 电路图的设计
共集-共集直接耦合放大电路
众所周知:基本共集放大电路
电压增益近似为1,输入阻抗近似为
β
1
⋅
β
2
⋅
R
L
′
\ {\beta_1}\cdot {\beta_2}\cdot {R'_L}
β1⋅β2⋅RL′,输出阻抗
R
o
=
R
0
1
+
r
b
e
2
1
+
β
2
{R_o} = \frac{{R_0}_1 + {{r_b}_e}_2}{1+\beta_2}
Ro=1+β2R01+rbe2,其中
R
o
1
=
R
S
+
r
b
e
1
1
+
β
1
{{R_o}_1}= \frac{R_S+ {{r_b}_e}_1}{1+\beta_1}
Ro1=1+β1RS+rbe1.
直流通路、交流通路、微变等效电路
公式结果
设计图
运行结果
一个小问题
这里运行的时候会有一个问题:R1R2R3的输入框跑到别的地方了
比如下面:
解决
1、窗口属性的设置
2、各个penal控件的属性设置:
MaxSize和MinSize都要跟Size大小一样
程序逻辑实现代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 电路图设计
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
double Vr = double.Parse(Vrms.Text);
double f = double.Parse(Frequence.Text);
double Beta = double.Parse(beta.Text);
double Rbb = double.Parse(rbb.Text);
double R1 = double.Parse(r1.Text);
double R2 = double.Parse(r2.Text);
double R3 = double.Parse(r3.Text);
double RL = double.Parse(rl.Text);
double IBQ1;
double ICQ1;
double VCEQ1;
double VCEQ2;
double Av1;
double Av2;
double Av;
double Vo;
double Ri1;
double Ri2;
double Ro1;
double Ro2;
double runTime;
//各个数据的计算,包括输出波形的振幅
IBQ1 = 1000 * (10.6 + (1 + Beta) * R3 * 0.7 / (R2 + (1 + Beta) * R3))
/ ((1 + Beta) * (1 + Beta) * R3 * R2 / (R2 + (1 + Beta) * R3) + R1);
double IBQ2 = ((1 + Beta) * IBQ1 * R2 - 700) / (R2 + (1 + Beta) * R3);
ICQ1 = Beta * IBQ1 / 1000;
VCEQ1 = 12 - (0.7 + (1 + Beta) * IBQ2 * R3 / 1000);
VCEQ2 = 12 - (1 + Beta) * IBQ2 * R3 / 1000;
double rbe1 = Rbb + 1000 * 26 / IBQ1;
double rbe2 = Rbb + 1000 * 26 / IBQ2;
Ri2 = rbe2 / 1000 + (1 + Beta) * R3 * RL / (R3 + RL);
double Ri11 = rbe1 / 1000 + (1 + Beta) * R2 * Ri2 * (R2 + Ri2);
Ri1 = R1 * Ri11 / (R1 + Ri11);
Av1 = ((1 + Beta) * R2 * Ri1 / (R2 + Ri1)) / (rbe1 / 1000 + (1 + Beta) * R2 * Ri1 / (R2 + Ri1));
Av2 = ((1 + Beta) * R3 * Ri2 / (R3 + Ri2)) / (rbe2 / 1000 + (1 + Beta) * R3 * Ri2 / (R3 + Ri2));
Av = Av1 * Av2;
Ro1 = (rbe1 / (1 + Beta)) * R2 / (rbe1 / (1 + Beta) + R2);
Ro2 = ((rbe2 + Ro1) / (1 + Beta)) * R3 / (R3 + (rbe2 + Ro1) / (1 + Beta));
Vo = Av * Vr;
//绘制波形图,注意共集放大电路是同相的
chart1.Series[0].Points.Clear();
for (int i = 0; i < 1000; i++)
{
double pointValue = Vo * Math.Sin(i * Math.PI / 180.0 * f);
chart1.Series[0].Points.AddXY(i, pointValue);
chart1.Series[0].BorderWidth = 1;
chart1.Series[0].Color = Color.Red;
chart1.ChartAreas[0].AxisX.Title = "x";
chart1.ChartAreas[0].AxisY.Title = "y = f(x)";
}
Ibq1.Text = Convert.ToString(string.Format("{0:F2}", IBQ1));
Icq1.Text = Convert.ToString(string.Format("{0:F2}", ICQ1));
Vceq1.Text = Convert.ToString(string.Format("{0:F2}", VCEQ1));
Vceq2.Text = Convert.ToString(string.Format("{0:F2}", VCEQ2));
av1.Text = Convert.ToString(string.Format("{0:F2}", Av1));
av2.Text = Convert.ToString(string.Format("{0:F2}", Av2));
av.Text = Convert.ToString(string.Format("{0:F2}", Av));
vo.Text = Convert.ToString(string.Format("{0:F2}", Vo));
ri1.Text = Convert.ToString(string.Format("{0:F2}", Ri1));
ri2.Text = Convert.ToString(string.Format("{0:F2}", Ri2));
ro1.Text = Convert.ToString(string.Format("{0:F2}", Ro1));
ro2.Text = Convert.ToString(string.Format("{0:F2}", Ro2));
//DataGridView
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[0].Value = Vrms.Text;
dataGridView1.Rows[index].Cells[1].Value = Frequence.Text;
dataGridView1.Rows[index].Cells[2].Value = beta.Text;
dataGridView1.Rows[index].Cells[3].Value = r1.Text;
dataGridView1.Rows[index].Cells[4].Value = r2.Text;
dataGridView1.Rows[index].Cells[5].Value = r3.Text;
dataGridView1.Rows[index].Cells[6].Value = rl.Text;
dataGridView1.Rows[index].Cells[7].Value = Ibq1.Text;
dataGridView1.Rows[index].Cells[8].Value = Icq1.Text;
dataGridView1.Rows[index].Cells[9].Value = Vceq1.Text;
dataGridView1.Rows[index].Cells[10].Value = Vceq2.Text;
dataGridView1.Rows[index].Cells[11].Value = av1.Text;
dataGridView1.Rows[index].Cells[12].Value = av2.Text;
dataGridView1.Rows[index].Cells[13].Value = av.Text;
dataGridView1.Rows[index].Cells[14].Value = vo.Text;
dataGridView1.Rows[index].Cells[15].Value = ri1.Text;
dataGridView1.Rows[index].Cells[16].Value = ri2.Text;
dataGridView1.Rows[index].Cells[17].Value = ro1.Text;
dataGridView1.Rows[index].Cells[18].Value = ro2.Text;
stopwatch.Stop();
TimeSpan t = stopwatch.Elapsed;
double runtime = t.TotalMilliseconds;
Runtime.Text = Convert.ToString(string.Format("{0:F2}", runtime));
dataGridView1.Rows[index].Cells[19].Value = Runtime.Text;
}
}
}
对数据库资料的学习和收集
实验5 解决专业问题
离散傅里叶变换
原理
设计图
程序逻辑实现代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 解决专业问题
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void PointsClear()
{
for (int i = 0; i < chart1.Series.Count; i++)
{
chart1.Series[i].Points.Clear();
}
for (int i = 0; i < chart2.Series.Count; i++)
{
chart2.Series[i].Points.Clear();
}
}
//离散傅里叶变换
public static double[] dft(double[] array)
{
int N = array.Length;
//重排数据
Complex[] Register = new Complex[N];
Complex[] dest = new Complex[N];
for (int i = 0; i < N; i++)
{
Register[i] = new Complex(array[i], 0);
}
for (int k = 0; k < N; k++)
{
Complex sum = new Complex();
for (int n = 0; n < N; n++)
{
sum += Register[n] * (new Complex(Math.Cos(2 * Math.PI / N * n * k), -1 * Math.Sin(2 * Math.PI / N * n * k)));
}
dest[k] = sum;
}
double[] dest1 = new double[N];
for (int k = 0; k < N; k++)
{
dest1[k] = dest[k].Modulus();
}
return dest1;
}
private void button1_Click(object sender, EventArgs e)
{
double[] array1 = new double[100];
if (wave.Text.Equals("正弦波"))
{
PointsClear();
for (int i = 0; i < 100; i++)
{
double pointValue = Math.Sin(i * Math.PI / 18.0);
array1[i] = pointValue;
chart1.Series[0].Points.AddXY(i, pointValue);
chart1.Series[0].BorderWidth = 5;
chart1.Series[0].Color = Color.Red;
chart1.ChartAreas[0].AxisX.Title = "x";
chart1.ChartAreas[0].AxisY.Title = "y = f(x)";
}
double[] array2 = dft(array1);
for (int i = 0; i < array2.Length; i++)
{
chart2.Series[0].Points.AddXY(i, array2[i]);
}
}
else if (wave.Text.Equals("方波"))
{
PointsClear();
double T = 10;//周期
for (int i = 0; i < 100; i++)
{
double pointValue = i % T <= T / 2 ? 5 : -5;
array1[i] = pointValue;
chart1.Series[0].Points.AddXY(i, pointValue);
chart1.Series[0].BorderWidth = 5;
chart1.Series[0].Color = Color.Green;
chart1.ChartAreas[0].AxisX.Title = "x";
chart1.ChartAreas[0].AxisY.Title = "y = f(x)";
}
double[] array2 = dft(array1);
for (int i = 0; i < array2.Length; i++)
{
chart2.Series[0].Points.AddXY(i, array2[i]);
}
}
else if (wave.Text.Equals("三角波"))
{
PointsClear();
double T = 10;
double k = 0.8 / T;
int count = 0;
double y;
for (int i = 1; i <= 100; i++)
{
y = (i % T <= T / 2) ? (k * i - 0.8 * count) : (0.8 * count - k * i);
if ((i + T / 2) % T == 0.0) count++;
array1[i - 1] = y;
chart1.Series[0].Points.AddXY(i, y);
chart1.Series[0].BorderWidth = 5;
chart1.Series[0].Color = Color.Yellow;
chart1.ChartAreas[0].AxisX.Title = "x";
chart1.ChartAreas[0].AxisY.Title = "y = f(x)";
}
double[] array2 = dft(array1);
for (int i = 0; i < array2.Length; i++)
{
chart2.Series[0].Points.AddXY(i, array2[i]);
}
}
}
//复数
public class Complex
{
public double Re;
public double Im;
public Complex()
{
Re = 0;
Im = 0;
}
public Complex(double re)
{
Re = re;
Im = 0;
}
public Complex(double re, double im)
{
Re = re;
Im = im;
}
public double Modulus()
{
return Math.Sqrt(Re * Re + Im * Im);
}
//操作符重载
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.Re + c2.Re, c1.Im + c2.Im);
}
public static Complex operator +(double d, Complex c)
{
return new Complex(d + c.Re, c.Im);
}
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex(c1.Re * c2.Re - c1.Im * c2.Im, c1.Re * c2.Im + c2.Re * c1.Im);
}
public static Complex operator *(Complex c, double d)
{
return new Complex(c.Re * d, c.Im * d);
}
public static Complex operator *(double d, Complex c)
{
return new Complex(c.Re * d, c.Im * d);
}
}
}
}