[問題] C++程式碼的問題
開發平台(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