1.题目:
中缀表示: a+b-a*((c+d)/e-f)+g--------将中缀变为后缀:
分析过程如图所示:
具体代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *s="a+b-a*((c+d)/e-f)+g";//表示中缀的字符串
char m[30];//表示后缀的字符串
char stack[10];
int n=-1;
int DM(char s){//判断运算符的优先级函数
if(s=='+'||s=='-'){
return 1;
}
if(s=='*'||s=='/'){
return 2;
}
if(s=='('){
return 0;
}
}
int send(char *s,int *top){//中缀变后缀功能函数
if('a'<=*s&&*s<='z'){
m[++n]=*s;
}else if(*top==-1){
stack[++(*top)]=*s;
} else if(*s=='+'||*s=='-'||*s=='*'||*s=='/'){
if(DM(*s)<=DM(stack[*top])){
m[++n]=stack[(*top)--];
send(s,top);
}
if(DM(*s)>DM(stack[*top])){
stack[++(*top)]=*s;}
} else if(*s=='('){
stack[++(*top)]=*s;
}else if(*s==')'){
m[++n]=stack[(*top)--];
if(stack[*top]=='('){
(*top)--;
return 0;
}else{
send(s,top);
}
}
return 0;
}
int main(){
int w=-1;
char *a=m;
while(*s!='\0'){
send(s,&w);
s++;
}
while(w!=-1){
m[++n]=stack[w--];
}
printf("%s",m);
return 0;
}
2.题目:
中缀表示: a+b-a*((c+d)/e-f)+g--------将中缀变为前缀:
分析如下:
在中缀->后缀中的基础上分析,思想倒置。
1.从右往左扫描中缀字符串,
2.新建立的字符串也从右往左写入
具体代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *s="?a+b-a*((c+d)/e-f)+g";//表示中缀的字符串
char m[20];//表示后缀的字符串
char stack[10];
int n=19;
int DM(char s){//判断运算符的优先级函数
if(s=='+'||s=='-'){
return 1;
}
if(s=='*'||s=='/'){
return 2;
}
if(s==')'){
return 0; //此处可以写到star()函数中
}
}
int star(char *s,int *top){
if('a'<=*s&&*s<='z'){//数字(字母)直接写入字符串
m[n--]=*s;
}else if(*s=='+'||*s=='-'||*s=='*'||*s=='/'){//运算符进项栈操作后在进字符串
if(*top==-1){
stack[++(*top)]=*s;//当前扫描到的元素是运算符,且栈为空直接入栈
//return 0;
}else if(DM(*s)<DM(stack[*top])){//当前运算符的优先级小于栈顶运算符的优先级
m[n--]=stack[(*top)--]; //将栈顶元素输出到字符串中
star(s,top);
}else if(DM(*s)>=DM(stack[*top])){//当前运算符的优先级大于等于栈顶运算符的优先级
stack[++(*top)]=*s;//进栈
}
}else if(*s==')'){//诺当前元素为右括号,
stack[++(*top)]=*s;//直接进栈
}else if(*s=='('){//诺当前元素为左括号,
m[n--]=stack[(*top)--];//栈顶出栈进字符串
if(stack[*top]==')'){//当栈顶为右括号时
(*top)--;//出栈一次
//(*top)--;
return 0;
}else{//否则递归思想,直到右括号为止
star(s,top);
}
}
return 0;
}
int main(){
int a=0; int top=-1;
while(*s!='\0'){
s++;
}
while(*s!='?'){
s--;
star(s,&top); //功能函数
//printf("%d",a);
}
printf("%d",top);
while(top!=-1){
m[n--]=stack[top--];
}
while(a<20){
printf("%c",m[a]);
a++;
}
return 0;
}
3.后缀转前缀:
后缀:ab+acd+e/f-*-g+
前缀:+ - +ab *a-/+cdef
实例代码:c++
只要运用字符串和栈相关知识
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
string after="ab+acd+e/f-*-g+";
string head="";
string temp[10];
void weer(string &s,int &top){
string a,b;
if("a"<=s&&s<="z"){
temp[++top]=s;
}else{
a=temp[top--];
b=temp[top--];
b+a;
temp[++top]=s+b+a;
//cout<<temp[top]<<endl;
}
}
int main(){
int i=0;
int top=-1;
string s;
while(i<after.size()){
s=after[i];
//cout<<s;
weer(s,top);
i++;
}
cout<<temp[0];
}
4.前缀转后缀:
从右往左扫描字符串
具体实例代码:与后缀转前缀类似
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
string after="+-+ab*a-/+cdefg";
string head="";
string temp[20];
void weer(string &s,int &top){
string a,b;
if("a"<=s&&s<="z"){
temp[++top]=s;
}else{
a=temp[top--];
b=temp[top--];
b+a;
temp[++top]=a+b+s;
//cout<<temp[top]<<endl;
}
}
int main(){
int i=0;
int top=-1;
string s;
i=after.size()-1;
while(i!=-1){
s=after[i];
//cout<<s;
weer(s,top);
i--;
}
cout<<temp[0];
}