正在写一个信号源程序,求一个str2num程序,先谢了!!!

mycrazycracy 2010-06-05 07:26:26
我在写一个C51程序,前些天求了个str2num的程序,最近发现我的num2str可能有些问题,想请大家帮忙想下。谢谢!!
要求函数原型为unsigned char num2str(double in_num,char* str_out,bit flag) 其中flag表示是否转化小数部分,为1不转化,只输出整数;返回值为字符串占用字节数(不计入'\0').
...全文
74 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
东莞某某某 2010-06-06
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


unsigned char num2str(double in_num,char* str_out,int flag);

int main()
{
double d=3.0213;
char str_out[64]={0};
int l=num2str(d, str_out,2);
printf("%s,%d\n",str_out,l);
return 0;
}

unsigned char num2str(double in_num,char* str_out,int flag)
{
if( 1==flag )
{
return sprintf(str_out,"%d",(int)in_num);
}
else
{
if( in_num==(int)in_num )
itoa(in_num,str_out,10);
else
gcvt(in_num,12,str_out);

return strlen(str_out);
}

}


不知道是否满足
zhusizhi007 2010-06-06
  • 打赏
  • 举报
回复
楼主去查一下LINUX下的头文件,string.h string2.h等看看应该有你要的东东。。。或是fcvt,gcvt,ecvt这些函数吧
「已注销」 2010-06-06
  • 打赏
  • 举报
回复
flag==1时转化小数,直接调用sprintf。flag==0时不转化小数,将number取整后调用sprintf


#include <stdio.h>

int main ()
{
char buffer [50];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a %d char long string\n",buffer,n);
return 0;
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧