帮忙看看哪出错了

xiaoliuliu2050 2010-12-22 09:51:50
#include<iostream>
using namespace std;
char main ()
{int a ;
cin>>a;
char b[10];
int i=0;
for(a;a!=0;a=a/16)
{if(a%16>=10)
{switch(a%16)
{case 10:b[i]='A';break;
case 11:b[i]='B';break;
case 12:b[i]='C';break;
case 13:b[i]='D';break;
case 14:b[i]='E';break;
case 15:b[i]='F';break;
default : return 0;}
}
else b[i]=a%16;
i++;
}
for (int j=i;j>=0;j--)
cout<<b[j];
return 0;
}

我是想用它实现十进制转化成十六进制数,但是不尽如人意啊 ,高手帮忙拉!
...全文
98 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
guguibin1988 2010-12-22
  • 打赏
  • 举报
回复
你好,我测试了一下你的程序,有这么几点的错误:
1,首先你定义了一个char型数组b,你是想将得到的每一位的16进制值存储在数组中,再倒着输出。这个想法可以的,但else b[i]=a%16;实现不了在屏幕输出0~9之间的数!!!假设此时i = 0,a%16=1,也就是b[0] = 1;你会发现屏幕会出现一张笑脸☺,其实你想要显示的是1.可以参考ASCII码表。通过查表发现,要在屏幕输出1,b[1]要被赋值为49,即b[1] = 49;建议修改为else b[i]=a%16 + 48;
2,还有个是数组下标的问题,修改1步骤后你输入a=16,你会发现输出的是?0(我这里是这样的结果。通过下面的分析你会发现问题所在,我也说不出这叫什么错误,,,,呵呵)。而不是10.
分析:假设a = 16,此时a!=0进入for循环,做a%16 = 0.所以执行b[0] = 48;然后i++变为1.
然后执行a=a/16,a为1,继续进入循环,a%16 = 1,执行b[1]=49;然后i++变为2.(注意此时的i值为2,也就是下面j的值为2。)
然后再执行a=/16,a为0,进不了循环了。。到这步你看下b[2]的值是不是没有呢?而下面的输出b[j],是从b[2]开始输出的,而b[2]是未知的,所以输出会错误。建议for (int j=i;j>=0;j--)改为for (int j=i-1;j>=0;j--)
3另外,你的程序还有修细节上的问题,可能不影响结果,但最好改正过来,比如缩进;
無_1024 2010-12-22
  • 打赏
  • 举报
回复
就是那两个地方 注意单个数字换成字符需要+'0'
zy020118 2010-12-22
  • 打赏
  • 举报
回复
代码内不支持改字体颜色
#include<iostream>
using namespace std;
char main ()
{
int a ;
cin>>a;
char b[10];
int i=0;
for(a;a!=0;a=a/16)
{
if(a%16>=10)
{
switch(a%16)
{
case 10:b[i]='A';break;
case 11:b[i]='B';break;
case 12:b[i]='C';break;
case 13:b[i]='D';break;
case 14:b[i]='E';break;
case 15:b[i]='F';break;
default : return 0;
}
}
else
b[i]=a%16+'0';
i++;
}

for (int j=i-1;j>=0;j--)
cout<<b[j];
return 0;
}
zy020118 2010-12-22
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;
char main ()
{
int a ;
cin>>a;
char b[10];
int i=0;
for(a;a!=0;a=a/16)
{
if(a%16>=10)
{
switch(a%16)
{
case 10:b[i]='A';break;
case 11:b[i]='B';break;
case 12:b[i]='C';break;
case 13:b[i]='D';break;
case 14:b[i]='E';break;
case 15:b[i]='F';break;
default : return 0;
}
}
else
b[i]=a%16+'0'; i++;
}

for (int j=i-1;j>=0;j--) cout<<b[j];
return 0;
}
一根烂笔头 2010-12-22
  • 打赏
  • 举报
回复
别写了,库内有现成的函数

char * ltoa ( long value, char * buffer, int radix );

Convert long integer value to string.
Converts a long integer value to a null-terminated string using the specified radix and stores the result in the given buffer.
If radix is 10 and value is negative the string is preceded by the minus sign (-). With any other radix, value is always considered unsigned.
buffer should be large enough to contain any possible value.

Parameters.

value

Value to be represented as a string.

buffer

Buffer where to store the resulting string.

radix

Numeral radix in which value has to be represented, between 2 and 36.

Return Value.
A pointer to the string.

Example.

//Example for function ltoa



#include <iostream.h>

#include <stdlib.h>



int main()

{

long num=16021977;

char buffer[40];



ltoa(num, buffer, 10);

cout<<"decimal: "<<buffer<<endl;



ltoa(num, buffer, 16);

cout<<"hexadecimal: "<<buffer<<endl;



ltoa(num, buffer, 2);

cout<<"binary: "<<buffer<<endl;



system("PAUSE");

return 0;

}



Output:



decimal: 16021977

hexadecimal: f479d9

binary: 111101000111100111011001


在满足函数要求内,你想转化成多少进制都行

64,641

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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