续功率功能消息

时间:2018-10-03 14:23:53

标签: c pow

我一直收到错误消息,说我对幂函数有未定义的引用,但是我不确定是在哪里发生的,或者为什么我的代码会出现该错误,因为我以前曾对幂函数进行过运算通过这种方式。如果有人可以帮助我弄清楚为什么现在不起作用,我将非常感激。

#include "stdio.h"
#include "string.h" //Needed for strlen()
#include "math.h"

#define MAX_BITS 32
#define MAX_LENGTH 49
#define NUMBER_TWO 2
#define NUMBER_ONE 1
#define TERMINATOR '\0'

//Code to find the index of where the string ends
int last_index_of(char in_str[], char ch) {
        for (int i = 0; i < MAX_LENGTH; i++) {
                if(in_str[i] == ch) {
                        last_index_of == i;
                }
        }
        return last_index_of;
}

//Code to find the start of the fractional aspect
void sub_string(char in_str[], char out_str[], int start, int end){
        int i = 0;
        while (i < 1) {
                out_str[i] = in_str[start] + in_str[end-1];
                i++;
        }
}

int main()
{
        //Declaration of variable
        char input[MAX_LENGTH +1]; // +1 for '\0'
        int number;
        double exponent;
        char output[MAX_BITS];
        int fraction;

 sub_string(input, output, 0, TERMINATOR);

        //Input from the user
        printf("Enter a floating point value in binary: ");
        scanf("%s", input);

        //Calculates the Decimal Part
        for (int i = 0; i < last_index_of(input, TERMINATOR) ; i++) {
                number = number + number + input[i];
        }
        printf("%d", number);

        exponent = -1;
        //Calculates the Fractional Part
        for (int j = 0; j < last_index_of(input, TERMINATOR); j++) {
                if (j == last_index_of) {
                        fraction = NUMBER_ONE/(pow(NUMBER_TWO, exponent));
                        printf("%d/n", fraction);
                }
                else {
                        fraction = NUMBER_ONE/(pow(NUMBER_TWO, exponent));
                        printf("%d + ", fraction);
                        exponent--;
                }
        }

        return 0;

}

1 个答案:

答案 0 :(得分:2)

一些问题:

  • 您需要-lm选项来链接,以告诉它在哪里可以找到pow函数
  • last_index_of书写不正确,您将函数名称用作内部变量,可以通过以下方式对其进行纠正:

    //Code to find the index of where the string ends
    int last_index_of(char in_str[], char ch) {
        int ret = 0;
        for (int i = 0; i < MAX_LENGTH; i++) {
            if(in_str[i] == ch) {
                ret = i;
            }
        }
        return ret;
    }
    

    请注意,您可以将last_index_of()函数替换为strlen()

  • 如注释中所指出,
  • sub_string()不是功能性的。更正后的版本可能是:

    //Code to find the start of the fractional aspect
    void sub_string(char in_str[], char out_str[], int start, int end){
        int i = 0;
        while (start != end) {
            /* warning, bounds are still not tested...*/
            out_str[i++] = in_str[start++];            
        }
        out_str[i] = '\0'
    }
    
  • 与其在现有的last_index_of()循环条件中调用for,不如将其值重新使用:

    for (int j = 0; j < last_index_of(input, TERMINATOR); j++) {
        /* Error here: will never be TRUE */
        if (j == last_index_of) {
            /* ... */   
        }
        else {
            /* ... */   
        }
    }
    

    将成为:

    int last_index = last_index_of(input, TERMINATOR);
    for (int j = 0; j < last_index; j++) {
        if (j == last_index) {
            /* ... */   
        }
        else {
            /* ... */   
        }
    }
    
  • 另一个问题是,您使用number变量而不对其进行初始化,应该编写int number = 0而不是int number;


在那之后,您的逻辑也有问题。

您对要执行的操作有所了解,但是代码中不清楚。

您似乎想要

  • 用户以10010.100111的形式输入一些字符串
  • 将此字符串分为两部分10010100111
  • 将第一部分转换为整数部分10010-> 18
  • 将第二部分转换为小数部分100111-> 0.609...

这种分解可能会导致您编写这种代码:

#include "stdio.h"
#include "string.h"

#define MAX_BITS 32
#define MAX_LENGTH 49

//Code to find the index of where the string ends
int last_index_of(char in_str[], char ch)
{
    int ret = 0;
    for (int i = 0; i < MAX_LENGTH; i++) {
        if (in_str[i] == ch) {
            ret = i;
        }
    }
    return ret;
}

void sub_string(char in_str[], char out_str[], int start, int end)
{
    int i = 0;
    while (start != end) {
        /* warning, bounds are still not tested... */
        out_str[i++] = in_str[start++];
    }
    out_str[i] = '\0';
}

void split(char *input, char *first, char *second)
{
    int idx = last_index_of(input, '.');

    sub_string(input, first, 0, idx);

    sub_string(input, second, idx + 1, strlen(input));

}

int main()
{
    //Declaration of variable
    char input[MAX_LENGTH + 1]; // +1 for '\0'
    char first[MAX_BITS];
    char second[MAX_BITS];

    /* Input from the user */
    printf("Enter a floating point value in binary: ");
    scanf("%s", input);

    /* split integer and fractionnal parts */
    split(input, first, second);

    /* decode integer part */
    printf("integer part:\n");
    for (int i = strlen(first) - 1, j = 1; i > -1; --i, j <<= 1) {
        if (first[i] == '1') {
            printf("%d ", j);
        }
    }

    /* decode frac part */
    printf("\nfractionnal part:\n");
    for (int i = 0; i < strlen(second); ++i) {
        if (second[i] == '1') {
            printf("1/%d ", 2 << i);
        }
    }

    return 0;
}