[英]Simple calculator using switch with if else in C
This is my code so far for my simple calculator. 到目前为止,这是我的简单计算器代码。 Im working on sine
right now(case 6) with a degree range of 0-360.Here is the output. 我正在sine
工作(情况6),度数范围为0-360。这是输出。
$ ./a.exe ProblemSolving.c
Arithmetic : Add(0) Sub(1) Mult(2) Div(4) Mod(5)
Trigonometry : sine(6) cosine(7) tan(8) arc_sin(9) arc_cos(10)
Exponent : x^y(11) 2^x(12) 10^x(13)
Enter the choice of operation:6
The choice of operation is:6
Enter degree range from 0 to 360
Enter degrees:400
After I enter the desired degrees nothing else happens and the program ends. 输入所需的度数后,没有其他反应,程序结束。 I believe there is something wrong with my if
statement or the sine
function. 我相信我的if
语句或sine
函数有问题。
#include <stdio.h>
#include <math.h>
int main() {
double Add1, Add2, Sub1, Sub2, Mult1, Mult2;
int Choice, Div1, Div2, Mod1, Mod2, Base1, Power1, Deg1;
printf("Arithmetic : Add(0) Sub(1) Mult(2) Div(4) Mod(5)\n");
printf("Trigonometry : sine(6) cosine(7) tan(8) arc_sin(9) arc_cos(10)\n");
printf("Exponent : x^y(11) 2^x(12) 10^x(13)\n");
printf("Enter the choice of operation:");
scanf("%d", &Choice);
printf("The choice of operation is:%d\n", Choice);
switch(Choice) {
case 0:
printf("Enter number one:");
scanf("%lf", &Add1);
printf("Enter number two:");
scanf("%lf", &Add2);
printf("%2.2lf + %2.2lf = %2.2lf", Add1, Add2, Add1+Add2);
break;
case 1:
printf("Enter number one:");
scanf("%lf", &Sub1);
printf("Enter number two:");
scanf("%lf", &Sub2);
printf("%2.2lf - %2.2lf = %2.2lf", Sub1, Sub2, Sub1-Sub2);
break;
case 2:
printf("Enter number one:");
scanf("%lf", &Mult1);
printf("Enter number two:");
scanf("%lf", &Mult2);
printf("%2.2lf * %2.2lf = %2.2lf", Mult1, Mult2, Mult1*Mult2);
break;
case 4:
printf("Enter number one:");
scanf("%d", &Div1);
printf("Enter number two:");
scanf("%d", &Div2);
if (Div2 == 0)
printf("Error! Denominator cannot equal 0");
else
printf("%d / %d = %d", Div1, Div2, Div1/Div2);
break;
case 5:
printf("Enter number one:");
scanf("%d", Mod1);
printf("Enter number two:");
scanf("%d", Mod2);
if (Mod2 == 0)
printf("Error! Denominator cannot equal 0");
else
printf("%d % %d = %d", Mod1, Mod2, Mod1%Mod2);
break;
case 6:
printf("Enter degree range from 0 to 360\n");
printf("Enter degrees:");
scanf("%d", Deg1);
if (0 > Deg1 > 360)
printf("Error! Value Entered is not within valid range");
else
printf("sin(%d) = %d", Deg1, sin(Deg1));
break;
default:
printf("Error! operator is not correct");
break;
}
return 0;
}
The sine
function (and the rest of the trig functions) in C works in radians, not degrees. C语言中的sine
函数(以及其余的trig函数)以弧度而不是度为单位。 You'll have to convert from degrees from radians before passing the value to sine
. 在将值传递给sine
之前,您必须从弧度转换为度。
Right now you also have a problem with the format in your printf
, since you're passing a double
, but telling printf
to expect an int
. 现在,您的printf
的格式也存在问题,因为您要传递一个double
,但告诉printf
期望一个int
。 You need to use %f
instead of %d
. 您需要使用%f
而不是%d
。
In addition, your if
statement doesn't currently make much sense, and almost certainly doesn't mean what you think. 另外,您的if
语句当前意义不大,几乎可以肯定并不意味着您的想法。 What you apparently want is if (Deg1 < 0.0 || Deg1 > 360.0)
您显然想要的是if (Deg1 < 0.0 || Deg1 > 360.0)
There are several problems in this code: 这段代码有几个问题:
scanf("%d", Deg1);
更改scanf("%d", Deg1);
to scanf("%d", &Deg1);
到scanf("%d", &Deg1);
, because scanf()
call for addresses. ,因为scanf()
需要地址。 Also, I think it might be better to declare Deg1
as a double
. 另外,我认为将Deg1
声明为double
可能更好。 0 > Deg1 > 360
is wrong in C. You have to write Deg1 < 0 || Deg1 > 360
0 > Deg1 > 360
在C中是错误的。您必须编写Deg1 < 0 || Deg1 > 360
Deg1 < 0 || Deg1 > 360
. Deg1 < 0 || Deg1 > 360
。 Operator ||
运算符||
stands for "logical Or". 代表“逻辑或”。 math.h
, sin()
works in radians. 在math.h
, sin()
以弧度工作。 So use sin(Deg1 * 3.14159265 / 180)
. 因此使用sin(Deg1 * 3.14159265 / 180)
。 Or, to improve readability and maintenance, #define PI 3.14159265
and sin(Deg1 * PI / 180)
. 或者,为了提高可读性和维护性,请#define PI 3.14159265
和sin(Deg1 * PI / 180)
。 Note that you cannot write Deg1 / 180 * 3.14159265
, because integer literals in C is int
s, and int
/ int
= int
. 请注意,您不能编写Deg1 / 180 * 3.14159265
,因为C中的整数文字是int
s,并且int
/ int
= int
。 For example, 3 / 2 == 1, rather than 1.5. 例如,3/2 == 1,而不是1.5。 To get the exact value, write 3.0 / 2.0. 要获得确切的值,请写3.0 / 2.0。 math.h
, sin()
returns double
, so write printf("sin(%d) = %g", Deg1, sin(...));
在math.h
, sin()
返回double
,因此编写printf("sin(%d) = %g", Deg1, sin(...));
. 。 Fixed code here: 固定代码在这里:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
// ...many lines of code...
case 6:
printf("Enter degree range from 0 to 360\n");
printf("Enter degrees:");
scanf("%d", &Deg1);
if (0 > Deg1 || Deg1 > 360)
printf("Error! Value Entered is not within valid range");
else
printf("sin(%d) = %g", Deg1, sin(Deg1 * PI / 180));
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.