题目
请你编写一个科学计算器,支持多括号嵌套的四则运算,三角函数及指数对数运算功能可选(功能越多越好,指数的输入格式为 a^b,对数的输入格式为 loga b,(其中a为底数))
代码
#include<stdio.h>
#include<math.h>
typedef struct {
int flg[100];
double num[100];
int topNum;
int topFlg;
int sumFlg;
}stack;
int isNumFull(stack *s){
if(s->topNum == 100)
return 1;
return 0;
}
int isFlgFull(stack *s){
if(s->topFlg == 100)
return 1;
return 0;
}
int isNumEmpty(stack *s){
if(s->topNum == 0)
return 1;
return 0;
}
int isFlgEmpty(stack *s){
if(s->topFlg == 0)
return 1;
return 0;
}
double popNum(stack *s){
if(isNumEmpty(s))
return -114514;
s->topNum = (s->topNum) - 1;
return s->num[(s->topNum)];
}
int popFlg(stack *s){
if(isFlgEmpty(s))
return '0';
s->topFlg = (s->topFlg) - 1;
if(s->flg[(s->topFlg)] != '('){
s->sumFlg--;
}
return s->flg[(s->topFlg)];
}
int pushNum(stack *s,double *a){
if(isNumFull(s))
return 0;
s->num[(s->topNum)] = *a;
s->topNum = (s->topNum) + 1;
*a = 0;
return 1;
}
int pushFlg(stack *s,int a){
if(isFlgFull(s))
return 0;
s->flg[(s->topFlg)] = a;
s->topFlg = (s->topFlg) + 1;
if(a != '('){
s->sumFlg++;
}
return 1;
}
double function1(double a1,double a2,int flg){
switch(flg){
case '+':
return a1 + a2;
case '-':
return a1 - a2;
case '*':
return a1 * a2;
case '/':
return a1 / a2;
case '^':
return pow(a1,a2);
}
return 0;
}
double function2(double a, int fuc){
a = a / 180 * 3.1415926535;
switch(fuc){
case 330: //sin
return sin(a);
case 325: //cos
return cos(a);
case 323: //tan
return sin(a) / cos(a);
case 315: //sec
return 1 / cos(a);
case 313: //csc
return 1 / sin(a);
case 326: //cot
return cos(a) / sin(a);
}
return 0;
}
double function3(double a1,double a2){
return log(a2) / log(a1);
}
double getNum(double *lf){
return scanf("%lf",lf);
}
int main() {
char inp[1],tmp[1];
double t1 = 0;
double t2 = 0;
int fuc,i;
stack s, *p;
s.topFlg = 0;
s.topNum = 0;
s.sumFlg = 0;
p = &s;
if ((inp[0] = getchar()) == '-') {
getNum(&t1);
t1 = 0 - t1;
pushNum(p, &t1);
}
else{
goto label1;
}
while (1) {
inp[0] = getchar();
label1:
if (inp[0] >= '0' && inp[0] <= '9') {
t1 = t1 * 10 + inp[0] - '0';
continue;
}
if(inp[0] == '.'){
getNum(&t2);
for(;t2>1;t2 /= 10);
t1 = t1 + t2;
t2 = 0;
}
if (t1 != 0)
pushNum(p, &t1);
if(inp[0] == '\n' || inp[0] == EOF ){
break;
}
if (inp[0] >= 'a' && inp[0] <= 'z') {
fuc = inp[0];
fuc += getchar();
fuc += getchar();
getNum(&t1);
if(fuc == 322){
getchar();
getNum(&t2);
t1 = function3(t1,t2);
}
else {
t1 = function2(t1, fuc);
}
pushNum(p, &t1);
continue;
}
if (inp[0] == '+' || inp[0] == '(') {
pushFlg(p, inp[0]);
continue;
}
if (inp[0] == '-'){
if (getNum(&t1) == 0){
pushFlg(p, inp[0]);
} else {
t1 = -t1;
pushNum(p, &t1);
if(p->sumFlg == p->topNum -2){
pushFlg(p, '+');
}
}
}
if (inp[0] == '*' || inp[0] == '/' || inp[0] == '^') {
tmp[0] = inp[0];
if(0 == getNum(&t2)){ //为括号或者是负号
fuc = getchar();
if(fuc == '('){
pushFlg(p, inp[0]);
pushFlg(p, '(');
continue;
} else if (fuc == '+' || fuc == '-' || fuc == '*' || fuc == '/' || fuc == '^'){
getNum(&t2);
t2 = -t2;
} else {
fuc += getchar();
fuc += getchar();
getNum(&t1);
if(fuc == 322){
getchar();
getNum(&t2);
t1 = function3(t1,t2);
}
else {
t1 = function2(t1, fuc);
}
t2 = t1;
}
}
t1 = popNum(p);
t1 = function1(t1, t2, tmp[0]);
pushNum(p, &t1);
continue;
}
if (inp[0] == ')') {
while ((fuc = popFlg(p)) != '(') {
t2 = popNum(p);
t1 = popNum(p);
t1 = function1(t1, t2, fuc);
pushNum(p, &t1);
}
continue;
}
}
while (p->topNum != 1) {
fuc = popFlg(p);
t2 = popNum(p);
t1 = popNum(p);
i = fuc;
fuc = popFlg(p);
if(fuc == '-') {
t1 = 0 - t1;
fuc = '+';
}
pushFlg(p,fuc);
t1 = function1(t1, t2, i);
pushNum(p, &t1);
}
printf("%lf",p->num[0]);
return 0;
}
标签:return,int,double,t2,t1,fuc,计算器,C语言
From: https://www.cnblogs.com/Shin404/p/16902806.html