繁体   English   中英

我编写了一个 C 程序来在十进制、十六进制和二进制之间进行转换

[英]I have written a C program to convert between decimal, hex and binary

我似乎无法正确获取十六进制部分,我得到了正确的 output 但 output 一直在屏幕上显示,并且在我点击 ^z 之前永远不会停止。 尚未能够测试代码的 rest。
/* 这个程序将十进制转换为二进制 * 或十六进制 */

#include<stdio.h>
#include <string.h>

/*function prints binary of decimal n*/
void binary(unsigned n)
{
/* step 1 recurse dividing by 2*/
if (n > 1)
    bin(n / 2);

/* step 2 print bit*/
printf("%d", n % 2);
}


int main(int argc, char const *argv[])
{
char i;
int c;
/*    run till ^d entered
*/
while ((i = scanf("%d", &c)) != EOF) {

    /*        check for argument flag
    */
    if (strcmp(argv[1], "-x") == 0)
    {
        /*convert to hexadecimal and print*/
        int decimal = c;
        /*read decimal as flag if ^d is not present in flag*/
        printf("%s", "0x" );
        /*starting 0x formatting for hex*/
        printf("%x", decimal);
        /*new line for next input*/
        printf("\n");


    } else if (strcmp(argv[1], "-b") == 0)
    {
        /*            convert to binary and print
        */
        int decimal;
        /*read decimal as flag if ^d is not present in flag*/

        decimal = c;
        binary(decimal);
        /*new line for next input*/
        printf("\n");
    } else {
        /*            prompt help, incorrect flag argument
        */
        printf("%s\n", "Usage: ./convert [-x |-b]");
        break;
    }


}
return 0;
}

我认为您的意思是这样写:

#include <stdio.h>
#include <string.h>

/*function prints binary of decimal n*/
void binary(unsigned n)
{
    /* step 1 recurse dividing by 2*/
    if (n > 1)
        binary(n / 2);

    /* step 2: print bit*/
    printf("%d", n % 2);
}


int main(int argc, char const * argv [])
{
    int num;
    signed char should_run = 1;

    // make sure that an argument was given
    if (argc == 1) {
        fprintf(stderr, "Please enter an argument!\n");
        should_run = 0;
    }

    // run until ^d is entered (or an error happened)
    while (scanf("%d", &num) > 0 && should_run) {

        // convert to hex
        if (strcmp(argv [1], "-x") == 0) {
            printf("0x%x\n", num);
        } // convert to binary
        else if (strcmp(argv [1], "-b") == 0) {
            binary(num);
            printf("\n");
        } // print usage
        else {
            printf("%s\n", "Usage: ./convert [-x |-b]");
            should_run = 0;
        }
    }
    return 0;(
}

一般改进提示

在访问之前检查数组的长度!

您忘记检查用户是否提供了您的 arguments 之一( -x-h )。 一般来说,这会导致“复活节彩蛋”(未定义的行为;)),因为您假设argv中至少有 2 个元素!

改进错误处理

好吧,我的“更正”代码也不是一个很好的例子,我认为这只是在练习,但还是要提一下:注意可能出现的错误。 在这种情况下,我的意思是scanf function。 最好在它的返回值上添加一些检查。

不要将字符串拆分为多个 printf 语句

您在代码中写道:

 /*read decimal as flag if ^d is not present in flag*/
        printf("%s", "0x" );
        /*starting 0x formatting for hex*/
        printf("%x", decimal);
        /*new line for next input*/
        printf("\n");

也可以改为单行: printf("0x%x\n", decimal) ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM