给千图网等网站做设计赚钱吗,绿色网站风格,网站访客抓取系统,男女做a视频网站coder的计算器
题目详情: coder现在已经上初中#xff0c;也会用计算器实现 ,-,*,/和幂运算^了#xff0c;但他觉得市场那些计算器太繁琐了#xff0c;有很多他不认识的符号#xff0c;所以他现在很想要能计算带括号的 ,-,*,/和幂运算^的混合表达式就可以了#xff0c;你…coder的计算器
题目详情: coder现在已经上初中也会用计算器实现 ,-,*,/和幂运算^了但他觉得市场那些计算器太繁琐了有很多他不认识的符号所以他现在很想要能计算带括号的 ,-,*,/和幂运算^的混合表达式就可以了你能帮他实现这个愿望吗还有coder希望这台计算器能告诉他每一步的计算结果以便学习和检查。注意 2^2^2表示2^(2^2)。
输入格式有T组数据每一组都是一个表达式表达式每个符号之间都会有一个空格如1 2 / 3
输出格式首先输出按照计算顺序的每一步的计算结果而且要空行最后输出计算结果
第k组前要加上Case k 每个输出都要保留3位小数点。 答题说明: 输入样例 1 1 - 0.5 ^ 2 ^ 0 ( 2 - 1 ) 输出样例 2.000^0.0001.000 0.500^1.0000.500 1.000-0.5000.500 2.000-1.0001.000 0.5001.0001.500 Case 1: 1.500 错误的代码由于对 ‘-’ 的二义性没有考虑到 #include stdio.h
#include string.h
#include math.h
#include ctype.h
#include stack
#define maxn 1000
using namespace std;
char buf[maxn],length;stackchar op;
/*在此犯了一个很严重的错误错误的定义成了 stackint n
这样的错误导致结果总是为 0调试了好久很没有发现错误的根源在哪里*/
stackdouble n;int getPriority(char c)
{switch(c){case (: return 1;case ): return 1; case : return 2;case -: return 2;case *: return 3;case /: return 3;case ^: return 4; default: return 0;}
}double calc(double a,double b,char c)
{double d; switch(c){case : d(ab); break;case -: d(a-b); break;case *: d(a*b); break;case /: d(a/b); break;case ^: dpow(a,b); break;}printf(%.3lf%c%.3lf%.3lf\n,a,c,b,d);return d;
}
void pull()
{double a,b;char cop.top(); op.pop();if(getPriority(c)1 n.size()1){bn.top(); n.pop();an.top(); n.pop();n.push(calc(a,b,c));}
} int main()
{int T,count0;scanf(%d,T);while(T--){char c; double d; int i;length0; count;do{cgetchar();if( !c) buf[length]c;}while(!c);i-1;while(ilength){ if(isalnum(buf[i])){//从左至右扫描表达式数字读入 sscanf(bufi,%lf,d); n.push(d);while(isalnum(buf[i1]) || .buf[i1]) i;}else{//从左至右扫描表达式运算符读入cbuf[i];if(getPriority(c)){ //能被识别的操作符 if((c || ^c || op.empty() || getPriority(c)getPriority(op.top()) ) op.push(c);else if()c){while(!op.empty() (!op.top()) pull();if(!op.empty()) op.pop();}else{while(!op.empty() getPriority(c)getPriority(op.top())) pull(); //操作符计算 op.push(c);}} } }while(!op.empty()) pull();printf(Case %d: %.3lf\n,count,n.top());while(!n.empty()) n.pop(); //清空数据栈 }return 0;
} 修改后AC #include stdio.h
#include string.h
#include math.h
#include ctype.h
#include stack
#define maxn 1000
using namespace std;
char buf[maxn],length;stackchar op;
stackdouble n;int getPriority(char c)
{switch(c){case (: return 1;case ): return 1; case : return 2;case -: return 2;case *: return 3;case /: return 3;case ^: return 4; default: return 0;}
}double calc(double a,double b,char c)
{double d; switch(c){case : d(ab); break;case -: d(a-b); break;case *: d(a*b); break;case /: d(a/b); break;case ^: dpow(a,b); break;}printf(%.3lf%c%.3lf%.3lf\n,a,c,b,d);return d;
}
void pull()
{double a,b;char cop.top(); op.pop();if(getPriority(c)1 n.size()1){bn.top(); n.pop();an.top(); n.pop();n.push(calc(a,b,c));}
} int main()
{int T,count0;scanf(%d,T);while(T--){char c; double d; int i;length0; count;do{cgetchar();if( !c \n!c) buf[length]c;}while(!c);i-1;while(ilength){ if( buf[i]- ){ // - 可能出现二义性符号或减号因此特殊处理 int flag0;if(i0) flag1;else if(i0){int tmpi;flag1;while(tmp--){if(isalnum(buf[tmp])){flag0; break;}else if(getPriority(buf[tmp])1) break;}}if(flag){sscanf(bufi,%lf,d); n.push(d);while(isalnum(buf[i1]) || .buf[i1]) i;continue;} }if(isalnum(buf[i])){//从左至右扫描表达式数字读入 sscanf(bufi,%lf,d); n.push(d);while(isalnum(buf[i1]) || .buf[i1]) i;}else{//从左至右扫描表达式运算符读入cbuf[i];if(getPriority(c)){ //能被识别的操作符 if((c || ^c || op.empty() || getPriority(c)getPriority(op.top()) ) op.push(c);else if()c){//遇到有括号退栈计算直到计算到左括号或栈空为止 while(!op.empty() (!op.top()) pull();if(!op.empty()) op.pop();}else{while(!op.empty() getPriority(c)getPriority(op.top())) pull(); //操作符计算 op.push(c);}} } }while(!op.empty()) pull();printf(Case %d: %.3lf\n,count,n.top());while(!n.empty()) n.pop(); //清空数据栈 }return 0;
} CSDN挑战编程交流群 372863405