《数据结构经典背包问题》由会员分享,可在线阅读,更多相关《数据结构经典背包问题(6页珍藏版)》请在金锄头文库上搜索。
1、#include #include #define size 20struct stacksint datasize;int top;stack;void main()int wsize;int V;int k=0;int i=0;int j=1;int number;int s=0;printf(n 请输入可供选择装入物品的个数:);scanf(%d,&number);printf(n 请输入各件物品的体积:);for(i=0;is)printf(n 输入背包体积错误);printf(n);for(i=0;i0&k=wk)stack.datastack.top=k;stack.top+;V-
2、=wk;k+;if(V=0)printf(第%d 个符合条件的解:,j);for(i=0;iusing namespace std;bool IsOperator(char ch)char ops = +-*/;for (int i = 0; i st;len = strlen(inFix);for (int i = 0; i = 0)postFixj+ = st.top(); st.pop();st.push(c);while (st.empty() = false)postFixj+ = st.top();st.pop();postFixj = 0;/ 后缀表达式求值程序double po
3、stFixEval(char* postFix)stack st;int len = strlen(postFix);char c;for (int i = 0; i len; i+)c = postFixi;if (IsOperator(c) = false)st.push(c - 0);elsechar op1, op2;int val;op1 = st.top();st.pop();op2 = st.top();st.pop();switch (c)case +:val = op1 + op2; break;case -:val = op2 - op1;break;case *:val
4、= op1 * op2;break;case /:val = op2 / op1;break;st.push(val);return st.top();int _tmain(int argc, _TCHAR* argv)char inFix100;char postFix100;double val;while (1)printf(enter an expression:);gets_s(inFix);if (strlen(inFix) = 0)continue;printf(n%s = , inFix);inFix2PostFix(inFix, postFix);printf(%s = , postFix);val = postFixEval(postFix);printf(%.3fn, val);return 0;