日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CSDN 编程挑战——《coder的计算器》

發(fā)布時間:2023/12/1 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CSDN 编程挑战——《coder的计算器》 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

coder的計算器

題目詳情:

coder現(xiàn)在已經(jīng)上初中,也會用計算器實現(xiàn)+ ,-,*,/和冪運算^了,但他覺得市場那些計算器太繁瑣了,有很多他不認識的符號,所以他現(xiàn)在很想要能計算帶括號的+ ,-,*,/和冪運算^的混合表達式就可以了,你能幫他實現(xiàn)這個愿望嗎?還有coder希望這臺計算器能告訴他每一步的計算結(jié)果,以便學習和檢查。注意 2^2^2表示2^(2^2)。

輸入格式:有T組數(shù)據(jù),每一組都是一個表達式,表達式每個符號之間都會有一個空格,如1 + 2 ?/ ?3 =

輸出格式:首先輸出按照計算順序的每一步的計算結(jié)果,而且要空行,最后輸出計算結(jié)果,

第k組前要加上Case k : ?,每個輸出都要保留3位小數(shù)點。



答題說明:

輸入樣例:

1

1 - 0.5 ^ 2 ^ 0 + ( 2 - 1 ) =

輸出樣例:

2.000^0.000=1.000

0.500^1.000=0.500

1.000-0.500=0.500

2.000-1.000=1.000

0.500+1.000=1.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;stack<char> op; /*在此犯了一個很嚴重的錯誤,錯誤的定義成了 stack<int> n, 這樣的錯誤導致結(jié)果總是為 0,調(diào)試了好久很沒有發(fā)現(xiàn)錯誤的根源在哪里*/ stack<double> 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=(a+b); break;case '-': d=(a-b); break;case '*': d=(a*b); break;case '/': d=(a/b); break;case '^': d=pow(a,b); break;}printf("%.3lf%c%.3lf=%.3lf\n",a,c,b,d);return d; } void pull() {double a,b;char c=op.top(); op.pop();if(getPriority(c)>1 && n.size()>1){b=n.top(); n.pop();a=n.top(); n.pop();n.push(calc(a,b,c));} } int main() {int T,count=0;scanf("%d",&T);while(T--){char c; double d; int i;length=0; count++;do{c=getchar();if(' '!=c) buf[length++]=c;}while('='!=c);i=-1;while(++i<length){ if(isalnum(buf[i])){//從左至右掃描表達式,數(shù)字讀入 sscanf(buf+i,"%lf",&d); n.push(d);while(isalnum(buf[i+1]) || '.'==buf[i+1]) i++;}else{//從左至右掃描表達式,運算符讀入c=buf[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(); //清空數(shù)據(jù)棧 }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;stack<char> op; stack<double> 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=(a+b); break;case '-': d=(a-b); break;case '*': d=(a*b); break;case '/': d=(a/b); break;case '^': d=pow(a,b); break;}printf("%.3lf%c%.3lf=%.3lf\n",a,c,b,d);return d; } void pull() {double a,b;char c=op.top(); op.pop();if(getPriority(c)>1 && n.size()>1){b=n.top(); n.pop();a=n.top(); n.pop();n.push(calc(a,b,c));} } int main() {int T,count=0;scanf("%d",&T);while(T--){char c; double d; int i;length=0; count++;do{c=getchar();if(' '!=c && '\n'!=c) buf[length++]=c;}while('='!=c);i=-1;while(++i<length){ if( buf[i]=='-' ){ // '-' 可能出現(xiàn)二義性(符號或減號)因此特殊處理 int flag=0;if(i==0) flag=1;else if(i>0){int tmp=i;flag=1;while(tmp--){if(isalnum(buf[tmp])){flag=0; break;}else if(getPriority(buf[tmp])>1) break;}}if(flag){sscanf(buf+i,"%lf",&d); n.push(d);while(isalnum(buf[i+1]) || '.'==buf[i+1]) i++;continue;} }if(isalnum(buf[i])){//從左至右掃描表達式,數(shù)字讀入 sscanf(buf+i,"%lf",&d); n.push(d);while(isalnum(buf[i+1]) || '.'==buf[i+1]) i++;}else{//從左至右掃描表達式,運算符讀入c=buf[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(); //清空數(shù)據(jù)棧 }return 0; }


? ? ? ? ??

CSDN挑戰(zhàn)編程交流群:?372863405 ? ? ? ??

?


總結(jié)

以上是生活随笔為你收集整理的CSDN 编程挑战——《coder的计算器》的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。