The Fourth Dimension Space

枯叶北风寒,忽然年以残,念往昔,语默心酸。二十光阴无一物,韶光贱,寐难安; 不畏形影单,道途阻且慢,哪曲折,如渡飞湍。斩浪劈波酬壮志,同把酒,共言欢! -如梦令

鼎嵌杯决赛第二题 模拟Modbus协议

最近日子过得很纠结啊 早上6:30跑到四工去占座 发现星期六 早上四工 6:00就开门了 好位子没占到 幸好还是占到了2个空调房的座位 回来吃个早饭 开始鼎嵌杯 。。。第一题还算简单吧 ,1Y了,不过第二题就纠结了,CRC好办,那个浮点数的转化确实很麻烦,我只能硬着头皮模拟之,IEEE32是啥?组原学过。。。只是一下子没反应过来和我们经常用的那个IEEE753是一个东西。。。 代码写得很水 不知道有没有什么改进的方法?
#include<cstring>
#include
<cmath>
#include
<algorithm>
#include
<cstdio>
using namespace std;

int crc=(1<<16)-1;//注意每一个case后crc要归位

#define  xor 0xA001
void trans(int a)
{
    crc
=(a^crc);
    
int cnt=8;
    
while(cnt--)
    
{
        
if(crc%2==1)
        
{
            
            crc
=(crc>>1);
            crc
=(crc^xor);
        }

        
else
        
{
            crc
=(crc>>1);
        }

    }


}


void myswap()
{
    
int i;
    
for(i=0;i<=7;i++)
    
{
        
if((crc&(1<<i))!=0&&(crc& ( 1<< (i+8 ) ) )==0)
        
{
            crc
-=1<<i;
            crc
+=1<< (i+8 ) ;
        }

        
else if((crc&(1<<i))==0&&(crc& ( 1<< (i+8 ) ) )!=0)
        
{
            crc
+=1<<i;
            crc
-=1<< (i+8 );
        }

    }

}


int bin[100];
void tobin(unsigned int n)
{
    memset(bin,
0,sizeof(bin));
    
int p=-1;
    
while(n!=0)
    
{
        p
++;
        bin[p]
=n%2;
        n
/=2;
    }

    reverse(bin,bin
+32);
}

char s[200];
char s2[200];//装浮点数信息
char s3[200];//装最后4位信息

int n;//记录浮点数个数

void departstring(char s[])
{

    
int len=strlen(s);
    n
=(len-4)/8;
    
int i,j;
    
for(i=6,j=0;i<len-4;i++,j++)
        s2[j]
=s[i];
    
for(i=len-4,j=0;i<len;i++,j++)
        s3[j]
=s[i];
}


int getCRC(char s[])
{
    
char t[100];
    crc
=0xFFFF;
    
int nn=(strlen(s)-4)/2;
    
int i,j;
    
int tt;
    
int len=strlen(s);
    
for(i=0;i<len-4;i+=2)
    
{

        t[
0]=s[i];
        t[
1]=s[i+1];
        t[
2]='\0';
        sscanf(t,
"%X",&tt);
        trans(tt);
    }

    myswap();
    
return crc;
}


double getfloatnum(int k)//取出第i个浮点数,这里i从1到n
{
    
char t[200]={0};
    
int i,j;
    
for(i=(k-1)*8,j=0;i<(k-1)*8+8;i++,j++)
        t[j]
=s2[i];
    unsigned 
int nn;
    sscanf(t,
"%X",&nn);
    tobin(nn);
    
int exp=0;
    
for(i=8;i>=1;i--)
    
{

        
if(bin[i]==1)
            exp
+=(int)pow((double)2,8-i);
    }

    exp
-=127;//获得右移位数

    
int zhengshu=0;
    
double xiaoshu=0;

    
int dotpos=8+exp;//小数点在bin[pos]这一位后面

    
for(i=dotpos,j=0;i>8;i--,j++)
    
{
        
if(bin[i]==1)
            zhengshu
+=(int)pow((double)2,j);
    }

    zhengshu
+=(int)pow((double)2,j);

    
for(i=dotpos+1,j=1;i<32;i++,j++)
    
{
        
if(bin[i]==1)
            xiaoshu
+=pow(0.5,j);
    }

    
if(bin[0]==1)
        
return -(zhengshu+xiaoshu);
    
else return zhengshu+xiaoshu;
}





int main()
{
    
int a,b,c,d;
    
int zhengshu=0;
    
double xiaoshu=0;
    
double res;
    
int getcrc;


    
    
while(scanf("%d,%d,%d,%d",&a,&b,&c,&d)!=EOF)
    
{
        
int i,j;
        memset(s,
0,sizeof(s));
        memset(s2,
0,sizeof(s2));
        memset(s3,
0,sizeof(s3));
        res
=0;

        
int tem=0X00FF;
        crc
=(1<<16)-1;
        trans(a);
        trans(b);
        trans((c
>>8));
        trans((c
&tem));
        
        trans((d)
>>8);
        trans((d
&tem));
        
        myswap();
        printf(
"%02X%02X%04X%04X%04X\n",a,b,c,d,crc);


        
//////////////////////////////////////////////////////////////////////////
        scanf("%s",s);
        departstring(s);
        getcrc
=getCRC(s);
        
//printf("%X\n",getcrc);
        int tt;
        sscanf(s3,
"%X",&tt);

        
if(tt!=getcrc)
        
{
            printf(
"CRC_ERROR\n");
            
continue;
        }


        
for(i=1;i<n;i++)
            printf(
"%.1lf,",getfloatnum(i));
        printf(
"%.1lf\n",getfloatnum(i));
    }

            

    
return 0;
}

#define  xor 0xA001 这个地方有点奇怪,如果我改成int xor=0xA0001,我的编译器能过,但是交上去却CE。。。
系统提示这个:
Main.cpp
Main.cpp(10) : error C3193: '^' : requires '/clr' command line option
Main.cpp(10) : error C2059: syntax error : '='
Main.cpp(21) : error C2059: syntax error : ')'
算了,现在弄不明白了,下次再说吧,赶紧自习去。。。

posted on 2010-01-16 20:21 abilitytao 阅读(1797) 评论(7)  编辑 收藏 引用

评论

# re: 鼎嵌杯决赛第二题 模拟Modbus协议 2010-01-16 21:07 OwnWaterloo

只要知道处理器使用的是IEEE754, 比如i386, 直接:

float toIEEE754(uint32_t x)
{

union {
uint32_t src;
float dst;
} u = x;
return u.dst;

}

即可。



顺便说说…… 题目出得很垃圾…… 语言都不通顺……
傻了吧唧的……
  回复  更多评论   

# re: 鼎嵌杯决赛第二题 模拟Modbus协议 2010-01-16 21:44 MasterLuo

sscanf可以直接将字符串变为符点数  回复  更多评论   

# re: 鼎嵌杯决赛第二题 模拟Modbus协议 2010-01-16 23:01 abilitytao

@OwnWaterloo
这个方法怎么调用呢?要内嵌汇编吗?  回复  更多评论   

# re: 鼎嵌杯决赛第二题 模拟Modbus协议 2010-01-17 00:35 OwnWaterloo

@abilitytao
这个…… 不就是普通的C函数么?

uint32_t x = 0;
float f = 0.0f;
sscanf("420B999A", "%x", &x );
f = toIEEE754(x);
printf("%.1f", f);
34.9  回复  更多评论   

# re: 鼎嵌杯决赛第二题 模拟Modbus协议 2010-01-17 00:42 abilitytao

@OwnWaterloo
奇怪,在我的vs2008上面怎么显示
c:\documents and settings\administrator\桌面\鼎嵌杯决赛\b.cpp(261) : error C3861: “toIEEE754”: 找不到标识符
???  回复  更多评论   

# re: 鼎嵌杯决赛第二题 模拟Modbus协议 2010-01-17 20:14 OwnWaterloo

@abilitytao
不知道了…… 要有错, 也应该是uint32_t 没有定义才对……

float toIEEE754(unsigned x)
{

typedef int constraint[sizeof(unsigned)==sizeof(float)? 1:-1];

union {
unsigned src;
float dst;
} u = x;

return u.dst;

}  回复  更多评论   

# re: 鼎嵌杯决赛第二题 模拟Modbus协议 2010-01-19 13:29 lky

欢迎看我的题解:

http://blog.csdn.net/LuckilyYu/archive/2010/01/17/5204348.aspx  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理