嗨,我是编程新手,并且已经在计算器上工作了一段时间。我正在尝试添加一些三角函数,但我遇到了正弦问题。其他函数可以工作(+、-、*、/),但是当我输入“正弦”时,它会跳到代码中它说它是一个不正确的函数的部分。请帮助我的代码。谢谢!
#include <stdio.h>
#include <math.h>
int main()
{
float firstnum, secondnum, angle, answer, pi;
char function, sine;
pi = atan(1.0)*4;
printf("\nHello and welcome to my calculator!\n");
while(1)
{
printf("\nPlease input the function you would like to use. These include +, -, *, /, sine.\n");
scanf("%s", &function);
switch(function)
{
case '+':
printf("\nNow please input the two variables.\n");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
answer = firstnum+secondnum;
break;
case '-':
printf("\nNow please input the two variables.\n");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
answer = firstnum-secondnum;
break;
case '*':
printf("\nNow please input the two variables.\n");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
answer = firstnum*secondnum;
break;
case '/':
printf("\nNow please input the two variables.\n");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
answer = firstnum/secondnum;
break;
case 'sine':
printf("\nPlease enter the angle.\n");
scanf("%f", &angle);
answer = sin(angle);
break;
default: printf("Sorry, that is an incorrect function. The only available choices are +, -, *, /, sine.");
break;
}
printf("Your answer is %f \n", answer);
printf("\nWhen you are ready to quit, simply press Ctrl + C or just hit the X button in the top right.\n");
}
return 0;
}