VC++单个十进制转三进制小代码

为了解决一些实际需要做了以下的小代码,简单实用。
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
//sscanf() int sscanf(const char *buffer,const char *format,[argument ]...)以固定字符串为输入源,从一个字符串(string scanf)中读进指定的格式
//scanf()  int scanf(const char *restrict format,...) 以键盘为输入源,按照变量在内存的地址将变量存进去
//strchr() char *strchr(const char *s,char c) 查找字符串S中首次出现字符c的位置,如果s中不存在c则返回NULL
//pow() double pow(double x,double y) 计算x的y次幂
 
void main(void)
{
while(1)
{//没有循环,程序顺序执行结束,提示“按任意键继续”后退出控制台
int a,b,c,d,e=0,j=0;
// double y=0;
char str[10]=""; //输入数组
char EnterKey=' ';
int *pIntputNumber;
/////////////////////////////////////////////////////////////Input Limit
InputAgain:
printf("输入十进制数:");
scanf("%s",str); //先存成字符串
 
if(strchr(str,'.') == NULL)
{//没有找到小数点则等于NULL
sscanf(str,"%d",&a);   //再从字符串格式化成整型
}
else
{//有小数点
goto InputAgain;
}
 
pIntputNumber=&a;
if(*pIntputNumber>50000)
{
goto InputAgain;
}
if(*pIntputNumber<=0)
{
goto InputAgain;
}
/////////////////////////////////////////////////////////////Decimal Conversion
if(a<3)
{
b=a;
}
else
{//a大于3
c=a;
do
{
d=c%3;
e+=d*pow(10,j);
c/=3;
j++;
}
while(c>=3);
}
b=c*pow(10,j)+e;
printf("所求三进制数:%d\n",b);
}
/////////////////////////////////////////////////////////////
end:
// system("pause"); //从程序中调用系统命令(和shell命令),此处调用pause命令
system("exit");  //此处调用exit命令
// return 0;
}
永不止步步 发表于12-22 10:07 浏览65198次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

永不止步步
金币:67410个|学分:302267个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号