📄 检校验.cpp
字号:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include <stdlib.h>
void tsimpodd() //奇校验程序
{
char sname[10];//结果文件
int a[80];
int i;
getname1: printf("please input your result code file name:\n");
scanf("%s",sname);
FILE *fp;
fp=fopen(sname,"r");
if(fp==NULL)
{ printf("file open error\n");
getch();
exit(0);
}//打开结果文件
for(i=0 ; !feof(fp)&&i<=80;i++)
{
a[i]=fgetc(fp)-'0';//将源文件内容转化后输入a数组
if( a[i]!=1&&a[i]!=0&&a[i]!=-49)
{
printf("source data error not 0&1\n");
getch();
exit(0);
} //保证为10序列
}
int t,j;
for(t=0,j=0 ; j<i-1 ; j++) t=t+a[j];
if((t+1)%2!=0)
{
printf("wrong communication");
goto getname1;
}
else
{
printf("right communication");
printf("the source will be output");
char rname[10];//结果文件
printf("please input the source code file name you want it saved:\n");
scanf("%s",rname);
FILE *fp1;
fp1=fopen(rname,"w");
for(j=0 ; j<i-2 ;j++ ) fputc(a[j]+'0',fp1);//输出原码
fclose(fp);
fclose(fp1);
}
return;//关文件,返回
}
void tverodd() //垂直奇校验程序
{
char sname[10];//源文件
int a[80][10];//二维数组
int i;
getname2: printf("please input your result code file name:\n");
scanf("%s",sname);
FILE *fp;
fp=fopen(sname,"r");
if(fp==NULL)
{
printf("file open error\n");
getch();
exit(0);
}//打开文件
for(i=0 ; !feof(fp)&&i<=800;i++)
{
a[i/10][i%10]=fgetc(fp)-'0';//读文件为二维数组付值
}
i--;
int k;
for(k=0;k<i;k++)
{
if(a[k/10][k%10]!=1&&a[k/10][k%10]!=0&&a[k/10][k%10]!=-38&&a[k/10][k%10]!=204)
{
printf("source data error not 0&1\n");
getch();
exit(0);
} //保证为10序列
}
int j,t;
k=i/10;
for(j=0;j<k;j++)
{
t=0;
for(i=0;i<9;i++) t=t+a[j][i];//求每一行的和
t=t+1;//实现奇校验
if(t%2!=0)
{
printf("wrong communication");
goto getname2;
}
}
for(j=0;j<=8;j++)
{
t=0;
for(i=0;i<=k;i++) t=t+a[i][j];//求每一列的和
t=t+1;//实现奇校验
if(t%2!=0)
{
printf("wrong communication");
goto getname2;
}
}
printf("right communication");
printf("the source will be output");
char rname[10];//结果文件
printf("please input the source code file name you want it saved:\n");
scanf("%s",rname);//获得结果文件名
FILE *fp1;
fp1=fopen(rname,"w");
for(j=0 ; j<=k-1 ;j++ )
for(i=0;i<=7;i++)
fputc(a[j][i]+'0',fp1);//输出到结果文件
fclose(fp);
fclose(fp1);
return;//关文件,返回
}
void tcrc()
{
char sname[10],pname[10],rname[10];//源文件、校验码组,结果文件
int a[128],b[40],c[80],r[40];//a源,b检验序列,c模二结果,r模二余
int i,j,t,k;
getname3: printf("please input your source code file name:\n");
scanf("%s",sname);
printf("please input your p(x) code file name:\n");
scanf("%s",pname);
FILE *fp1,*fp,*fp2;
fp=fopen(sname,"r");
if(fp==NULL)
{
printf("file open error\n");
getch();
exit(0);
}//打开文件
fp1=fopen(pname,"r");
if(fp1==NULL)
{
printf("file open error\n");
getch();
exit(0);
}//打开文件
for(i=0 ; !feof(fp)&&i<=128;i++)
{
a[i]=fgetc(fp)-'0';//将源文件内容转化后输入a数组
if( a[i]!=1&&a[i]!=0&&a[i]!=-49)
{
printf("source data error\n");
getch();
exit(0);
} //保证为10序列
}
for(j=0 ; !feof(fp1)&&j<=40;j++)
{
b[j]=fgetc(fp1)-'0';//将校验码文件内容转化后输入b数组
if( b[j]!=1&&b[j]!=0&&b[j]!=-49 )
{
printf("source data error\n");
getch();
exit(0);
} //保证为10序列
}
i--;
j--;//计数
for(k=0;k<j;k++)
r[k]=a[k];
for(t=0;t<=i-j;t++)
{
if(r[0]==0)
{
c[t]=0;
if(t!=i-j)
{
for(k=0;k<j-1;k++)
r[k]=r[k+1];
r[j-1]=a[t+j];
}
}
else
{
c[t]=1;
for(k=0;k<j;k++)
r[k]=(b[k]+r[k])%2;
if(t!=i)
{
for(k=0;k<j-1;k++)
r[k]=r[k+1];
r[j-1]=a[t+j];
}
}
}
for(k=0;k<j;k++)
{
if (r[k]!=0)
{
printf("wrong communication");
goto getname3;
}
}
printf("right communication");
printf("the source will be output");
printf("please input the source code file name you want it saved:\n");
scanf("%s",rname);//获得结果文件名
fp2=fopen(rname,"w");
for(k=0;k<i-j;k++)
fputc(a[k]+'0',fp2);
fclose(fp);
fclose(fp1);
fclose(fp2);
return;//关文件,返回
}
void main()
{
begin: printf("please choose error-correct way:\n");
printf("(1)test for simple odd:\n");
printf("(2)test for vericle odd:\n");
printf("(3)test for crc test:\n");
int n;
scanf("%d",&n);
switch(n)
{
case 1:tsimpodd();break;
case 2:tverodd();break;
case 3:tcrc();break;
default:printf("wrong number.\n");goto begin;
}
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -