- Code first
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int data[10];//存放数据的数组
int count=0;//变量记录数据中1的个数
ifstream inFile("data.txt");//读取数据文本
if(!inFile)
{
cerr<<"无法打开"<<endl;exit(1);}
for(int i=0;i<10;i++){
inFile>>data[i];
if(data[i]==1){
//数据中有1则count+1
count=count+1;
}
}
cout<<"原始数据为:"<<endl;
for(int j=0;j<10;j++){
cout<<data[j];//读出原始数据
break;
}
cout<<endl;
if(count%2==1){
cout<<"偶校验校验位为 0"<<endl;
cout<<"奇校验位验位为 1"<<endl;
}
if(count%2==0){
cout<<"偶校验校验位为 1"<<endl;
cout<<"奇校验位验位为 0"<<endl;
}
}
We need to
build a data.txt file in the .cpp file directory to store the ten-digit 01 binary string
- The effect is shown in the figure