簡體   English   中英

計算器和C語言中的浮點模數函數

[英]Calculator and a floating point modulus function in C

我必須為計算器的'%'(模數)函數編寫一個案例。 編譯時,它告訴我我沒有正確使用fmod函數。 沒有大小寫'%',其余代碼將按預期工作。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>    // Originally missing

#define MAXOP 100
#define NUMBER '0'

int getop(char []);
void push(double);
double pop(void);

main()
{
    int type;
    double op2;
    char s[MAXOP];

    printf("\nEnter numbers below in the following format\nwith a space between each:\n");
    printf("\tnum1 num2 operator\n");
    printf("\tyou may use (+, -, /, *) for the operators\n");

    while((type = getop(s)) != EOF)
    {
        switch(type)
        {
            case NUMBER:
                push(atof(s));
                break;

            case '+':
                push(pop() + pop());
                break;

            case '*':
                push(pop() * pop());
                break;

            case '-':
                op2 = pop();
                push(pop() - op2);
                break;

            case '/':
                op2 = pop();
                if(op2 != 0.0)
                    push(pop() / op2);
                else
                    printf("error: zero divisor\n");
                break;

              case '%':
                op2=pop();
                if(op2 != 0.0)
                    push(fmod(pop(),op2));
                else
                    printf("error: zero divisor");
                break;

            case '\n':
                printf("\t%.8g\n", pop());
                break;
            default:
                printf("error: unknown command %s\n", s);
                break;
        }
    }
    return 0;
}

我查找了fmod函數,但仍然不知道為什么它不起作用。 有什么想法嗎?

我添加了math.h標頭,現在收到的錯誤消息是:

/tmp/ccgiVhWI.o: In function `main':
calc.c:(.text+0x14f): undefined reference to `fmod'
collect2: ld returned 1 exit status

您缺少#include <math.h>因此編譯器認為fmod()是返回int的函數,或者抱怨它知道fmod()但是您尚未正確聲明或聲明它。

您最好包含確切的編譯器錯誤消息(除非文件名的路徑很長,在這種情況下,截斷文件名是一種禮貌)。

您缺少了math.h標頭來提供fmod的原型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM