- 论坛徽章:
- 0
|
回复 #3 jixian01 的帖子(附修改后的源代码)
#include<iostream>
#define debug
using namespace std;
int main(){
void printBinary(const int);
void setvalue(void*,short,unsigned short);
int i_array[10]={1,2,3,4,5,6,7,8,9,10};
void* v_ptr=static_cast<void*>(i_array);
cout<<"Please input the byte and the value :"<<endl;
short byte;unsigned short value;
cin>>byte>>value;
setvalue(v_ptr,byte,value);
int* int_ptr=static_cast<int*>(v_ptr);
#ifndef debug
for(int i=0;i<10;i++){
cout<<i_array<<endl;
}
#endif
#ifdef debug
for(int i=0;i<10;i++){
printBinary(i_array);
cout<<endl;
}
#endif
system("pause");
return 0;
}
void setvalue(void* v_ptr,short byt,unsigned short val){
void setbyte(int*,unsigned short);
int* int_ptr=static_cast<int*>(v_ptr);
for(int* i_ptr=int_ptr;i_ptr<int_ptr+byt/2;i_ptr++){
setbyte(i_ptr,val);
}
if(byt%2!=0){
setbyte(int_ptr+(byt-1)/2,val);
}
}
void setbyte(int* int_ptr,unsigned short val){
unsigned char* uc_ptr
=reinterpret_cast<unsigned char*>(int_ptr);
for(int i=sizeof(int)-1;i>=0;i--){
uc_ptr=val;
}
}
void printBinary(const int val){
for(int i=31;i>=0;i--){
if(val&(1<<i)){
std::cout<<"1";
}else{
std::cout<<"0";
}
if(i%8==0)cout<<" ";
}
} |
|