简体   繁体   English

C语言中的科学计算器使用函数

[英]A Scientific Calculator in C using functions

I wrote a program which used most of the math.h library functions for creating a sort of scientific calculator. 我编写了一个程序,它使用了大多数math.h库函数来创建一种科学计算器。 However, I am not getting the desired output. 但是,我没有得到所需的输出。 I always get 0.000000 in the end of the output. 我总是在输出结束时得到0.000000。 Please help me identify my mistake, Thank you. 请帮我辨别我的错误,谢谢。

#include <stdio.h>
#include <math.h>
#define PI 3.14159265
float sine(float  x);
float cosine(float x);
float tangent(float x);
float sineh(float x);
float cosineh(float x);
float tangenth(float x);
float logten(float x);
float squareroot(float x);
float exponent(float x);
float power(float x,float y);
int main()
{
    int x,y,n,answer;
    printf("What do you want to do?\n");
    printf("1.sin 2.cos 3. tan 4. sinh 5.cosh 6.tanh 7.1og10 8. square root. 9.exponent 10.power.");
    scanf ("%d",&n);
    if (n<9 && n>0)
    {
        printf("\n What is x? ");
        scanf("%f",&x);
        switch (n)
        {
            case 1: answer = sine(x);       break;
            case 2: answer = cosine(x);     break;
            case 3: answer = tangent(x);    break;
            case 4: answer = sineh(x);      break;
            case 5: answer = cosineh(x);    break;
            case 6: answer = tangenth(x);   break;
            case 7: answer = logten(x);     break;
            case 8: answer = squareroot(x); break;
            case 9: answer = exponent(x);   break;
        }
    }
    if (n==10)
    {
        printf("What is x and y?\n");
        scanf("%f%f",&x,&y);
        answer = power(x,y);
    }
    if (n>0 && n<11)
        printf("%f",answer);
    else
        printf("Wrong input.\n");
    return 0;
}
float sine(float x)
{
    return (sin (x*PI/180));
}
float cosine(float x)
{
    return (cos (x*PI/180));
}
float tangent(float x)
{
    return (tan(x*PI/180));
}
float sineh(float x)
{
    return (sinh(x));
}
float cosineh(float x)
{
    return (sinh(x));
}
float tangenth(float x)
{
    return (sinh(x));
}
float logten(float x)
{
    return (log10(x));
}
float squareroot(float x)
{
    return (sqrt(x));
}
float exponent(float x)
{
    return(exp(x));
}
float power(float x, float y)
{
    return (pow(x,y));
}

You have declared these four variables as integers: 您已将这四个变量声明为整数:

int x,y,n,answer;

but n is the only one that you actually treat as an integer. 但是n是你实际上作为整数处理的唯一一个。 Declare x , y and answer as float : 声明xyanswerfloat

int n;
float x, y, answer;
#include <stdio.h>
#include <math.h>
#include<conio.h>
#define PI 3.14159265

float sine(float x)
{
return (sin (x*PI/180));
}
float cosine(float x)
 {
return (cos (x*PI/180));
}
float tangent(float x)
{
return (tan(x));
}
float sineh(float x)
{
return (sinh(x));
}
float cosineh(float x)
{
return (sinh(x));
}
 float tangenth(float x)
 {
return (sinh(x));
}
 float logten(float x)
{
return (log10(x));
}
 float squareroot(float x)
 {
return (sqrt(x));
}
float exponent(float x)
   {
  return(exp(x));
}
float power(float x, float y)
{
return (pow(x,y));
}
 int main(void)
  {
  int n;
    float x,y,answer;
   printf("What do you want to do?\n");
  printf("1.sin 2.cos 3. tan 4. sinh 5.cosh 6.tanh 7.1og10 8. square root. 

  9.exponent   10.power.");
    scanf ("%d",&n);
    if (n<9 && n>0)
    {
    printf("\n What is x? ");
    scanf("%f",&x);
    switch (n)
    {
        case 1: answer = sine(x);       break;
        case 2: answer = cosine(x);     break;
        case 3: answer = tangent(x);    break;
        case 4: answer = sineh(x);      break;
        case 5: answer = cosineh(x);    break;
        case 6: answer = tangenth(x);   break;
        case 7: answer = logten(x);     break;
        case 8: answer = squareroot(x); break;
        case 9: answer = exponent(x);   break;
    }
}
if (n==10)
{
    printf("What is x and y?\n");
    scanf("%f%f",&x,&y);
    answer = power(x,y);
}
if (n>0 && n<11)
    printf("%f",answer);
else
    printf("Wrong input.\n");
    getch();
return 0;
}

If I enable -Wall and -Wconversion, I see the following warnings, which, if you solved, would solve your problem: 如果我启用了-Wall和-Wconversion,我会看到以下警告,如果你解决了,它将解决你的问题:

foo.c:23:14: warning: format specifies type 'float *' but the argument has type 'int *' [-Wformat]
                scanf("%f",&x);
                       ~~  ^~
                       %d
foo.c:26:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 1: answer = sine(x);       break;
                                       ~ ^~~~~~~
foo.c:27:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 2: answer = cosine(x);     break;
                                       ~ ^~~~~~~~~
foo.c:28:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 3: answer = tangent(x);    break;
                                       ~ ^~~~~~~~~~
foo.c:29:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 4: answer = sineh(x);      break;
                                       ~ ^~~~~~~~
foo.c:30:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 5: answer = cosineh(x);    break;
                                       ~ ^~~~~~~~~~
foo.c:31:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 6: answer = tangenth(x);   break;
                                       ~ ^~~~~~~~~~~
foo.c:32:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 7: answer = logten(x);     break;
                                       ~ ^~~~~~~~~
foo.c:33:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 8: answer = squareroot(x); break;
                                       ~ ^~~~~~~~~~~~~
foo.c:34:21: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                        case 9: answer = exponent(x);   break;
                                       ~ ^~~~~~~~~~~
foo.c:40:16: warning: format specifies type 'float *' but the argument has type 'int *' [-Wformat]
                scanf("%f%f",&x,&y);
                       ~~    ^~
                       %d
foo.c:40:19: warning: format specifies type 'float *' but the argument has type 'int *' [-Wformat]
                scanf("%f%f",&x,&y);
                         ~~     ^~
                         %d
foo.c:41:12: warning: implicit conversion turns floating-point number into integer: 'float' to 'int' [-Wconversion]
                answer = power(x,y);
                       ~ ^~~~~~~~~~
foo.c:44:15: warning: format specifies type 'double' but the argument has type 'int' [-Wformat]
                printf("%f",answer);
                        ~~  ^~~~~~
                        %d
foo.c:51:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (sin (x*PI/180));
        ~~~~~~  ^~~~~~~~~~~~~~
foo.c:55:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (cos (x*PI/180));
        ~~~~~~  ^~~~~~~~~~~~~~
foo.c:59:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (tan(x*PI/180));
        ~~~~~~  ^~~~~~~~~~~~~
foo.c:63:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (sinh(x));
        ~~~~~~  ^~~~~~~
foo.c:67:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (sinh(x));
        ~~~~~~  ^~~~~~~
foo.c:71:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (sinh(x));
        ~~~~~~  ^~~~~~~
foo.c:75:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (log10(x));
        ~~~~~~  ^~~~~~~~
foo.c:79:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (sqrt(x));
        ~~~~~~  ^~~~~~~
foo.c:83:9: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return(exp(x));
        ~~~~~~ ^~~~~~
foo.c:87:10: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
        return (pow(x,y));
        ~~~~~~  ^~~~~~~~
24 warnings generated.

You declared x and other variables as int . 您将x和其他变量声明为int Try declaring as float instead 尝试声明为float而不是

The problem is that the format specifier is wrong. 问题是格式说明符是错误的。 Change all %f to %d in the scanf statement. scanf语句中将所有%f更改为%d

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

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