[問題] C++程式碼的問題

看板C_and_CPP作者 (阿翔)時間14年前 (2011/03/03 09:49), 編輯推噓3(304)
留言7則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) C++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) #include<iostream> #include<sstream> #include<string> #include<stack> 問題(Question): 請問這程式碼是如何運作的 程式碼(Code):(請善用置底文網頁, 記得排版) #include<iostream> #include<sstream> #include<string> #include<stack> using namespace std; int main() { //字串""字元'' string str,sub; int cc,result; stack<int> nstack; while(getline(cin,str) ){ istringstream iss(str); while( iss>>sub){ switch(sub[0]){ case '+': result=nstack.top(); nstack.pop(); result+=nstack.top(); nstack.pop(); nstack.push(result); break; case '-': result=nstack.top(); nstack.pop(); result=nstack.top()-result; nstack.pop(); nstack.push(result); break; case '*': result=nstack.top(); nstack.pop(); result=nstack.top()*result; nstack.pop(); nstack.push(result); break; case '/': result=nstack.top(); nstack.pop(); result=nstack.top()/result; nstack.pop(); nstack.push(result); break; case '%': result=nstack.top(); nstack.pop(); result=nstack.top()%result; nstack.pop(); nstack.push(result); break; default: istringstream in(sub); in>>cc; nstack.push(cc); } } cout<<nstack.top()<<endl; } return 0; } 補充說明(Supplement): 這是http://zerojudge.tw/ShowProblem?problemid=d016的題目 老師今天才教完,但我聽得不是很懂,我現在高一,所以請不要用太深奧的語言解釋 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.82.202.131

03/03 18:16, , 1F
演算法本身,你去找一本「資料結構」或「演算法」的書,看
03/03 18:16, 1F

03/03 18:16, , 2F
堆疊那章,就會解釋清楚。如果是語法不懂,也要講清楚是
03/03 18:16, 2F

03/03 18:16, , 3F
哪幾行有問題呀
03/03 18:16, 3F

03/03 18:54, , 4F
就是物件部分 謝謝
03/03 18:54, 4F

03/03 18:54, , 5F
直接問老師或請家教.
03/03 18:54, 5F

03/03 19:35, , 6F
高一應該不至於要你寫程式碼這麼深奧。但你該弄懂的是
03/03 19:35, 6F

03/03 19:35, , 7F
你所附網頁上的說明
03/03 19:35, 7F
文章代碼(AID): #1DRsG-Nx (C_and_CPP)