如何检查一些使用该程序进入基地? [英] How to check the base of a number entered using this program?

查看:164
本文介绍了如何检查一些使用该程序进入基地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序转换输入的号码到所需的基地。但我想核对一下电话号码的present基地。例如:基地1是3和输入的号码是4,从而它是不可能的。我写的函数来检查。但是,这是行不通的。哪里是我的错吗?

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;INT检查(INT tmp_number,诠释source_base)
{
    INT检查= 0;
    字符味精[tmp_number]
    sprintf的(味精,%D,tmp_number);
    INT I = 0;
    如果(source_base> tmp_number)
    {
    返回1;
    }
 而(tmp_number)
    {        如果(检查< source_base)
        {
        检查= tmp_number%10;
        tmp_number / = 10;
        我++;
        }    }
     如果(sizeof的(MSG)=​​= I)
     {
     返回1;
     }
    其他
     {
     返回0;
     }}诠释的main()
{    INT tmp_number = 1;
    INT十进制= 0;
    INT source_base,target_base;
    个char [9];
    INT I = 0;
    做
    {
        的printf(请输入BASE1 \\ n);
    scanf函数(%d个,&安培; source_base);    的printf(输入号码\\ n);
    scanf函数(%d个,&安培; tmp_number);
    }而(检查(tmp_number,source_base)!);    的printf(请输入BASE2 \\ n);
    scanf函数(%d个,&安培; target_base);
     而(tmp_number)
    {
    十进制+ =(tmp_number%10)* POW(source_base,I);
    tmp_number / = 10;
    ++我;
    }
    itoa(十进制,S,target_base);
    的printf(结果为%s \\ n,S);   的getchar();
    的getchar();返回1;
}


解决方案

 字符味精[tmp_number]

创建一个真正的大阵,和的sizeof(MSG)(你的电话号码,而不是你号码的长度值的大小)== I 将只当数字是完全正确的 1 。你可以通过查找号码长度和创造大小的数组解决您的code,但它会更好重写它有点像这样:

  INT检查(INT tmp_number,诠释source_base)
{    诠释检查;
    而(tmp_number)
    {
        检查= tmp_number%10;
        tmp_number / = 10;        如果(检查> = source_base)
            返回0;    }    返回1;}

My program converts the entered number to a desired base. But I want to check the present base of the number. For example : Base 1 is 3 and entered number is 4 so it isn't possible. I wrote the function to check that. But it doesn't work. Where is my fault ?

#include <stdlib.h>
#include <stdio.h>

int check(int tmp_number , int source_base)
{
    int check = 0 ;
    char msg[tmp_number];
    sprintf(msg, "%d", tmp_number);
    int i=0;
    if(source_base>tmp_number)
    {
    return 1;
    }
 while( tmp_number )
    {

        if(check<source_base)
        {
        check =  tmp_number % 10 ;
        tmp_number /= 10 ;
        i++;
        }

    }
     if (sizeof(msg)==i)
     {
     return 1;
     }
    else
     {
     return 0;
     }

}

int main()
{

    int tmp_number=1;
    int decimal=0;
    int source_base,target_base;
    char s[9];
    int i=0;
    do
    {
        printf("Enter the base1\n");
    scanf("%d",&source_base);

    printf("Enter the number\n");
    scanf("%d",&tmp_number);
    } while (!check(tmp_number,source_base));

    printf("Enter the base2\n");
    scanf("%d",&target_base);


     while( tmp_number )
    {
    decimal += (tmp_number % 10) * pow( source_base, i ) ;
    tmp_number /= 10 ;
    ++i ;
    }
    itoa(decimal, s, target_base);
    printf("The result is %s\n", s);

   getchar();
    getchar();

return 1; 
}

解决方案

char msg[tmp_number];

Creates a really big array (the size of the value of your number, not the length of your number), and sizeof(msg) == i will only be true when the number is exactly 1. You could fix your code by finding the length of the number and creating an array of that size, but it would be better to rewrite it a bit like this:

int check(int tmp_number , int source_base)
{

    int check;
    while( tmp_number )
    {
        check = tmp_number % 10;
        tmp_number /= 10 ;

        if(check >= source_base)
            return 0;

    }

    return 1;

}

这篇关于如何检查一些使用该程序进入基地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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