从一个基础到另一个基础的转换 [英] conversions from one base to another

查看:115
本文介绍了从一个基础到另一个基础的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序未提供输出..
请告诉我我要去哪里错了...
我已经在前几行中编写了程序逻辑,将最多十位数的数字从任何基数转换为任何基数...(基数范围:2-20)

Following program doesn''t give the output..
Please tell where am i going wrong...
I have written the program logic in first few lines, Convert a number of maximum ten digit from any base to any base...( base range :2-20)

// This program converts given no. to decimal.
// then it converts decimal no. to source base.
//checks if the new no. and initial no. are same
//if same, converts the decimal no. into target base
//if not, again asks for input
#include<stdio.h>
#include<conio.h>
#include<math.h>          //for pow function
#include<string.h>        //for strcmpi

long int cnvrtdeci(char *, int);              //convert into decimal
int chkbase(long int, int, char*);            //checks base
char* cnvrt(long int,int);                    //convert into target base

void main()
{
    char num[11],*p;
    long int m;
    int base=0,trgt, c,i;
    clrscr();
    back:
    printf("\nInput:");
    scanf("%s", num);
    scanf("%d %d", &base, &trgt);
    m=cnvrtdeci(&num[0], base);
    c=chkbase(m, base, &num[0]);
    if(c==1)
    {
       printf("\nWrong input!!");
       goto back;
    }
    p = cnvrt(m,trgt);
    printf("\nOutput:");
    for (; *p!=''\0'';p++)
       printf("%s", *p);
    getch();
}
long int cnvrtdeci(char *j,int b)
{
    char *i;
    long int sum=0;
    int k;
    while( *j != ''\0'' )
      j++;
    for( i=j-1,k=0; i>=0; i--,k++)
    {
      switch(*i)
      {
        case ''A'':
        case ''a'': sum+=10 * pow(b,k);
              break;
        case ''B'':
        case ''b'': sum+=11 * pow(b,k);
              break;
        case ''C'':
        case ''c'': sum+=12 * pow(b,k);
              break;
        case ''D'':
        case ''d'': sum+=13 * pow(b,k);
              break;
        case ''E'':
        case ''e'': sum+=14 * pow(b,k);
              break;
        case ''F'':
        case ''f'': sum+=15 * pow(b,k);
              break;
        case ''G'':
        case ''g'': sum+=16 * pow(b,k);
              break;
        case ''H'':
        case ''h'': sum+=17 * pow(b,k);
              break;
        case ''I'':
        case ''i'': sum+=18 * pow(b,k);
              break;
        case ''J'':
        case ''j'': sum+=19 * pow(b,k);
              break;
        default: sum+= (*j) * pow(b,k);
       }

    }
    return sum;
}
int chkbase( long int m,int base,char *p)
{
    char *j;
    int t;
    j= cnvrt(m,base);
    t= strcmpi ( j,p);
    if( t!=0)
         return 1;
    else
         return 0;
}

char* cnvrt( long int m, int t)
{
    char res[50];
    int r,j;
    for( j=48;m>=0;j--)
    {
      r= m%t;
      m= m/t;
      if ( r>=0 && r<=9 )
       res[j]=r;
      else
       switch(r)
       {
         case ''10'': res[j] = ''A'';
            break;
         case ''11'': res[j] = ''B'';
            break;
         case ''12'': res[j] = ''C'';
            break;
         case ''13'': res[j] = ''D'';
            break;
         case ''14'': res[j] = ''E'';
            break;
         case ''15'': res[j] = ''F'';
            break;
         case ''16'': res[j] = ''G'';
            break;
         case ''17'': res[j] = ''H'';
            break;
         case ''18'': res[j] = ''I'';
            break;
         case ''19'': res[j] = ''J'';
            break;
         }
      }
         return &res[0];
}

推荐答案

很难确定您所遇到的问题,因为您没有真正告诉我们:程序没有" t给出输出"并不完全是诊断.

所以:我将解释几个明显的问题,然后继续介绍如何治愈它.但是请注意,我不会为您修复它-这是您的作业,是修复它的工作!

1)次要,但重要.不要仅仅提示用户输入"Input:".要求他输入要转换的数字,该数字所在的基数,然后输入他想要的基数.对用户来说好多了!
2)转到.请从您的代码中删除它,并忘记它已经存在两年了.之后,您应该了解为什么初学者不应该使用它.相信我.
3)此代码可能永远不会退出.或者至少不是在合理的时间范围内.
It''s difficult to be certain what the problem you have is, since you don''t really tell us: "program doesn''t give the output" is not exactly diagnostic.

So: I''ll explain a couple of obvious nasties, and then move on to how to cure it. Be aware though, that I am not going to fix it for you - it''s your homework, it''s your job to fix it!

1) Minor, but important. Don''t just prompt the user for "Input:". Ask him to enter the number to convert, the base it is in, and then the base he wants. A lot nicer for the user!
2) Goto. Please remove this from your code, and forget for two years that it ever existed. After that, you should understand why beginners should never use it. Trust me on this.
3) This code may never exit. Or at least, not in a reasonable timescale.
for( j=48;m>=0;j--)
{
  r= m%t;
  m= m/t;

为什么不呢?答案:零除以非零数字的结果是什么?
4)请帮个忙:重命名变量以描述变量的实际含义.使用单个字符名称可以节省键入时间,但是在两周内,如果您想算出j是尝试转换为基数或循环计数器的中间结果,那么将导致严重的头疼.再次,请相信我!

现在:修复它.

启动调试器.
在两个scanf调用之后的第一行上放置一个断点:

Why not? Answer: what is the result of zero divided by a non-zero number?
4) Please, do yourself a favour: rename your variables to be descriptive of what they actually are there for. Using single character names save you typing, but in two weeks time, it will cause serious head-scratchiong as you try to work out if j is supposed to be the interim result of trying a conversion to a base, or a loop counter. Again, trust me on this!

Now: fixing it.

Kick up the debugger.
Place a breakpoint on the first line after the two scanf calls:

m=cnvrtdeci(&num[0], base);


运行程序,直到遇到断点.
看一下变量"num"和"base".在调用cnvrtdeci后,请您自己计算出"m"中的值.现在,单步调试器完成调用. "m"的值是您的预期吗?
如果没有,为什么会有所不同?重新启动程序,这次进入例程.

继续这样做:弄清您的期望,然后看看您是否得到了.如果您这样做,那就太好了!如果不这样做,为什么不呢?
您将在开发中做很多工作-现在值得一试!
祝你好运!


Run your program until the breakpoint is hit.
Look at you variables "num" and "base". Work out in your own mind what value should be in "m" after cnvrtdeci has been called. Now single step the debugger over the call. Is the value of "m" what you thought it would be?
If not, why is it different? Restart the program, and this time step into the routine.

Keep doing this: Work out what you expect, then see if you get it. If you do, good! If you don''t, why not?
You will be doing this a lot in development - it is well worth getting comfortable doing it now!
Good luck!


这篇关于从一个基础到另一个基础的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆