| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4981 人关注过本帖
标题:怎样将十六进制转换为十进制 用递归的方法
只看楼主 加入收藏
雪松2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:1
注 册:2010-5-22
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:9 
怎样将十六进制转换为十进制 用递归的方法
怎样将十六进制转换为十进制 用递归的方法
已经有预设代码
#include"stdio.h"
int main()
{  char s[10];
   unsigned long n, htod();
   gets(s);
   n = htod(s);
   printf("%u\n",n);
}
搜索更多相关主题的帖子: 十进制 十六进制 递归 
2010-06-11 16:45
myhnuhai
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:3
帖 子:425
专家分:1725
注 册:2010-3-17
收藏
得分:2 
小数部分不能计算:
#include"stdio.h"
#include<string.h>
#include<math.h>
int main()
{  char s[10];
    int x;
   unsigned long htod(char a[],int n);
   gets(s);
   x=strlen(s);
   printf("%u\n",htod(s,x));
}
unsigned long htod(char a[],int n)
{
    int i,sum=0;
    for(i=0;i<n;i++)
    sum+=(int)pow(a[i],n-i-1);
    return sum;
}

不要让肮脏的记忆,迷失了原本纯洁的心灵!
2010-06-11 17:59
雪松2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:1
注 册:2010-5-22
收藏
得分:0 
但是这样就修改了预设代码  预设代码是不能修改的啊
2010-06-11 20:50
kettle99
Rank: 2
等 级:论坛游民
帖 子:52
专家分:54
注 册:2010-5-25
收藏
得分:2 
以下是引用myhnuhai在2010-6-11 17:59:36的发言:

小数部分不能计算:
#include"stdio.h"
#include
#include
int main()
{  char s[10];
    int x;
   unsigned long htod(char a[],int n);
   gets(s);
   x=strlen(s);
   printf("%u\n",htod(s,x));
}
unsigned long htod(char a[],int n)
{
    int i,sum=0;
    for(i=0;i
这不对吧?a[i]为字符,如1为整型的49而不是1,这能对吗?楼上的给解释解释呵呵

[ 本帖最后由 kettle99 于 2010-6-12 13:03 编辑 ]
2010-06-12 13:02
雪松2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:1
注 册:2010-5-22
收藏
得分:0 
#include"stdio.h"
#include"math.h"
unsigned long htod(char s[])
{ int i,j,k;
  unsigned a[20];
  unsigned long r=0,m;
  i=0;
  for(i=0;s[i]!='\0';i++);
  for(j=0;j<=i-1;j++)
   { if(s[j]>=48&&s[j]<=57)
      a[j]=s[j]-48;
      if(s[j]>=65&&s[j]<=90)
      a[j]=s[j]-55;}
  for(k=0;k<=i-1;k++)
   { m=pow(16,i-1-k);
     r=r+a[k]*m;}
 return(r);
}
main()
{  char s[10];
    unsigned long n,htod();
    gets(s);
    n=htod(s);
    printf("%u\n",n);
}

这个是正确的 但是为什么在DEV C++上不能编译呢   比如说 输入FF 输出255
2010-06-12 13:13
kettle99
Rank: 2
等 级:论坛游民
帖 子:52
专家分:54
注 册:2010-5-25
收藏
得分:0 
#include"stdio.h"
#include"math.h"
int main()
{  char s[10];
   unsigned long n, htod();
   gets(s);
   n = htod(s);
   printf("%u\n",n);
}

unsigned long htod(char *s)

{
            int i,j;
            long int sum;
            sum=0;
            i=strlen(s);
            for(i=i-1,j=0;i>=0;i--,j++)
                {
                    if(s[j]>='0'&&s[j]<='9')
                    sum=(s[j]-48)*pow(16,i)+sum;
                    else if(s[j]>='a'&&s[j]<='f')
                    sum=(s[j]-87)*pow(16,i)+sum;
                    else if(s[j]>='A'&&s[j]<='F')
                    sum=(s[j]-55)*pow(16,i)+sum;
                }
            return sum;

}
2010-06-12 13:32
kettle99
Rank: 2
等 级:论坛游民
帖 子:52
专家分:54
注 册:2010-5-25
收藏
得分:0 
以下是引用雪松2009在2010-6-12 13:13:57的发言:

#include"stdio.h"
#include"math.h"
unsigned long htod(char s[])
{ int i,j,k;
  unsigned a[20];
  unsigned long r=0,m;
  i=0;
  for(i=0;s!='\0';i++);
  for(j=0;j<=i-1;j++)
   { if(s[j]>=48&&s[j]<=57)
      a[j]=s[j]-48;
      if(s[j]>=65&&s[j]<=90)
      a[j]=s[j]-55;}
  for(k=0;k<=i-1;k++)
   { m=pow(16,i-1-k);
     r=r+a[k]*m;}
 return(r);
}
main()
{  char s[10];
    unsigned long n,htod();
    gets(s);
    n=htod(s);
    printf("%u\n",n);
}

这个是正确的 但是为什么在DEV C++上不能编译呢   比如说 输入FF 输出255
我用的是Turbo C
2010-06-12 13:38
qq8801103
Rank: 5Rank: 5
来 自:苏州中科大软件学院
等 级:职业侠客
威 望:1
帖 子:422
专家分:340
注 册:2009-10-8
收藏
得分:2 
用栈也可以

Discuz!  
好好学习  天天向上
2010-06-12 15:21
monk02698
Rank: 1
等 级:新手上路
帖 子:13
专家分:7
注 册:2010-5-31
收藏
得分:2 
这里有个2 10 16互转的
#include "stdio.h"
#include "math.h"
main()
{
  int error,i,j,k,number_b,number_d,temp;
char type;
char number_copy[60],number[60];
error=1;
number_d=0;
printf("please choose type,1--Binery,2--Decimal,3--Hex\n");
scanf("%c",&type);
while((type!='1')&&(type!='2')&&(type!='3'))
   {
       printf("type error,please input the type again\n");
       scanf("%c",&type);
   }





switch (type)
   {
       case '1':
           {
               printf("\nplease input the binery number:");
               scanf("%s",number);
        /*判断二进制输入是否合法,不合法重新输入*/      
               while (error==1)
                 {
                   for(i=0;number[i]!='\0';i++)
                     {
                         if((number[i]!='0')&&(number[i]!='1'))
                            {
                                error=1;
                                break;
                            }
                         else
                         error=0;
                     }
                  if(error==0)
                  break;
                  else
                     {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
                 }
         
  /*输入的二进制转换为十进制和十六进制输出*/        
          for(i=i-1,j=0;i>=0;i--,j++)
            {
                number_d=(number[j]-48)*pow(2,i)+number_d;
               }
          printf("\nthe decimal number is:%d\nthe hex number is:%X",number_d,number_d);     
           }
       break;
      
      
       case '2':
           {
               printf("\nplease input the decimal number:");
               scanf("%s",number);
        /*判断二进制输入是否合法,不合法重新输入*/  
            
             while(error==1)
               {
                   for(i=0;number[i]!='\0';i++)
                     {
                       if(number[i]<'0'||number[i]>'9')
                         {
                             error=1;
                        break;
                         }
                       else
                       error=0;
                     }
                   if(error==0)
                   break;
                   else
                   {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
               }
           
/*输入的十进制转换为二进制和十六进制输出*/        
          for(i=i-1,j=0;i>=0;i--,j++)
            {
                number_d=(number[j]-48)*pow(10,i)+number_d;
            }
          printf("\nthe hex number is:%X",number_d);  
         
          for(i=0;number_d!=0;i++)
            {
                number[i]=number_d%2+48;
                number_d=number_d/2;
               }         

          for(j=i-1,k=0;k<(i-1)/2+1;j--,k++)
            {
                temp=number[j];
                number[j]=number[k];
                number[k]=temp;
               }
           
          printf("\nthe binary number is:%s",number);   
           }
       break;
      
      
       case '3':
           {
               printf("\nplease input the hex number:");
               scanf("%s",number);
               /*判断十六进制输入是否合法,不合法重新输入*/
               while(error==1)
               {
                   for(i=0;number[i]!='\0';i++)
                     {
                       if((number[i]>='0'&&number[i]<='9')||(number[i]>='a'&&number[i]<='f')||(number[i]>='A'&&number[i]<='F'))
                         {
                             error=0;
                         }
                       else
                        {
                            error=1;
                            break;
                        }
                     }
                   if(error==0)
                   break;
                   else
                   {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
               }
          /*输入的十六进制转换为二进制和十进制输出*/
         
                  for(i=i-1,j=0;i>=0;i--,j++)
                {
                    if(number[j]>='0'&&number[j]<='9')
                    number_d=(number[j]-48)*pow(16,i)+number_d;
                    else if(number[j]>='a'&&number[j]<='f')
                    number_d=(number[j]-87)*pow(16,i)+number_d;
                    else if(number[j]>='A'&&number[j]<='F')
                    number_d=(number[j]-55)*pow(16,i)+number_d;
                }
            
              printf("\nthe decimal number is:%d",number_d);  
              
              for(i=0;number_d!=0;i++)
                {
                    number[i]=number_d%2+48;
                    number_d=number_d/2;
                }      
         
            
          for(j=i-1,k=0;k<(i-1)/2+1;j--,k++)
            {
                temp=number[j];
                number[j]=number[k];
                number[k]=temp;
               }
         
           
          printf("\nthe binary number is:%s",number);     
           }
       break;
      
       }

}
2010-06-12 23:24
雪松2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:1
注 册:2010-5-22
收藏
得分:0 
#include"stdio.h"
#include"math.h"
int main()
{  char s[10];
   unsigned long n, htod();
   gets(s);
   n = htod(s);
   printf("%u\n",n);
}

unsigned long htod(char *s)

{
            int i,j;
            long int sum;
            sum=0;
            i=strlen(s);
            for(i=i-1,j=0;i>=0;i--,j++)
                {
                    if(s[j]>='0'&&s[j]<='9')
                    sum=(s[j]-48)*pow(16,i)+sum;
                    else if(s[j]>='a'&&s[j]<='f')
                    sum=(s[j]-87)*pow(16,i)+sum;
                    else if(s[j]>='A'&&s[j]<='F')
                    sum=(s[j]-55)*pow(16,i)+sum;
                }
            return sum;

}
回复6楼的
 E:\未命名1.cpp In function `int main()':
6 E:\未命名1.cpp too many arguments to function `long unsigned int htod()'  
8 E:\未命名1.cpp at this point in file
 E:\未命名1.cpp In function `long unsigned int htod(char*)':
22 E:\未命名1.cpp [Warning] converting to `long int' from `double'
在我的电脑上就会知道会这样 完全不能编译 很是恼火 不知哪位知道这是为什么
2010-06-13 12:52
快速回复:怎样将十六进制转换为十进制 用递归的方法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018603 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved