当前位置:首页 » 编程语言 » 后缀式转中缀式c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

后缀式转中缀式c语言

发布时间: 2022-05-19 21:20:19

c语言后缀表达式转换成中缀表达式

在计算机上用的
1、利用栈来实现
2、利用语法树来实现
先把中缀表达式用二叉树表示出来,再后序遍历该二叉树就得到相应的后缀表达式了
在草稿上手工转换
3、加括号法

⑵ C语言代码将中缀表达式转换为后缀表达式,参数为字符型数组的中缀表达式,返回字符型数组的后缀表达式,

你自己改为C吧
#include<iostream.h>
const int MAX=40;
void main(void){
char infix[MAX]={'#'};
char oprator[MAX]={'@','#'};
int opr=1;
char postfix[12]={'#'};
int post=0;
int i,j,cnt=0,cntl;
char c;
//输入表达式,以等号结束
cin.get(c);
while(c!='='){
infix[cnt]=c;
cnt++;
cin.get(c);
}
cntl=cnt;
for(i=0;i<cnt;i++){
switch(infix[i]){
//左括号就直接入栈
case '(':
cntl=cntl-2;
oprator[opr]=infix[i];
opr++;
break;
//右括号则先退栈,直到遇见第一个左括号
case ')':
for(j=opr-1;j>0;j--){
if(oprator[j]!='('){
postfix[post]=oprator[j];
oprator[j]='#';
post++;
}
else{
oprator[j] = '#';
break;
}
}
opr=j;
break;
case '*':
case '/':
//如果前一个运算符为*或/,则先退栈,再入栈,否则直接入栈
if (oprator[opr] == '*' || oprator[opr] == '/') {
postfix[post] = oprator[opr];
oprator[opr]='#';
post++;
}
oprator[opr] = infix[i];
opr++;
break;
case '+' :
case '-' :
//如果上一个运算符不是左括号也不是栈顶,则先退栈再入栈
if (oprator[opr-1] != '(' && oprator[opr-1] != '@') {
postfix[post] = oprator[opr];
oprator[opr]='#';
}
oprator[opr] = infix[i];
opr++;
break;
default :
//如果是数字则直接进入后缀表达式数组
postfix[post] = infix[i];
post++;
break;
}
}
//如果扫描完成,则退栈
for(j=opr-1;j>0;j--){
if(oprator[j]!='@'){
postfix[post]=oprator[j];
oprator[j]='#';
}
else
break;

⑶ 后缀式转中缀式如何用c语言实现

用堆栈试试吧。

⑷ 谁知道用C++实现后缀变中缀的算法呀

后缀式转中缀式么?

下面是代码,如有帮助,谢谢采纳

#include <iostream>
#include <string>
#include <stack>
using namespace std;
/* 运算符类,含符号和优先级,目前只支持四则运算 */
struct exp_symbol{
char op;
int priority;
exp_symbol(char op=0)
{
this->op = 0;
priority = 255;
if( '+' == op || '-' == op )
{
this->op = op;
priority = 1;
}
else if( '*' == op || '/' == op )
{
this->op = op;
priority = 2;
}
}
exp_symbol(const exp_symbol &sym):op(sym.op),priority(sym.priority){ }
friend bool operator < (const exp_symbol &l_sym, const exp_symbol &r_sym)
{
return (l_sym.priority < r_sym.priority) ? true : false;
}
exp_symbol & operator = (const exp_symbol &r_sym)
{
if( this != &r_sym )
{
op = r_sym.op;
priority = r_sym.priority;
}
return *this;
}
};
/* 表达式类,表达式字符串和运算符构成 */
struct expression{
string expr;
exp_symbol ex_sym;
expression(double x):ex_sym(0)
{
char buffer[64];
sprintf(buffer,"%g", x);
expr = buffer;
}
expression(const exp_symbol &sym, const string &l_expr, const exp_symbol &l_sym, const string &r_expr, const exp_symbol &r_sym):ex_sym(sym)
{
char op = sym.op;
if( l_sym < sym )
expr = "( " + l_expr + " ) " + op + ' ';
else
expr = l_expr + ' ' + op + ' ';
if( sym < r_sym )
expr += r_expr;
else
expr = expr + "( " + r_expr + " )";
}
expression(const expression &exp):expr(exp.expr), ex_sym(exp.ex_sym){ }
};
/* 从字符串中输入一个运算符 */
static int getsymbol(const char * &src_expr, exp_symbol &op_sym)
{
char op=0;
int len=0;
do {
if(0 == sscanf(src_expr, "%c%n", &op, &len))
break;
src_expr += len;
} while( ' ' == op );
op_sym = exp_symbol(op);
return (int)op_sym.op;
}
/**
* @brief 后缀表达式转中缀表达式
* @note <表达式>=<常数>|<表达式><运算符><表达式>
* @note 遇到常数,压栈;遇到运算符,从栈中弹出两个表达式,与运算符合成新的表达式,压栈;直到输入结束
*/
int PostToMiddleExpr(const char * src_expr, string & dst_expr)
{
double x = 0.0;
int len=0, bValid=1;
exp_symbol op_sym(0);
std::stack<expression> stExpr;

while( bValid )
{
bValid = 0;
if( sscanf(src_expr, "%lf%n", &x, &len) > 0 ) /* 运算数,直接入栈 */
{
src_expr += len;
stExpr.push( expression(x) );
bValid = 1;
}
else if( getsymbol(src_expr, op_sym) > 0 ) /* 运算符,出栈两个表达式,组成新的表达式 */
{
if(!stExpr.empty())
{
const expression &expr2 = stExpr.top();
string r_expr = expr2.expr;
exp_symbol r_sym = expr2.ex_sym;
stExpr.pop();
if(!stExpr.empty())
{
const expression &expr1 = stExpr.top();
string l_expr = expr1.expr;
exp_symbol l_sym = expr1.ex_sym;
stExpr.pop();
stExpr.push(expression(op_sym, l_expr,l_sym, r_expr, r_sym));
bValid = 1;
}
}
}
}

/* 判断表达式栈并返回中缀表达式 */
if(stExpr.empty())
return -1;
dst_expr = stExpr.top().expr;
stExpr.pop();
if(!stExpr.empty())
{
do{
stExpr.pop();
}while(!stExpr.empty());
return -1;
}
return 0;
}
/**
* 后缀表达式转中缀表达式的试验程序~
*/
int main( void )
{
string dst_expr;
const char *src_str = "19.3 23.4 - 3.5 * 1.7 + 1.4 / 3.4 *";
PostToMiddleExpr( src_str, dst_expr);
cout << "dst_expr = "<< dst_expr << endl;
return 0;
}

⑸ c语言数据结构栈,后缀表达式转中缀表达式,代码不能执行

网上说的都比较麻烦,其实很简单:首先你要知道一点就是中缀转为后缀时操作数的顺序是不会变的。另外“(”也不会出现在后缀表达式中。然后,你可以这样看,在这个表达式中,按照运算法则,应该先算(-B)(这里你的表达式里应该是少了个括号),所以就是“B-”在一起,然后再把(-B)的结果乘以A,就变成了AB-*(因为是A*(-B),所以A在B前,而“*”在“-”的后面),然后将上面的结果+C,同样的道理分析,自然就是:AB-*C+了。(注:因为C是在AB的后面,所以C在*的后面)如果中缀表达式是:C+A*(-B),则后缀表达式即为:CAB-*+。希望你能理解!!!同样的例子,请参见:?oldq=1

⑹ 如何将中缀式转换成后缀式 C语言 递归

思路的话其实很简单,就是构建一棵二叉树,根节点和中间节点为运算符,叶子结点为运算数字。如 a + b*c, 构建为二叉树的话,就如下图: +a * b c对于该二叉树,使用不同的遍历方式就可以得到不同的表达式了。遍历的代码很简单就不多说了。因此,你的问题主要可以分解为3个小问题:1。将后缀表达式转换为二叉树 该方法是最简单的。如a + b*c 的后缀表达式为 bc*a+.处理步骤如下: 1。建立一个栈S
2。从左到右读后缀表达式,读到数字就创建叶子节点,节点值为数字值。将节点压入栈S中,读到运算符则创建中间节点,并从栈中依次弹出两个节点分别为Y和X,作为中间节点的左右子节点,然后以“X 运算符 Y”的形式计算机出中间节点的值,再将此中间节点压加栈S中 3。就重复第二步直至后缀表达式结束,此时栈顶的节点就是二叉树的根节点了。2。将中缀表达式转换为二叉树 按照上一个回答者的方法将中缀表达式转为后缀表达式,然后调用后缀表达式生成二叉树的解法即可。3。将前缀表达式转换为二叉树 将前缀表达式直接取反即为后缀表达式。 如前缀表达式为+*bca,对应的后缀表达式为acb*+。因此,我们只需要字符串取反,然后调用后缀表达式的方法生成二叉树即可。

⑺ 中缀表达式换成后缀表达式的问题(c语言)

原输入字符串后面不用添加#,字符串的结束符是'\0'

#不应该参与比较,但是要第一个压入堆栈,a数组定义为[6][6]。在poplinkstack时用循环,因为出栈的可能是多个运算符。当数字读入结束时(也就是字符串结束时)输出所有在站内的符号,直到#的出现。

⑻ C++后缀表达式转中缀表达式

#include <iostream> using namespace std; #define MaxSize 99 void trans(char str[],char exp[]) {

struct

{

char data[MaxSize];

int top;

}op;

char ch;

int i = 0, t = 0;

op.top = -1;

ch = str[i];

i++;

while (ch != '\0')

{

switch (ch)

{

case'(':

op.top++;

op.data[op.top] = ch;

break;

case')':

while (op.data[op.top] != '(')

{

exp[t] = op.data[op.top];

op.top--;

t++;

}

op.top--;

break;

case'+':

case'-':

while (op.top != -1 && op.data[op.top] != '(')

{

exp[t] = op.data[op.top];

op.top--;

t++;

}

op.top++;

op.data[op.top] = ch;

break;

case'*':

case'/':

while (op.top == '/' || op.top == '*')

{

exp[t] = op.data[op.top];

op.top--;

t++;

}

op.top++;

op.data[op.top] = ch;

break;

case' ':

break;

default:

while (ch >= '0' && ch <= '9')

{

exp[t] = ch;

t++;

ch = str[i];

i++;

}

i--;

exp[t] = '#';

t++;

}

ch = str[i];

i++;

}

while (op.top != -1)

{

exp[t] = op.data[op.top];

t++;

op.top--;

}

exp[t] = '\0'; }
float compvalue(char exp[]) {

struct

{

float data[MaxSize];

int top;

}st;

float d;

char ch;

int t = 0;

st.top = -1;

ch = exp[t];

t++;

while (ch != '\0')

{

switch (ch)

{

case'+':

st.data[st.top - 1] = st.data[st.top - 1] + st.data[st.top];

st.top--;

break;

case'-':

st.data[st.top - 1] = st.data[st.top - 1] - st.data[st.top];

st.top--;

break;

case'*':

st.data[st.top - 1] = st.data[st.top - 1] - st.data[st.top];

st.top--;

break;

case'/':

if (st.data[st.top] != 0)

st.data[st.top - 1] = st.data[st.top - 1] - st.data[st.top];

else

{

printf("\n\t除0是错误的");

}

st.top--;

break;

default:

;

d = 0;

while (ch >= '0' && ch <= '9')

{

d = 10 * d + ch - '0';

ch = exp[t];

t++;

}

st.top++;

st.data[st.top] = d;

}

ch = exp[t];

t++;

}

return st.data[st.top]; } int main() {

char str[MaxSize], exps[MaxSize];

printf("表达式:");

gets(str);

trans(str,exps);

printf("后缀表达式:%s\n",exps);

printf("计算结果:%g\n",compvalue(exps));

return 0; }

⑼ C语言问题 关于栈的应用,数学运算后缀表达式转换成中缀表达式

#include <string.h>
#include <iostream.h>
#include <stdlib.h>

#define MaxSize 32

typedef char Qelemtype;

typedef struct
{
Qelemtype *base; //指向队列的存储空间;
int front; //指向队头元素;
int rear; //指向队尾元素的下一位置;
}SqQueue;

typedef struct LNode
{
int data;
struct LNode *next;
}Snode,*LinkStack;

void DestroyLinStack(LinkStack &S)
{//销毁链栈S。
LinkStack temp=S,p;
while(temp)
{
p=temp;
temp=temp->next;
free(p);
}
}

void Push(LinkStack &S, char x)
{//入栈。
LinkStack temp=(LinkStack )malloc(sizeof(Snode ));
temp->data=x;
temp->next=S->next;
S->next=temp;
}

void Pop(LinkStack &S, char &x)
{//出栈。
LinkStack temp=S->next;
x=temp->data;
S->next=temp->next;
free(temp);
}

int GetTop(LinkStack &S)
{//读栈顶元素.
int x;
if(S->next)
x=S->next->data;
else
cout<<"Stack Null "<<endl;
return x;

}

void Initstack(LinkStack &S)
{
S=(LinkStack )malloc(sizeof(Snode ));
if(!S)
{
cout<<"Alloctation Error"<<endl;
exit(1);
}
S->next=0;
}

int InitQueue(SqQueue &Q)
{//队列的初始化;
Q.front=Q.rear=0;
Q.base=(Qelemtype *)malloc(MaxSize*sizeof(Qelemtype));
if(!Q.base)
exit(1);
Q.base[Q.front]='\0';
return 1;
}

int QueueLength(SqQueue Q)
{//计算队列的长度;
return (Q.rear-Q.front+MaxSize)%MaxSize;
}

void EnQueue(SqQueue &Q, Qelemtype x)
{//入队;
if(QueueLength(Q)==MaxSize)
{
cout<<"EnQueue Error "<<endl;
return ;
}
Q.base[Q.rear++]=x;
}

void DispQueue(SqQueue Q)
{//输出队列的所有元素;
int i=0,j=Q.front;
while(i<QueueLength(Q))
{
cout<<Q.base[j];
j++;
i++;
}
}

void DestroyQueue(SqQueue &Q)
{
//队列的销毁;
delete []Q.base;
Q.base=0;
Q.front=Q.rear=0;
return ;
}

int StackEmpty(LinkStack S)
{//判断栈是否为空.
return (S->next==0);
}

int Priority(char oper)
{
switch(oper)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
}

void convertpostexp(char *str,SqQueue &post)
{
char c,t;
int i=0,k=strlen(str);
LinkStack S;
Initstack(S);
Push(S,'(');
InitQueue(post);
while(i<k || !StackEmpty(S))
{
c=*str++;
switch(c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
EnQueue(post, c);
break;
case '(':
Push(S,c);
break;
case ')':
case ';':
do{
Pop(S,t);
if(t!='(')
EnQueue(post, t);
}while(t!='(' && !StackEmpty(S));
break;
case '+':
case '-':
case '*':
case '/':
while(Priority(c)<=Priority(GetTop(S)))
{
Pop(S,t);
EnQueue(post, t);
}
Push(S,c);
break;
}
i++;
}
DestroyLinStack(S);
}

void main()
{
SqQueue post;
char str[32];
cout<<"请输入计算表达式:(在最后要加上;)"<<endl;
cin>>str;
convertpostexp(str,post);
cout<<"转换成后缀表达式是:"<<endl;
DispQueue(post);
cout<<endl;
DestroyQueue(post);
}

⑽ C语言中缀表达式换后缀表达式,有错,求解

原输入字符串后面不用添加#,字符串的结束符是'\0'

#不应该参与比较,但是要第一个压入堆栈,a数组定义为[6][6]。在poplinkstack时用循环,因为出栈的可能是多个运算符。当数字读入结束回时(也就是字符串结束时)输出所有在答站内的符号,直到#的出现。