⑴ 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時用循環,因為出棧的可能是多個運算符。當數字讀入結束回時(也就是字元串結束時)輸出所有在答站內的符號,直到#的出現。