【问题标题】:How to check the correctness of the input binary number contains digits different from 0 and 1 in c?如何检查输入的二进制数是否包含c中不同于0和1的数字?
【发布时间】:2020-10-06 07:15:51
【问题描述】:

我写了一个函数,提示用户输入0-s和1-s的二进制数序列, 输出以下内容。

A) 二进制数的十进制表示

B) 二进制数的十六进制表示(以 16 为基数)

如果输入无效(即 bin_num 包含不同于 0 和 1 的数字),用户应该 收到一条错误消息(详情如下)。

程序运行示例:

Please enter binary number input: 112011

invalid input, please try again.

Please enter binary number input: 110011

110011 to decimal is: 51

110011 to hexadecimal is: 0x33

为了正确解决问题,我将无法将二进制数读取为 整数,因为它可能会导致溢出。 另一方面,答案(十进制)将始终符合标准整数大小。

我的问题是如果二进制数(用户输入)包含不同于 0 和 1 的数字,如何检查正确性?

我尝试了一些方法,但它不起作用。

这是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define ENTERING_QUESTION "Please choose a question by entering 1-5 (enter 0 to exit):" 


#define MAX_SIZE_INPUT 31
#define QUESTION1_INPUT_MESSAGE "Please enter binary number input:"
#define QUESTION1_OUTPUT_MESSAGE_DECIMAL "to decimal is:"
#define QUESTION1_OUTPUT_MESSAGE_HEXADECIMAL "to hexadecimal is:"
#define QUESTION1_ERROR_MESSAGE "invalid input, please try again."
void bin2hexanddec(char *bin_str)
{       
        for (int i = 0; i < MAX_SIZE_INPUT; i++) {
        if (bin_str[i] != '0' && bin_str[i] != '1') {
            printf(QUESTION1_ERROR_MESSAGE"\n");
            return;
        }
    }
    char *ptr;
    long long input_num= strtol(bin_str, &ptr, 10);
    int dec = 0, i = 0, rem;
    
    while (input_num != 0) {
        rem = input_num % 10;
        input_num /= 10;
        dec += rem * pow(2, i);
        ++i;
    }
    printf("%lld " QUESTION1_OUTPUT_MESSAGE_DECIMAL " %d\n", input_num, dec);
    printf("%lld " QUESTION1_OUTPUT_MESSAGE_DECIMAL " %X\n", input_num, dec);

    return;

}

.
.
.
int main()
{

    char bin_str[MAX_SIZE_INPUT
    .
    .
    .
    printf(ENTERING_QUESTION"\n");
    scanf("%d", &choice);
        if (choice == 1) {
            printf(QUESTION1_INPUT_MESSAGE"\n");
            scanf("%s", bin_str);
            bin2hexanddec(bin_str);
        }
    .
    .
    .


    return 0;
}

【问题讨论】:

    标签: c binary bitwise-operators


    【解决方案1】:

    您可以使用库函数strspn 来检查字符串是否只有二进制数字,如本例所示:

    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char str[100];
        if(fgets(str, sizeof str, stdin) == NULL) {
            // handle error
            return 1;
        }
        str[ strcspn(str, "\n") ] = 0;   // remove trailing newline
    
        if (strspn(str, "01") == strlen(str)) {
            puts("Binary digits");
        }
        else {
            puts("Not binary");
        }
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      您检查的字符太多:

      for (int i = 0; i < MAX_SIZE_INPUT; i++) {
      

      如果输入字符串小于MAX_SIZE_INPUT,则您正在读取已写入字符串的末尾。你反而想要:

      for (int i = 0; i < strlen(bin_str); i++) {
      

      另外,要转换数字,而不是将其读取为基数 10 并对其进行转换,只需告诉 strtol 使用基数 2。您还可以检查 ptr 是否指向末尾的空字节检查无效字符的字符串。

      void bin2hexanddec(char *bin_str)
      {  
          char *ptr;
          int input_num= strtol(bin_str, &ptr, 2);
          if (!*ptr) {
              printf(QUESTION1_ERROR_MESSAGE"\n");
              return;
          }
      
          printf("%s " QUESTION1_OUTPUT_MESSAGE_DECIMAL " %d\n", bin_str, input_num);
          printf("%s " QUESTION1_OUTPUT_MESSAGE_DECIMAL " %X\n", bin_str, input_num);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-19
        • 2021-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 2011-08-12
        相关资源
        最近更新 更多