免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1947 | 回复: 7
打印 上一主题 下一主题

c++编程思想一道习题,敬请高手指教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-12 16:41 |只看该作者 |倒序浏览
定义一个int数组。获得该数组的起始地址,使用static_cast将其转化为void*。写一个带以下参数的函数:一个void*,一个数字(表明字节数目)和一个值(表明每个字节需要设定的值)。该函数必须为特定范围内的每个字节设定特定的值。在这个int数组上试验函数。

代码如下:
#include<iostream>
using namespace std;
int main(){
    void setvalue(void*,short,unsigned char);
    int i_array[10];
    cout<<"please input the value"<<endl;
    for(int i=0;i<10;i++){
            cin>>i_array;        
            cout<<i_array;
    }
    int* i_arrptr=i_array;
    void* v_ptr=static_cast<void*>(i_arrptr);
    cout<<"Please input the byte and the value :"<<endl;
    short byte;unsigned char value;
    cin>>byte>>value;
    setvalue(v_ptr,byte,value);
    system("pause");
    return 0;
}
void setvalue(void* vptr,short byt,unsigned char val){
     void display(short,int*);
     int* ptr=static_cast<int*>(vptr);
     if(byt%2==0){
                  for(int i=0;i<byt/2;i++){
                          (ptr+i)[0]=val;
                          (ptr+i)[1]=val;      
                          display(byt,ptr);
                  }            
     }else{
           for(int j=0;j<byt/2;j++){
                   (ptr+j)[0]=val;
                   (ptr+j)[1]=val;        
           }
           (ptr+(byt/2))[0]=val;
           display(byt,ptr);
     }     
}
void display(short bt,int* ptr){
     if(bt%2==0){
                  for(int i=0;i<(bt/2);i++){
                          cout<<ptr<<endl;                          
                  }            
     }else{
           for(int j=0;j<bt/2;j++){
                   cout<<ptr[j]<<endl;        
           }
           cout<<ptr[(bt+1)/2]<<endl;
     }     
}
我的函数只输出改变了的值,比如改变了四个字节,就只输出前两个元素
输入4c(ASCII码99)
输出:
99
99
99
99

输入5a
输出:
97
97
4
请教一下问题出在哪?

论坛徽章:
0
2 [报告]
发表于 2008-02-12 17:26 |只看该作者

不好意思,重新写了一下代码

#include<iostream>
using namespace std;
int main(){
    void setvalue(void*,short,unsigned char);
    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 char value;
    cin>>byte>>value;
    setvalue(v_ptr,byte,value);
    int* int_ptr=static_cast<int*>(v_ptr);
    for(int i=0;i<10;i++){
            cout<<i_array<<endl;
    }
    system("pause");
    return 0;
}
void setvalue(void* v_ptr,short byt,unsigned char val){
     void setbyte(int*,unsigned char);
     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 char val){
     unsigned char* uc_ptr
     =reinterpret_cast<unsigned char*>(int_ptr);
     uc_ptr[0]=val;
     uc_ptr[1]=val;
}
输入byte=4,value=0
输出:
12336
12336
3
4
5
6
7
8
9
10
我觉得头两个应该都是0吧,为什么是12336呢?请不吝赐教

论坛徽章:
0
3 [报告]
发表于 2008-02-12 18:46 |只看该作者
输入的0是char ascII的代码是 0011 0000
所以在setbyte后 数组的头两个元素的值是 0011000000110000 = 12336

论坛徽章:
0
4 [报告]
发表于 2008-02-12 21:06 |只看该作者

回复 #3 jixian01 的帖子

谢谢大哥指点,我再好好想想哈

论坛徽章:
0
5 [报告]
发表于 2008-02-12 21:29 |只看该作者

回复 #3 jixian01 的帖子

还想问大哥一个题外话啊,就是我把setvalue函数的val参数从原来的unsigned char 改成unsigned short这样这个程序就对了,那么,unsigned short 占两个字节呀,那么当执行uc_ptr=val时是不是只把低位的附给了这个数组啊?

论坛徽章:
0
6 [报告]
发表于 2008-02-12 21:29 |只看该作者

回复 #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<<" ";
     }
}

论坛徽章:
0
7 [报告]
发表于 2008-02-13 04:04 |只看该作者
setbyte没看懂,好像有点问题
如果将short类型转为char类型,是只将低8位赋给char
关于类型转化 你可以看看谭浩强老师的“c 程序设计“的59页

论坛徽章:
0
8 [报告]
发表于 2008-02-13 08:36 |只看该作者

回复 #7 jixian01 的帖子

那个setbyte,应该把uc_ptr=val改成uc_ptr=val,不好意思
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP