为什么我的+-/x函数不起作用,而我的正余弦和正切起作用


#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <stdlib.h>
#define PI 3.14159265
float sine(float  x);
float cosine(float x);
float tangent(float x);
int main()
{
int n;
float x, y, answer;
printf("Welcome to Guru's Scientific Calculator.");
Sleep(3000);
system("cls");
Sleep(500);
printf("Wait, while we gather requiredndatabases for the application tonfunction properly!nn");
Sleep(7500);
system("pause");
system("cls");
printf("What do you want to do?n");
printf("1.sinn2.cosn3.tann4.Additionn5.Subtractionn6.Divisionn7.MultipicationnnYour input: ");
scanf ("%d",&n);
if (n<3 && n>0)
{
printf("n What is x?nYour input: ");
scanf("%f",&x);
switch (n)
{
case 1 : answer = sine(x);       break;
case 2 : answer = cosine(x);     break;
case 3 : answer = tangent(x);    break;
}
}
if (n<7 && n>3){
printf("What is x and y?nYour inputs: ");
scanf("%f , %f", x, y);
switch (n)
{
case 1 : answer = (x + y);       break;
case 2 : answer = (x - y);       break;
case 3 : answer = (x / y);       break;
case 4 : answer = (x * y);       break;
}
}
if (n>0 && n<8)
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));
}

sin,cos,tan函数仍然可以工作。不是我的加法、减法、乘法和除法。为什么?代码出了什么问题?

以下建议的代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 包含对问题的评论
  4. 不执行函数CCD_ 1和CCD_。您应该为健壮代码添加错误检查
  5. 请注意,在对printf()的调用中使用了字符串的"串联",以实现对右边距的尊重(用于打印等(
  6. 注意使用double来匹配math.h中定义的函数的语法
  7. 更正了multiplication的拼写
  8. 使用do...while循环输入所需的选择值并验证其有效性
  9. 使用对getchar()的调用来输入所需的选择值
  10. 检查并避免除以0.0

注意:头文件math.h中定义了一个"PI"值:scanf()0

现在,提出的代码:

#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <stdlib.h>
#define PI 3.14159265
double sine   ( double x );
double cosine ( double x );
double tangent( double x );
int main( void )
{
int n;
double x;
double y;
double answer;
printf("Welcome to Guru's Scientific Calculator.");
Sleep(3000);
system("cls");
Sleep(500);
printf("Wait, while we gather requiredn"
"databases for the application ton"
"function properly!nn");
Sleep(7500);
system("pause");
system("cls");
do
{
printf("What do you want to do?n"
"1.sinn"
"2.cosn"
"3.tann"
"4.Additionn"
"5.Subtractionn"
"6.Divisionn"
"7.Multiplicationn"
"nYour input: ");
n = getchar();
} while( 1 > n || n > 8 );
if ( n < 4 )
{
printf("n What is x?n"
"Your input: ");
scanf("%lf",&x);
switch (n)
{
case 1 : 
answer = sine(x);       
break;
case 2 : 
answer = cosine(x);     
break;
case 3 : 
answer = tangent(x);    
break;
}
}
else
{
printf("What is x and y?n"
"Your inputs: ");
scanf("%lf , %lf", &x, &y);
switch (n)
{
case 4 : 
answer = (x + y);       
break;
case 5 : 
answer = (x - y);       
break;
case 6 : 
if( y == 0.0 )
{
printf( "%sn", 
"cannot perform division by 0" ); 
exit( EXIT_FAILURE );
}
answer = (x / y);       
break;
case 7 : 
answer = (x * y);       
break;
}
}
printf("%lf",answer);
return 0;
}
double sine( double x )
{
return (sin (x*PI/180.0));
}
double cosine( double x )
{
return (cos (x*PI/180.0));
}
double tangent( double x )
{
return (tan(x*PI/180.0));
}

在第二个case块中,case语句查找1到4:

if (n<7 && n>3){
printf("What is x and y?nYour inputs: ");
scanf("%f , %f", x, y);
switch (n)
{
case 1 : answer = (x + y);       break;
case 2 : answer = (x - y);       break;
case 3 : answer = (x / y);       break;
case 4 : answer = (x * y);       break;
}
}

但是你正在读的代码是从4到7。你需要检查一下合适的箱子。另外,检查n<8,因为7不小于7;并校正scanf:

if (n<8 && n>3){
printf("What is x and y?nYour inputs: ");
scanf("%f %f", &x, &y);
switch (n)
{
case 4 : answer = (x + y);       break;
case 5 : answer = (x - y);       break;
case 6 : answer = (x / y);       break;
case 7 : answer = (x * y);       break;
}
}

最新更新