将十进制数转换为二进制、八进制、十六进制的问题

ftefzs 2011-07-02 10:46:30

# include<stdio.h>
# include<string.h>
void main()
{ long n;
int radix;
char b[33];
char *q;
char * trans(long m,int base); // 函数声明

printf("please input the integer:"); //要转换的十进制数
scanf("%ld",&n);
printf("please input the radix:"); // 进制数,可以为2、8、16
scanf("%d",radix);

q=trans(n,radix); //调用函数
for(int i=strlen(b)-1;i>=0;i--)
printf("%c",*(b+i));


}

char *trans(long m,int base)
{ int r;
char a[33];
char *p=a;

while(m>0)
{ r=m%base;
if(r<10) *p=r+48; //小于10的数转换为数字字符输出
else *p=r+55; //大于10的数转为A B C D E F输出
m=m/base;
p++; }
*p='\0';
p=a;

return p;
}


为什么编译能通过,可就是不能执行出结果呢?问题出在哪呢?是指针的问题吗?
请大家帮帮忙好吗。。。
...全文
1498 60 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
60 条回复
切换为时间正序
请发表友善的回复…
发表回复
li08240418 2011-09-29
  • 打赏
  • 举报
回复
henhao 很强大
竞择 2011-07-04
  • 打赏
  • 举报
回复
[Quote=引用 48 楼 masmaster 的回复:]
这种问题,不外乎自己多想想! 编译器只能检测到语法的错误,对程序逻辑错误,它是100%不管的。
[/Quote]+++
fdisksys 2011-07-04
  • 打赏
  • 举报
回复
[Quote=引用 57 楼 zhao4zhong1 的回复:]

引用 55 楼 zhao4zhong1 的回复:
C/C++ code
#include <stdio.h>
#include <stdlib.h>
void main() {
long n;
int radix;
char b[33];

while (1) {
printf("Input a integer(>=0):");//要转换的十进制数
fflush(……

……
[/Quote]
这个卖瓜的吗?
赵4老师 2011-07-04
  • 打赏
  • 举报
回复
[Quote=引用 55 楼 zhao4zhong1 的回复:]
C/C++ code
#include <stdio.h>
#include <stdlib.h>
void main() {
long n;
int radix;
char b[33];

while (1) {
printf("Input a integer(>=0):");//要转换的十进制数
fflush(……
[/Quote]
正解!(^_^)
ftefzs 2011-07-04
  • 打赏
  • 举报
回复
谢谢以上各位大牛的指点,因为C语言水平处于初级水平,所以很多地方不明白,各位多多包涵啊~~
赵4老师 2011-07-04
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
void main() {
long n;
int radix;
char b[33];

while (1) {
printf("Input a integer(>=0):");//要转换的十进制数
fflush(stdout);
rewind(stdin);
if (1==scanf("%ld",&n)) {
if (n>=0) break;
}
}
while (1) {
printf("Input radix(2..36):");//进制,可以为2..36
fflush(stdout);
rewind(stdin);
if (1==scanf("%d",&radix)) {
if (2<=radix && radix<=36) break;
}
}
printf("%d(10)==%s(%d)\n",n,_itoa(n,b,radix),radix);
}
5t4rk 2011-07-03
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
#define ok 1
#define inter int
typedef struct node
{
int data;
struct node *next;
}lnode,*linknode; //结点的定义
inter emptystack(linknode top) //***************************** 栈空与否判断函数
{
if(top==NULL)
return 1;
else
return 0;
}
inter pushstack(linknode &top,int in) //****************** 栈的压入操作函数
{
linknode p;
p=(linknode)malloc(sizeof(lnode));
p->data=in;
p->next=top;
top=p; //栈顶指针往上移动
return in;
}
inter popstack(linknode &top)//3****************** 栈的弹出操作函数
{
linknode p;
int out;
out=top->data;
p=top;
top=top->next; //栈顶指针往下移动
free(p);
return out;
}
int main(void)
{
system("cls");
system("color 2b");
int src=0;
cout<<"请用户输入你要转换的数:"<<endl;
cin>>src;
int n=0;
cout<<"请用户输入你要转换的进制:"<<endl;
cin>>n;
linknode Lnode=NULL;
while (src!=0)
{
pushstack(Lnode,src%n);
src=src/n; //循环检查是否除完
}
cout<<"转换的结果是:"<<endl;
while (!emptystack(Lnode))
{
cout<<popstack(Lnode);
}

cout<<endl;
system("pause");
return 1;
}

看看我的写吧
希望有用
半人马座星星 2011-07-03
  • 打赏
  • 举报
回复
你忘记了在 radix 前加&
  • 打赏
  • 举报
回复
楼主,我在你的程序的基础上增加了很多循环和判断,来增加程序的健壮性,你参考一下吧,
还有,楼主,一定要注意风格,你的程序风格很烂,希望你从现在开始就好好改改,养成一个
良好的习惯。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define BIN_BIT 33

void trans(long numChang,int changBase); // 函数声明

void main()
{
long numChang;
int changBase;
char binBit[BIN_BIT],
inChoice,
outChoice;

do
{
//////////////////////////////输入十进制正整数块开始////////////////////////////////
do
{
printf("输入一个十进制正整数: \n"); //要转换的十进制数
scanf(" %ld",&numChang);
if (numChang < 0)
{
printf("\n输入错误,请输入一个十进制正整数: \n");
}
else
break;
} while (1);
///////////////////////////////输入十进制正整数块结束///////////////////////

//////////////////////////////////////////////////////////////////////////
do
{
///////////////////////////输入要转换的进制块开始///////////////////////////
do
{
printf("您要转换成什么进制?(2、8、16): \n"); // 进制数,可以为2、8、16
scanf(" %d", &changBase);
if (changBase != 2 && changBase != 8 && changBase != 16)
{
printf("\n输入错误,只能将十进制转换成2、8、16进制,请重新输入: \n");
}
else
break;
} while (1);
/////////////////////////////输入要转换的进制块结束//////////////////////////
trans(numChang, changBase);

printf("还需要继续转换成其他进制吗?(y/n): \n");
scanf(" %c", &inChoice);
if (inChoice == 'Y' || inChoice == 'N')
{
inChoice = tolower(inChoice);
}
if (inChoice == 'n')
break;
else if (inChoice == 'y')
continue;
else
printf("\n\n输入错误,请重新输入: \n");

} while (1);
//////////////////////////////////////////////////////////////////////////
printf("\n\n继续输入其他十进制来进行转换吗?(y/n): \n");
scanf(" %c", &outChoice);
if (outChoice == 'Y' || outChoice == 'N')
{
outChoice = tolower(outChoice);
}
if (outChoice != 'y' && outChoice != 'n')
{
printf("\n\n输入错误,请重新输入: \n");
}
if (outChoice == 'n')
break;
else if (outChoice == 'y')
continue;

} while (1);
//////////////////////////////////////////////////////////////////////////
}

//////////////////////////////////////////////////////////////////////////
void trans(long numChang,int changBase)
{
int r,
i = 0,
bitLen;
char binBit[BIN_BIT];

printf("\n十进制数%ld转换成%d进制数,转换后,结果为:", numChang, changBase);
while(numChang > 0)
{
r = numChang % changBase;
if(r < 10)
binBit[i++] = r + '0'; //小于10的数转换为数字字符输出
else
binBit[i++] = r + 'A'- 10; //大于10的数转为A B C D E F输出
numChang = numChang / changBase;
}
binBit[i] = '\0';
bitLen = strlen(binBit);

//////////////////////////////////////////////////////////////////////////
if (changBase == 16)
{
printf("0X");
}
else if (changBase == 8)
{
printf("0");
}
else if (changBase == 2)
{
if (bitLen == 1)
printf("000000");
else if (bitLen == 2)
printf("00000");
else if (bitLen == 3)
printf("0000");
else if (bitLen == 4)
printf("000");
else if (bitLen == 5)
printf("00");
else if (bitLen == 6)
printf("0");
else if (bitLen == 7)
printf("0");
}
//////////////////////////////////////////////////////////////////////////
for(i = bitLen-1; i >= 0; i--)
printf("%c", binBit[i]);

printf("\n\n");
system("pause");
printf("\n");
}


masmaster 2011-07-03
  • 打赏
  • 举报
回复
这种问题,不外乎自己多想想! 编译器只能检测到语法的错误,对程序逻辑错误,它是100%不管的。
nong1988 2011-07-03
  • 打赏
  • 举报
回复
每日一帖,不忘csdn
heibaiyijing 2011-07-03
  • 打赏
  • 举报
回复
看来一下你的程序,你把子函数写在主函数后面,最好要加一个声明
hackbuteer1 2011-07-02
  • 打赏
  • 举报
回复
这个可以的。。
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void Trans10_2_8_16(char *p,long m,int base)
{
int r;
while(m>0)
{
r=m%base;
if(r<10)
*p=r+48; //二进制、八进制(字符0的ASCII码是:48)
else
*p=r+55; //十六进制(字符A的ASCII码是:65)
m=m/base;
p++;
}
*p='\0';
}
int main(void)
{
int i,base;
long n;
char a[33];
printf("请输入一个需要转换的正整数:");
scanf("%ld",&n);
printf("请输入一个进制(2,8,16):");
scanf("%d",&base);
Trans10_2_8_16(a,n,base);
printf("转化为%d进制数为:",base);
for(i=strlen(a)-1;i>=0;i--)
printf("%c",*(a+i));
printf("\n");
system("pause");
return 0;
}
ftefzs 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 terhack 的回复:]

不管定义成啥的。变量作用域就是从最近的{这个开始,然后匹配的}结束
[/Quote]

大概知道了,还要自己消化消化,谢谢啊^_^
ftefzs 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 kangqi7000 的回复:]

应该是可以的,你注意你的这个循环 //for(int i=strlen(b)-1;i>=0;i--)
// printf("%c",*(b+i)); ……
[/Quote]


是哦 我本来是想 用q来指向数组b[]的,然后对*q的操作就是对数组b[]操作 忘了写
kangqi7000 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ftefzs 的回复:]

哦。。我懂了 谢谢啊!!
不过我又想到一个问题,如果 把a[] 定义为静态的,那那个内存不就不会消失了吗?可是这样做 程序运行后,还是不行啊....这样不可以吗?
[/Quote]
应该是可以的,你注意你的这个循环 //for(int i=strlen(b)-1;i>=0;i--)
// printf("%c",*(b+i)); 这个是对b进行操作的,但是main函数中你trans并没有改变b什么啊
2401_863541072 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ftefzs 的回复:]
哦。。我懂了 谢谢啊!!
不过我又想到一个问题,如果 把a[] 定义为静态的,那那个内存不就不会消失了吗?可是这样做 程序运行后,还是不行啊....这样不可以吗?
[/Quote]
不管定义成啥的。变量作用域就是从最近的{这个开始,然后匹配的}结束
ftefzs 2011-07-02
  • 打赏
  • 举报
回复
哦。。我懂了 谢谢啊!!
不过我又想到一个问题,如果 把a[] 定义为静态的,那那个内存不就不会消失了吗?可是这样做 程序运行后,还是不行啊....这样不可以吗?
2401_863541072 2011-07-02
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ftefzs 的回复:]
可是。。。trans函数调完,a就释放内存了。但是最后已经把a传给了p,然后调完只返回p就可以了吧...不关a的事了吧...这样不正确吗?
[/Quote]
因为p指向了a指向的内存,如果a释放了,那么a那块内存就没了你想要的内容。所以P也就没有意义了。
ftefzs 2011-07-02
  • 打赏
  • 举报
回复
可是。。。trans函数调完,a就释放内存了。但是最后已经把a传给了p,然后调完只返回p就可以了吧...不关a的事了吧...这样不正确吗?
加载更多回复(17)

70,020

社区成员

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

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