栈
魔法机
void mofaji()
{
int i,n;
int num[100];
SqStack s;
initStack(s);
cout<<"请输入入栈元素个数(0退出):"<<endl;
cin>>n;
while(n)
{
cout<<"请输入出栈个数"<<endl;
for(i=1; i<=n; i++)
cin>>num[i];
int k=1;
for(i=1; i<=n; i++)
{
push(s,i);
while(s.top!=-1 && num[k]==s.data[s.top])
{
int e;
pop(s,e);
k++;
}
}
if(s.top==-1)
{
cout<<"该出栈序列合法!"<<endl;
}
else
cout<<"该出栈序列不合法!"<<endl;
cout<<'\n';
cout<<"请输入入栈元素个数(0退出):"<<endl;
cin>>n;
}
}
背包问题
void knapsack1(int w[],int T,int n)
{
SqStack S;
initStack(S);
int k=0,m=0;
do
{
while(T>0&&k<n)
{
if(T>=w[k])
{
push(S,k);
T=T-w[k];
}
k++;
}
if(T==0)
{
m++;
StackTraverse(S,w);
}
pop(S,k);
T=T+w[k];
k++;
}
while(S.top!=-1||k!=n);
cout<<"总共有"<<m<<"组装法"<<endl;
}
void test_knapsack1()
{
int n,t;
cout<<"请输入物品个数:";cin>>n;
cout<<"请输入各个物品重量: ";
int *w=new int[n];
for(int i=0;i<n;i++)
{
cin>>w[i];
}
cout<<"请输入书包可以承受的重量:"<<endl;
cin>>t;
knapsack1(w,t,n);
}
表达式求值
#include <iostream>
#include <stack>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
void pop_c(stack <char> &S,char &e)
{
e=S.top();
S.pop();
}
void pop_c(stack <double> &S,double &e)
{
e=S.top();
S.pop();
}
void trans(char *exp,char postexp[])
{
char e;
stack <char> S;
int i=0;
while(*exp!='\0')
{
switch(*exp)
{
case '(':
S.push('(');
exp++;
break;
case ')':
pop_c(S,e);
while(e!='(')
{
postexp[i++]=e;
pop_c(S,e);
}
exp++;
break;
case '+':
case '-':
while(!S.empty())
{
e=S.top();
if(e!='(')
{
postexp[i++]=e;
pop_c(S,e);
}
else break;
}
S.push(*exp);
exp++;
break;
case '*':
case '/':
while(!S.empty())
{
e=S.top();
if(e=='*'||e=='/')
{
postexp[i++]=e;
pop_c(S,e);
}
else break;
}
S.push(*exp);
exp++;
break;
default:
while(*exp>='0' && *exp<='9')
{
postexp[i++]=*exp;
exp++;
}
postexp[i++]='#' ;
}
}
while(!S.empty())
{
pop_c(S,e);
postexp[i++]=e;
}
postexp[i]='\0';
}
double compvalue(char *postexp)
{
double d,a,b,c,e;
stack <double> S;
while(*postexp!='\0')
{
switch(*postexp)
{
case '+':
pop_c(S,a);
pop_c(S,b);
c=a+b;
S.push(c);
break;
case '-':
pop_c(S,a);
pop_c(S,b);
c=b-a;
S.push(c);break;
case '*':
pop_c(S,a);
pop_c(S,b);
c=b*a;
S.push(c);break;
case '/':
pop_c(S,a);
pop_c(S,b);
if(a!=0)
{
c=b/a;
S.push(c);break;
}else{
cout<<"除0错误"<<endl;
exit(0);
} break;
default:
d=0;
while(*postexp>='0'&&*postexp<='9')
{
d=10*d+*postexp-'0';
postexp++;
}
S.push(d);
break;
}
postexp++;
}
e=S.top();
return e;
}
void test_compvalue()
{ char e1[100],*exp=e1,postexp[100];
string str;
cout<<"请输入中缀表达式:";
cin>>str;
int length=str.copy(e1,str.size());
e1[length]='\0';
trans(exp,postexp);
double e=compvalue(postexp);
cout<<"表达式的结果是:"<<e;}
int main()
{
test_compvalue();
return 0;
}
4.14 关于递归的题
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
typedef struct LNode
{
int data;
LNode *next;
}*LinkList;
void f(int n,int i)
{
if(n==0)
return;
cout<<"前n="<<n<<",前i="<<i<<endl;
i++;
f(n-1,i);
cout<<"后n="<<n<<",后i="<<i<<endl;
}
void change(int num,int i)
{
if(num==0)
return;
change(num/i,i);
cout<<num%i;
}
void test_change()
{
int num,i;
cout<<" 请输入转换的数:";
cin>>num;
cout<<" 输入需要转换成为的进制:";
cin>>i;
cout<<" 10转"<<i<<" : ";
change(num,i);
}
void demo2(int num)
{
if(num==0) return;
demo2(num/10);
cout<<num%10<<" ";
}
void demo3(string str,int n)
{
if(n==0) return;
cout<<str.substr(n-1,1);
demo3(str,n-1);
}
void test_demo3()
{
string str;
cin>>str;
demo3(str,str.size());
}
void demo4(LinkList &L)
{
int i;
LinkList p=new LNode;
cin>>i;
if(i==-999) return;
p->data=i;
p->next=NULL;
if(L!=NULL)
{ p->next=L;
L=p;
}else{
L=p;
cout<<"顺序输出:";
}
demo4(L);
cout<<i<<" ";
}
void travel_list(LinkList &L)
{
LinkList p;
p=L;
while(p)
{
cout<<p->data;
p=p->next;
}
}
void test_demo4()
{
LinkList L;
L=NULL;
cout<<"请倒序插入元素到单链表(输入-999结束):";
demo4(L);
}
void demo5(string str,int &i)
{
if(str.at(i)==*str.rbegin())
{
i++;
return;
}
i++;
demo5(str,i);
}
void test_demo5()
{
string str;
cout<<"请输入字符串:";
cin>>str;
int i=0;
demo5(str,i);
cout<<"字符串长度为:";
cout<<i;
}
int main()
{
test_demo4();
return 0;
}