- 注册时间
- 2004-8-29
- 最后登录
- 1970-1-1
|
本来是想写一组函数用struct在GVmaker中实现浮点数(小数),但用LAVAX3.1IDE写了开始部分后,却发现在GVmakerIDE中的解释器上运行不了.而且LAVAX3.1IDE中的虚拟机上显示"含有新版函数...",如果是使用了struct做函数参数造成的,那在GVmaker只能用数组实现浮点数了...所以不想再写下去了.
把代码贴出来仅供菜鸟参考之用.
- #define DEBUG 1<BR>#define FLEN 11<BR>#define FSTR 15<BR>#define FPRE 6 //<=FLEN-1<BR>//浮点数设计: str 为能打印出来的表达式,buf 为实际数据<BR>// 打印出来的为_prec位有效数字,实际保存FLEN位有效数字<BR>// 如果打印的数表达式长度超过FSTR位,则用科学表达式表示(x.xxxxxEy)<BR>struct Float{<BR> char str[FSTR+1];<BR> char buf[FLEN]; //从0~6,按从高位到低位顺序,其中buf[0]为原数字中左边第一个非零值<BR> int exp; //位<BR> char len; //有效数字的长度<BR> char sign; //0 if>=0<BR>};<BR>int _prec=FPRE; //显示的精度,默认为FPRE(>=1&&<=FLEN-1)<BR>//******************************************************<BR>long SetFValue(struct Float& f,long s);<BR>long SetFString(struct Float& f);<BR>long SetF(struct Float& f,long s);<BR>int SetPrec(int p);<BR>//******************************************************<BR>int SetPrec(int p)//设置显示的精度<BR>{<BR> if(p<=1) _prec=1;<BR> else if(p>=FLEN-1)_prec=FLEN-1;<BR> else _prec=p;<BR> return _prec;<BR>}<BR>long SetFString(struct Float& f){<BR>/*函数说明:<BR> 前置条件:f 已被赋值<BR> 功能 :将f.str赋予正确的表达式<BR>*/<BR> int i,j,k;<BR> memset(f.str,0,FSTR+1);<BR> if(!f.len){<BR> f.str[0]='0';<BR> return 1;<BR> }<BR> i=0;<BR> if(f.sign) f.str[i++]='-';<BR> if(f.exp>=0){<BR> if(f.exp<_prec){//能够直接显示<BR> for(j=0;j<_prec&&j<f.len;j++){<BR> if(j==f.exp+1) f.str[i++]='.';<BR> f.str[i++]=f.buf[j]+'0';<BR> }<BR> for(;j<=f.exp;j++) f.str[i++]='0';<BR> }<BR> else{<BR> for(j=0;j<_prec&&j<f.len;j++){<BR> if(j==1) f.str[i++]='.';<BR> f.str[i++]=f.buf[j]+'0';<BR> }<BR> sprintf(f.str+i,"E%d",f.exp);<BR> }<BR> }<BR> else{<BR> if(_prec-f.exp+3<=FSTR){<BR> memcpy(f.str+i,"0.",2);<BR> i=i+2;<BR> memset(f.str+i,'0',-f.exp-1);<BR> i=i-f.exp-1;<BR> for(j=0;j<_prec&&j<f.len;j++) f.str[i++]=f.buf[j]+'0';<BR> }<BR> else{<BR> for(j=0;j<_prec&&j<f.len;j++){<BR> if(j==1) f.str[i++]='.';<BR> f.str[i++]=f.buf[j]+'0';<BR> }<BR> sprintf(f.str+i,"E%d",f.exp);<BR> }<BR> } <BR> return 1;<BR>} <BR>long SetFValue(struct Float& f,long s){<BR>//函数说明:用字符串给f赋值,出错返回0<BR> int i,j,k,pos; //pos为有效数字最右边的位置<BR> i=0;<BR> pos=-1;<BR> f.exp=-1;<BR> f.len=0;<BR> k =0;<BR> if(*s=='+'){<BR> f.sign=0;<BR> i++;<BR> }<BR> else if(*s=='-'){<BR> f.sign=1;<BR> i++;<BR> }<BR> else<BR> f.sign=0;<BR> while(*(s+i)=='0') i++;<BR> while(*(s+i)!='.'&&k<FLEN&&*(s+i)!=0){<BR> if(*(s+i)!='0') pos=k;<BR> f.buf[k]=*(s+i)-'0';<BR> f.exp++;<BR> k++;<BR> i++;<BR> }<BR> if(*(s+i)==0){<BR> while(k<FLEN){<BR> f.buf[k]=0;<BR> k++;<BR> }<BR> }<BR> else if(k>=FLEN){<BR> while(*(s+i)!='.'&&*(s+i)!=0){<BR> f.exp++;<BR> i++;<BR> }<BR> }<BR> else{<BR> i++;<BR> if(k==0){<BR> while(*(s+i)=='0'&&*(s+i)!=0){<BR> i++;<BR> f.exp--;<BR> }<BR> }<BR> while(*(s+i)!=0&&k<FLEN){<BR> if(*(s+i)!='0') pos=k;<BR> f.buf[k]=*(s+i)-'0';<BR> k++;<BR> i++;<BR> }<BR> if(*(s+i)==0){<BR> while(k<FLEN){<BR> f.buf[k]=0;<BR> k++;<BR> }<BR> }<BR> }<BR> pos++;<BR> f.len=pos;<BR> if(pos==0){<BR> f.exp=0;<BR> f.sign=0;<BR> }<BR> return 1;<BR>}<BR>long SetF(struct Float& f,long s)<BR>{<BR> if(!SetFValue(f,s)) return 0;<BR> if(!SetFString(f)) return 0;<BR> return 1;<BR>}<BR>void main()<BR>{<BR> struct Float f1,f2;<BR> int k;<BR> SetScreen(1);<BR> SetF(f1,"0.000000000001235566");<BR> SetF(f2,"123463613321.0121");<BR>#ifdef DEBUG<BR> if(f1.sign) printf("-");<BR> for(k=0;k<FLEN;k++) printf("%d",f1.buf[k]);<BR> printf("\n~%d~%d~%d",f1.sign,f1.exp,f1.len);<BR>#endif<BR> printf("\nf1=%s",f1.str);<BR> printf("\nf2=%s",f2.str);<BR> SetPrec(9);<BR> SetFString(f1);<BR> SetFString(f2);<BR> printf("\nf1=%s",f1.str);<BR> printf("\nf2=%s",f2.str);<BR> getchar();<BR>}
复制代码 <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR>
[upload=txt]viewFile.asp?ID=4287[/upload]<BR> |
|