免费注册 查看新帖 |

Chinaunix

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

[函数] 请教个函数作用...真的有点恶心了. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-03 18:05 |只看该作者 |倒序浏览
从早上到晚上的看别人遗留下来的代码,实在有点恶心了,浑身脑袋疼,哪位朋友有空帮我看看

int ntc(char *s, char *t, int type,int len,int dec)
{
        register i;
        char *q;
        int zero=1,l,p,neg=0;
        char r[2000+1];
        int mdy[3];

        int y,m,d;


        t [0] = 0;

        if (type==FVARL)
        {
                strcpy( t, s );
                return 0;
        }

        if (len<=0)
                return 1;
        else if (len>2000)
        {
                memcpy( t, s, len );
                t[len] = 0;
                return 0;
        }

        memcpy( r, s, len );
        r[len] = 0;

        if (type == FINT || type == FFLOAT || type == FMONEY )
        {
                if (r[len-1]>'9')
                {
                        if (r[len-1]==0x7d)
                                r[len-1] = '0';
                        else   
                                r[len-1] -= 0x19;
                        neg=1;
                }
        }
      
    switch(type)
        {
        case FCHAR:
        case FDGTCH:
        case FPASS:      
                memcpy( t, r, len );
                t[len] = 0;
                break;

        case FINT:
                l=0;
                if (neg)
                        t[l++]='-';
                for (i=0; i<len; i++)
                {
                        if ((r=='0'||r==' ') && zero) continue;
                        zero=0;
                        t[l++]=r;
                }
                if (l==neg)
                {
                        t[0] = '0';
                        l = 1;
                }
                t[l] = '\0';
                    break;

        case FFLOAT:
        case FMONEY:
                l=0;
                if (neg) t[l++]='-';
                for (i=0; i<len-dec; i++)
                {
                        if ((r=='0'||r==' ') && zero) continue;
                        zero=0;
                        t[l++]=r;
                }
                if (l==neg)
                {
                        t[neg] = '0';
                        l++;
                }
                t[l++]='.';
                if (dec!=0) memcpy( t+l, r+i, dec );
                *(t+l+dec) = 0;
                if (t[l]=='\0')
                {
                        t[l++]='0';
                        t[l++]='0';
                        t[l]='\0';
                }
                break;

        case FDATE:
                get_date(&y,&m,&d);

                r[len] = 0;
                if (r[0]=='\0' || r[0]==' ')
                {
                        t[0]=0;
                        break;
                }
                switch (len)
                {
                case 4:
                        sscanf(r,"%02d%02d",&mdy[0],&mdy[1]);
                        mdy[2] = y;
                        break;
                case 6:
                        sscanf(r,"%02d%02d%02d",&mdy[2],&mdy[0],&mdy[1]);
                        mdy[2] += (mdy[2]>=80) ? 1900 : 2000 ;
                        break;
                case 8:
                default:
                        sscanf(r,"%04d%02d%02d",&mdy[2],&mdy[0],&mdy[1]);
                        mdy[2] += (mdy[2]<100) ? 1900 : 0 ;
                        break;
                }
                sprintf(t,"%04d%02d%02d",mdy[2],mdy[0],mdy[1]);
                break;
        }

        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2008-09-03 19:32 |只看该作者
原帖由 zhangqqqf 于 2008-9-3 18:05 发表
从早上到晚上的看别人遗留下来的代码,实在有点恶心了,浑身脑袋疼,哪位朋友有空帮我看看

int ntc(char *s, char *t, int type,int len,int dec)
{
        register i;
        char *q;
        int zero=1,l,p,neg=0;
        cha ...


哈哈!

论坛徽章:
0
3 [报告]
发表于 2008-09-03 19:43 |只看该作者
彼此彼此,
BTW:函数没函数说明
如:
Description
file change log:

论坛徽章:
0
4 [报告]
发表于 2008-09-03 20:39 |只看该作者
读代码要耐心读。如果想训练自己的读代码能力,欢迎阅读GCC。
PS:这段代码中的名字以及一些pattern还是很好辨认的,平时看代码的时候留心些。
/* There is a string in the *s, parse it, put result into *t.
* type is the TYPE of the data in *s, maybe(see the below Macros)
* len is the length of string *s
* if the data is a float or money, dec means the decision of the float number
*/

int ntc(char *s, char *t, int type,int len,int dec)
{
        register i;
        char *q;
        int zero=1,l,p,neg=0;
        char r[2000+1];
        int mdy[3];

        int y,m,d;


        t [0] = 0;

        if (type==FVARL) // If type is FVARL, copy src to target and return

        {
                strcpy( t, s );
                return 0;
        }

        if (len<=0) // len must >0, or return failed

                return 1;
        else if (len>2000) // if len > 2000, just copy src to target

        {
                memcpy( t, s, len );
                t[len] = 0;
                return 0;
        }

        memcpy( r, s, len ); //if 0<len<=2000, analyze it

        r[len] = 0;

        if (type == FINT || type == FFLOAT || type == FMONEY )
        {
                if (r[len-1]>'9')
                {
                        if (r[len-1]==0x7d) // if last char is '}', change it to '0'

                                r[len-1] = '0';
                        else   
                                r[len-1] -= 0x19;//minux 0x19 ? I,J,K...

                        neg=1;   // if last char > '9', set this flag, negative amount

                }
        }
      
    switch(type)
        {
        case FCHAR:
        case FDGTCH:
        case FPASS:      
                memcpy( t, r, len ); //this three type just copy to target

                t[len] = 0;
                break;

        case FINT:
                l=0;
                if (neg)
                        t[l++]='-';
                for (i=0; i<len; i++)
                {
                        // There may be problem, should be r\[i\] ... r\[i\]

                        if ((r=='0'||r==' ') && zero) continue; //skip leading ' ' and '0'

                        zero=0;
                        // There may be problem, should be r

                        t[l++]=r;
                }
                if (l==neg) //if no other digit put '0' at the first place

                {
                        t[0] = '0';
                        l = 1;
                }
                t[l] = '\0'; //put a tail 0

                    break;

        case FFLOAT:
        case FMONEY:
                l=0;
                if (neg) t[l++]='-';
                for (i=0; i<len-dec; i++) //dec means decision of floating point number

                        // There may be problem

                        // should be r\[i\] ... r\[i\]...

                        if ((r=='0'||r==' ') && zero) continue;// like in FINT

                        zero=0;
                        // There may be problem, should be r

                        t[l++]=r;
                }
                if (l==neg)
                {
                        t[neg] = '0';
                        l++;
                }
                t[l++]='.'; //floating point

                if (dec!=0) memcpy( t+l, r+i, dec );
                *(t+l+dec) = 0;
                if (t[l]=='\0') //if no number after floating point, put 2 '0'

                {
                        t[l++]='0';
                        t[l++]='0';
                        t[l]='\0';
                }
                break;

        case FDATE:
                get_date(&y,&m,&d); //get_date get the current year,month,day


                r[len] = 0;
                if (r[0]=='\0' || r[0]==' ') // if no char or first char is ' ', get out

                {                             // donot change target

                        t[0]=0;
                        break;
                }
                switch (len)
                {
                case 4:
                        sscanf(r,"%02d%02d",&mdy[0],&mdy[1]); //r has MMDD,put them in mdy

                        mdy[2] = y; //current year

                        break;
                case 6:
                        sscanf(r,"%02d%02d%02d",&mdy[2],&mdy[0],&mdy[1]);//r has YYMMDD

                        mdy[2] += (mdy[2]>=80) ? 1900 : 2000 ; // if year >=80, it is 19xx

                        break;                                 // otherwise, it is 20xx

                case 8:
                default: //r has YYYYDDMM

                        sscanf(r,"%04d%02d%02d",&mdy[2],&mdy[0],&mdy[1]);
                        mdy[2] += (mdy[2]<100) ? 1900 : 0 ; // if year < 100, add 1900

                        break;
                }
                sprintf(t,"%04d%02d%02d",mdy[2],mdy[0],mdy[1]);// put date in target

                break;
        }

        return 0;
}


[ 本帖最后由 freearth 于 2008-9-3 20:44 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-09-03 21:10 |只看该作者
主要原因应该是注释太少,

加上工作时间太长,有时候大家都会这样
register i;
        char *q;
        int zero=1,l,p,neg=0;
        char r[2000+1];
        int mdy[3];

        int y,m,d;
参数也要多设为可读性的,那就更好

[ 本帖最后由 qliu00 于 2008-9-4 23:17 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-09-03 21:11 |只看该作者

回复 #1 zhangqqqf 的帖子


在网络上贴公司代码,你很可能已经违反了公司的保密协议了哦.

论坛徽章:
1
双子座
日期:2015-01-04 14:25:06
7 [报告]
发表于 2008-09-04 09:20 |只看该作者
LZ应该提高自己的水平

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
8 [报告]
发表于 2008-09-04 09:33 |只看该作者
直接测试一下这个函数的功能,然后结合代码看函数

论坛徽章:
1
15-16赛季CBA联赛之深圳
日期:2016-07-07 22:34:24
9 [报告]
发表于 2008-09-04 09:38 |只看该作者
没有一点注释说明,看着是比较头疼

论坛徽章:
0
10 [报告]
发表于 2008-09-04 15:37 |只看该作者
原帖由 freearth 于 2008-9-3 20:39 发表
读代码要耐心读。如果想训练自己的读代码能力,欢迎阅读GCC。
PS:这段代码中的名字以及一些pattern还是很好辨认的,平时看代码的时候留心些。
/* There is a string in the *s, parse it, put result into * ...


thank you.. very much.... ; )
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP