【问题标题】:Overwrite a string in C覆盖C中的字符串
【发布时间】:2015-05-01 07:41:02
【问题描述】:

我正在尝试创建一个代码,将小数转换为 2 到 16 之间的任何基数。但我不知道如何在我的字符串中写入新值。

void base_conversion(char s[], int x, int b) {
int j, y;
j = 0;
// your code here
while(b > 1 && b < 17) {

    if (x < 0) {
        x = (-x);
    }else if(x == 0) {
        s[j] = '\0';
    }
        x = (x/b);

    while(x > 0) {

        y = (x%b);


        if (y == 10) {
            s[j] = 'A';
        }else if(y == 11) {
            s[j] = 'B';
        }else if(y == 12) {
            s[j] = 'C';
        }else if(y == 13) {
            s[j] = 'D';
        }else if(y == 14) {
            s[j] = 'E';
        }else if(y == 15) {
            s[j] = 'F';
        }else{
            s[j] = y;
        }
    }
}j = j + 1;
}

【问题讨论】:

  • 你对你的问题做了哪些研究?
  • 首先你在任何循环之外增加j,所以你总是会覆盖s[0]。其次,您没有在内部循环中修改xb,所以如果x 不为零,那么您就有一个无限循环。第三,您能否详细说明您的问题(除了我提到的问题)?对于某些输入,预期和实际输出是多少?
  • 您不需要while() 循环来测试基础b:基础有效或无效 - 在函数入口处。
  • while (b &gt; 1 &amp;&amp; b &lt; 17) { /* code that does not change b and does not have a break statement */ } 是一个无限循环。
  • 另外,请学习如何使用调试器。在调试器中逐行执行代码会使我提到的所有问题都非常明显。

标签: c string loops while-loop base-conversion


【解决方案1】:

您几乎完成了,虽然有几个错误,所以我已经“改进”了您的代码。无限循环测试只需要完成一次的基础。 while() 循环的组织不完全正确 - x/b 在数字提取循环之外完成。我所做的另一项更改是使用查找数组将每个数字转换为字符,这样可以节省大量费力的测试。我还返回了作为函数值传递的字符串 - 不妨添加更多功能。在传递一个错误的基值的情况下,我可以返回NULL 而不是一个空字符串。另请注意,我在将 j 用作索引的相同语句中更新了它,这使代码更加流畅。

#include <stdio.h>

char *base_conversion (char *s, int x, int b) {
    char charr[] = "0123456789ABCDEF";
    int i, j = 0, len, digit, neg = 0;
    *s = 0;                     // terminate the string passed
    if (b < 2 || b > 16)        // check the base
        return s;               // return an empty string
    if (x < 0) {
        x = -x;                 // adjust for negative input
        neg = 1;
    }
    do {
        digit = x % b;          // extract each l.s. digit
        s[j++] = charr [digit]; // convert to character
    } while (x /= b);           // implicitly test for 0

    if (neg)                    // negative input
        s[j++] = '-';           // append a minus sign
    s[j] = 0;                   // terminate the string

    // reverse the string
    len = j;
    for (i=0; i<len/2; i++) {
        digit = s[i];
        s[i] = s[--j];          // pre-decrement j to next char index
        s[j] = digit;
    }
    return s;
}    

int main () { 
    int n;
    char strig[65];
    for (n=1000; n>=-1000; n-=2000) {
        printf ("Binary      %d: %s\n", n, base_conversion (strig, n, 2)); 
        printf ("Ternary     %d: %s\n", n, base_conversion (strig, n, 3)); 
        printf ("Octal       %d: %s\n", n, base_conversion (strig, n, 8)); 
        printf ("Decimal     %d: %s\n", n, base_conversion (strig, n, 10)); 
        printf ("Hexadecimal %d: %s\n", n, base_conversion (strig, n, 16)); 
    }
    return 0;
}

程序输出:

Binary      1000: 1111101000
Ternary     1000: 1101001
Octal       1000: 1750
Decimal     1000: 1000
Hexadecimal 1000: 3E8
Binary      -1000: -1111101000
Ternary     -1000: -1101001
Octal       -1000: -1750
Decimal     -1000: -1000
Hexadecimal -1000: -3E8

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多