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

使用函数的C语言科学计算器,c,logic,calculator,C,Logic,Calculator,我写了一个程序,它使用了大多数math.h库函数来创建一种科学计算器。然而,我没有得到期望的输出。我总是在输出的最后得到0.000000。请帮我找出我的错误,谢谢 #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

我写了一个程序,它使用了大多数math.h库函数来创建一种科学计算器。然而,我没有得到期望的输出。我总是在输出的最后得到0.000000。请帮我找出我的错误,谢谢

#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));
}
#包括
#包括
#定义PI 3.14159265
浮点正弦(浮点x);
浮点余弦(浮点x);
浮动切线(浮动x);
浮子筋(浮子x);
浮动余弦(浮动x);
浮动切线H(浮动x);
浮点数logten(浮点数x);
浮点平方根(浮点x);
浮点指数(浮点x);
浮动功率(浮动x、浮动y);
int main()
{
int x,y,n,答案;
printf(“您想做什么?\n”);
printf(“1.sin 2.cos 3.tan 4.sinh 5.cosh 6.tanh 7.1og10 8.平方根9.指数10.幂”);
scanf(“%d”和“&n”);
如果(n0)
{
printf(“\n x是什么?”);
scanf(“%f”、&x);
开关(n)
{
案例1:答案=正弦(x);中断;
案例2:答案=余弦(x);中断;
案例3:答案=切线(x);中断;
案例4:答案=正弦(x);中断;
案例5:答案=余弦(x);中断;
案例6:答案=切线H(x);断裂;
案例7:答案=logten(x);中断;
案例8:答案=平方根(x);中断;
案例9:答案=指数(x);中断;
}
}
如果(n==10)
{
printf(“x和y是什么?\n”);
scanf(“%f%f”、&x和&y);
答案=功率(x,y);
}

如果(n>0&&n您已将这四个变量声明为整数:

int x,y,n,answer;
但是
n
是唯一一个您实际将其视为整数的变量。将
x
y
answer
声明为
float

int n;
float x, y, answer;

如果启用-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.

您将
x
和其他变量声明为
int
。请尝试声明为
float
#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;
}
#包括 #包括 #定义PI 3.14159265 浮点正弦(浮点x) { 回报率(sin(x*PI/180)); } 浮点余弦(浮点x) { 收益率(cos(x*PI/180)); } 浮动切线(浮动x) { 返回(tan(x)); } 浮动正弦(浮动x) { 返回(新罕布什尔州(x)); } 浮动余弦(浮动x) { 返回(新罕布什尔州(x)); } 浮动切线H(浮动x) { 返回(新罕布什尔州(x)); } 浮动对数十(浮动x) { 返回(log10(x)); } 浮点平方根(浮点x) { 返回(sqrt(x)); } 浮点指数(浮点x) { 返回(exp(x)); } 浮动功率(浮动x、浮动y) { 返回(功率(x,y)); } 内部主(空) { int n; 浮动x,y,回答; printf(“您想做什么?\n”); printf(“1.sin 2.cos 3.tan 4.sinh 5.cosh 6.tanh 7.1og10 8.平方根。 9.10.权力。”); scanf(“%d”和“&n”); 如果(n0) { printf(“\n x是什么?”); scanf(“%f”、&x); 开关(n) { 案例1:答案=正弦(x);中断; 案例2:答案=余弦(x);中断; 案例3:答案=切线(x);中断; 案例4:答案=正弦(x);中断; 案例5:答案=余弦(x);中断; 案例6:答案=切线H(x);断裂; 案例7:答案=logten(x);中断; 案例8:答案=平方根(x);中断; 案例9:答案=指数(x);中断; } } 如果(n==10) { printf(“x和y是什么?\n”); scanf(“%f%f”、&x和&y); 答案=功率(x,y); }
如果(n>0&&n则问题在于格式说明符错误。请在
scanf
语句中将所有
%f
更改为
%d