- #include <fstream.h>
- #include <iostream.h>
- #include <stdlib.h>
- void main(){
- int data[10];//存放数据的数组
- int count;//变量记录数据中1的个数
- ifstream inFile("data.txt");//读取数据文本
- if(!inFile)
- { cerr<<"cannot open my.dat"<<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;
- cout<<"奇偶校验编码后为:"<<endl;
- if(count%2==1){//1的数目为1,则码后加1
- for(int k=0;k<10;k++){
- cout<<data[k]<<1<<endl;
- break;
- }
- }
- if(count%2==0){//1的数目为0,则码后加0
- for(int l=0;l<10;l++){
- cout<<data[l]<<0<<endl;
- break;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/110220148688.html
来源: http://www.codesnippet.cn/detail/110220148688.html