画直线:
mainwindow.h中定义画直线的函数
protected:
void paintEvent(QPaintEvent *);
mainwindow.cpp中
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(QPen(Qt::blue,4));//设置画笔形式
painter.drawLine(100,100,200,100);//画直线
}
结果如图所示
三者转换如下:
消息码 : 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1
AMI码 : -1 0 0 0 0 +1 0 0 0 0 -1 +1 0 0 0 0 0 0 0 0 -1 +1
HDB3码: -1 0 0 0 -V +1 0 0 0 +V -1 +1 -B 0 0 -V +B 0 0 +V -1 +1
C++代码实现:
#include<stdio.h>
#include<cmath>
#define M 100
//int origin[M];
int origin[22] = { 1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1 };
int AMIcode[M];
void AMI(int n);
void HDB3(int n);
void main()
{
int i, n;
n = 22;
//printf("输入原码位数:\n");
//scanf("%d", &n);
//printf("输入原码:");
//for (i = 0; i < n; i++)scanf("%d", &origin[i]);
AMI(n);
printf("AMI码:\n");
for (i = 0; i < n; i++)printf("%5d", AMIcode[i]);
printf("\n");
printf("HDB3码:\n");
HDB3(n);
for (i = 0; i < n; i++)printf("%5d", AMIcode[i]);
printf("\n");
}
void AMI(int n)
{
int i, k = -1;
for (i = 0; i < n; i++)
{
if (origin[i] == 0)AMIcode[i] = 0;
else if (origin[i] == 1)
{
AMIcode[i] = k;
k = -k;
}
else;
}
}
void HDB3(int n)
{
int i = 0, m, j = 0, v, w, x = 0;
while (i < n)
{
m = 0;
while (AMIcode[i] == 0 && m < 4 && i < n) { m++; i++; }
if (m == 4)j++;
if (j == 1 && m == 4) { AMIcode[i - 1] = AMIcode[i - 5]; v = AMIcode[i - 1]; }
if (j > 1 && m == 4)
{
AMIcode[i - 1] = -v;
if (AMIcode[i - 1] != AMIcode[i - 5])
{
AMIcode[i - 4] = -AMIcode[i - 5];
AMIcode[i - 1] = AMIcode[i - 4];
w = AMIcode[i - 1];
/*while (x < 3 && i < n)
{
if (abs(AMIcode[i]) == 1) { AMIcode[i] = -w; w = AMIcode[i]; }
if (AMIcode[i] == 0 && AMIcode[i + 1] == 0)x++;
i++;
}
if (x > 2)i = -2;*/
}
else;
v = AMIcode[i - 1];
}
if (m == 0)i++;
else;
}
}
运行结果:
可视化实现:
原理很简单:码为1的时候画上横线,码为0的时候画中横线,码为-1的时候画下横线
原理如下:
p=0;
//AMI 码
AMI(M);
for(int z=0;z<M;z++)
{
if(AMIcode[z]==1)
{
painter.drawLine(X+p,200,X+10+p,200);
}
if(AMIcode[z]==0)
{
painter.drawLine(X+p,250,X+10+p,250);//画直线
}
if(AMIcode[z]==-1)
{
painter.drawLine(100+p,300,110+p,300);
}
p+=20;
}
mainwindow.cpp 代码如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<stdio.h>
#include<cmath>
#define M 22
//int origin[M];
int origin[M] = { 1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1 };
int AMIcode[M];
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void AMI(int n)
{
int i, k = -1;
for (i = 0; i < n; i++)
{
if (origin[i] == 0)AMIcode[i] = 0;
else if (origin[i] == 1)
{
AMIcode[i] = k;
k = -k;
}
else;
}
}
void HDB3(int n)
{
int i = 0, m, j = 0, v, w, x = 0;
while (i < n)
{
m = 0;
while (AMIcode[i] == 0 && m < 4 && i < n) { m++; i++; }
if (m == 4)j++;
if (j == 1 && m == 4) { AMIcode[i - 1] = AMIcode[i - 5]; v = AMIcode[i - 1]; }
if (j > 1 && m == 4)
{
AMIcode[i - 1] = -v;
if (AMIcode[i - 1] != AMIcode[i - 5])
{
AMIcode[i - 4] = -AMIcode[i - 5];
AMIcode[i - 1] = AMIcode[i - 4];
w = AMIcode[i - 1];
/*while (x < 3 && i < n)
{
if (abs(AMIcode[i]) == 1) { AMIcode[i] = -w; w = AMIcode[i]; }
if (AMIcode[i] == 0 && AMIcode[i + 1] == 0)x++;
i++;
}
if (x > 2)i = -2;*/
}
else;
v = AMIcode[i - 1];
}
if (m == 0)i++;
else;
}
}
int X=100;
int Y=50;
void MainWindow::paintEvent(QPaintEvent *)
{
int p=0;
QPainter painter(this);
painter.setPen(QPen(Qt::blue,4));//设置画笔形式
for(int z=0;z<M;z++) //源 码
{
if(origin[z]==1)
{
painter.drawLine(X+p,50,X+10+p,50);
}
if(origin[z]==0)
{
painter.drawLine(X+p,100,X+10+p,100);//画直线
}
p+=20;
}
p=0;
//AMI 码
AMI(M);
for(int z=0;z<M;z++)
{
if(AMIcode[z]==1)
{
painter.drawLine(X+p,200,X+10+p,200);
}
if(AMIcode[z]==0)
{
painter.drawLine(X+p,250,X+10+p,250);//画直线
}
if(AMIcode[z]==-1)
{
painter.drawLine(100+p,300,110+p,300);
}
p+=20;
}
p=0;
HDB3(M);
for(int z=0;z<M;z++)
{
if(AMIcode[z]==1)
{
painter.drawLine(X+p,400,X+10+p,400);
}
if(AMIcode[z]==0)
{
painter.drawLine(X+p,450,X+10+p,450);//画直线
}
if(AMIcode[z]==-1)
{
painter.drawLine(100+p,500,110+p,500);
}
p+=20;
}
}
ps:因为通信原理不是我的主科目。。我就懒得写注释了/(ㄒoㄒ)/~~