繁体   English   中英

C:没有错误,编译成功,但是没有运行

[英]C: no errors, successful compilation, but does not run

我正在尝试建立自己的小型图书馆,以处理超过int或double限制的巨大数字的计算。 我知道有一些有用的库可用于此目的,但我只是想自己尝试一下。

下面的代码,我找不到错误,编译器(MinGW的gcc)也找不到。 它根本无法运行。 我多次重读了该代码,但仍然无法理解为什么我的计算机拒绝运行它。 顺便说一下,我正在使用Windows 7。 任何帮助或建议,将不胜感激。

(该代码旨在打印11111 ............ 111111111.11111)

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

#define PLUS 10
#define MINUS 11

void myprint(char f[]);
int getlen(char f[]);
void daeip(char f[], char g[]);

/*
(len: the length of the array f[])
f[len - 3]: start of the number; the number part is from f[f[len - 3]] to f[0]
f[len - 2]: number of decimals
f[len - 1]: plus or minus
*/

int main(){
    char f[100], g[100];
    int i;

    for(i=0;i<50;i++){
        f[i] = '1';
    }
    f[100 - 3] = 49;
    f[100 - 2] = 5;
    f[100 - 1] = MINUS;
    daeip(f, g);
    myprint(g);

    return 0;
}

void myprint(char f[]){
    int i;
    int len = getlen(f);

    if(f[len - 1] == MINUS){
        printf("-");
    }
    for(i = f[len - 3]; i >= 0; i--){
        printf("%s", f[i]);
        if(i == f[len - 2]) printf(".");
    }
}

int getlen(char f[]){
    int len = sizeof(f)/sizeof(f[0]);
    return len;
}

void daeip(char f[], char g[]){
    int flen = getlen(f);
    int glen = getlen(g);
    int i;
    for(i=0; i <= f[flen - 3]; i++){
        g[i] = f[i];
    }
    g[glen - 3] = f[flen - 3];
    g[glen - 2] = f[flen - 2];
    g[glen - 1] = f[flen - 1];
}

您在printf语句中使用%s,该语句期望将指向字符串(char数组)的指针作为参数。 您应该使用%c。 期望使用char作为参数。 换句话说,更改:

printf("%s", f[i]); printf("%c", f[i]);

细节:

char[] test = "This is a test";
char firstCharacter = test[0];// 'T'

// The following will print out: This is a test
printf("%s", test);
// The following will be unpredictable, and may cause a runtime error. (even though it compiles)
printf("%s", firstCharacter);
// The following will print out 'T'
printf("%c", firstCharacter);

我还建议创建一个结构或类,以便您可以将长度,小数位和符号与实际数字分开。

暂无
暂无

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

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