这是一个简易计算器代码,但是在算三角函数的时候需要选择运算符号,选择运算内容和输入计算的数值,应该怎么实现直接输入算式呢,如:sin(5)或在continue 后输入 +sin(5)
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
define pi 3.1415926
int fuhao(char c);
double jisuan(double a, double b, int p);
double again1(double digit, double n, int p);
double again2(double digit, int c, double result);
int panduan(int d);
int radian_angle(int e);
double sin_cos_tan(int f);
int main()
{
double a, b, digit = 0.0, n;
char c, tmp;
int d, e, h, c1;
printf("若普通四则运算,输入1;若三角函数计算输入2\n");
scanf("%d", &d);
if (panduan(d) == 1)
{
scanf("%lf%c%lf", &a, &c, &b);
digit = jisuan(a, b, fuhao(c));
}
else if (panduan(d) == 2)
{
printf("radian:1 angle:2\n");
scanf("%d", &e);
radian_angle(e);
digit = sin_cos_tan(e);
printf("%lf\n", digit);
}
printf("continue?");
scanf(" %c", &tmp);
for (; tmp == 'Y';)
{
printf("继续四则运算:1 继续三角函数:2\n");
scanf("%d", &h);
if (h == 1)
{
scanf(" %c%lf", &c, &n);
digit = again1(digit, n, fuhao(c));
printf("continue?");
scanf(" %c", &tmp);
}
else if (h == 2)
{
printf("+:1 -:2 :3 /:4 %%:5\n");
scanf(" %d", &c1);
printf("弧度:1 角度:2\n");
scanf("%d", &e);
radian_angle(e);
digit = again2(digit, c1, sin_cos_tan(e));
printf("%lf\n", digit);
printf("continue?");
scanf(" %c", &tmp);
}
else
{
printf("在继续中输入四则运算还是三角错误");
return 0;
}
}
return 0;
}
int panduan(int d)
{
if (d == 1)
return 1;
else if (d == 2)
return 2;
else
{
printf("第一次输入判断错误");
exit(0);
}
}
int fuhao(char c)
{
switch (c)
{
case '+':
return 1;
case'-':
return 2;
case'':
return 3;
case '/':
return 4;
case'%':
return 5;
default:
printf("输入符号错误\n");
exit(0);
}
}
double jisuan(double a, double b, int p)
{
double d;
int d1;
switch (p)
{
case 1:
d = a + b;
printf("%lf\n", d);
return d;
case 2:
d = a - b;
printf("%lf\n", d);
return d;
case 3:
d = a * b;
printf("%lf\n", d);
return d;
case 4:
d = a / b;
printf("%lf\n", d);
return d;
case 5:
d1 = (int)a % (int)b;
d = (double)d1;
printf("%lf\n", d);
return d;
default:
printf("第一次四则运算错误\n");
exit(0);
}
}
int radian_angle(int e)
{
if (e == 1)
return 1;
else if (e == 2)
return 2;
else
{
printf("输入角度还是弧度错误");
exit(0);
}
}
double sin_cos_tan(int f)
{
int g;
double q;
double radian, result, angle;
if (f == 1)
{
printf("选择计算的内容:sin:1,cos:2,tan:3\n");
scanf("%d", &g);
printf("输入弧度:");
scanf("%lf", &radian);
switch (g)
{
case 1:
result = sin(radian);
return result;
case 2:
result = cos(radian);
return result;
case 3:
result = tan(radian);
return result;
default:
printf("选择sin cos tan弧度制错误");
exit(0);
}
}
else if (f == 2)
{
printf("选择计算的内容:sin:1,cos:2,tan:3\n");
scanf("%d", &g);
printf("输入角度:");
scanf("%lf", &angle);
q = (pi / 180) * angle;
switch (g)
{
case 1:
result = sin(q);
return result;
case 2:
result = cos(q);
return result;
case 3:
if (q == (pi / 2))
{
printf("tan不能计算90°\n");
exit(0);
}
else
{
result = tan(q);
return result;
}
default:
printf("选择sin cos tan角度制错误\n");
exit(0);
}
}
else
{
printf("错误\n");
exit(0);
}
}
double again1(double digit, double n, int p)
{
double d;
int d1;
switch (p)
{
case 1:
d = digit + n;
printf("%lf", d);
return d;
case 2:
d = digit - n;
printf("%lf", d);
return d;
case 3:
d = digit * n;
printf("%lf", d);
return d;
case 4:
d = digit / n;
printf("%lf", d);
return d;
case 5:
d1 = (int)digit % (int)n;
d = (double)d1;
printf("%lf", d);
return d;
default:
printf("输入再一次四则运算错误");
exit(0);
}
}
double again2(double digit, int c, double result)
{
double again_result;
int temp;
switch (c)
{
case 1:
again_result = digit + result;
return again_result;
case 2:
again_result = digit - result;
return again_result;
case 3:
again_result = digit * result;
return again_result;
case 4:
again_result = digit / result;
return again_result;
case 5:
temp = (int)digit % (int)result;
again_result = (double)temp;
return again_result;
default:
printf("输入再一次三角错误");
exit(0);
}
}