中綴轉後綴c語言
Ⅰ 中綴表達式換成後綴表達式的問題(c語言)
原輸入字元串後面不用添加#,字元串的結束符是'\0'
#不應該參與比較,但是要第一個壓入堆棧,a數組定義為[6][6]。在poplinkstack時用循環,因為出棧的可能是多個運算符。當數字讀入結束時(也就是字元串結束時)輸出所有在站內的符號,直到#的出現。
Ⅱ 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語言將中綴表達式轉換為後綴表達式的程序出了點小問題,謝謝各位解答一下!
#include <stdio.h>
#define MAXCOLS 100
typedef struct
{
int top;
char items[MAXCOLS];
}stack;
int empty(stack *ps)
{
if(ps->top==0)
return 1;
else
return 0;
}
char pop(stack *ps)
{
char x;
if(empty(ps))
{
printf("%s","stack underflow");
x=NULL;
}
else
x=(ps->items)[ps->top--];
return x;
}
void push(stack *ps,char x)
{
if(ps->top==MAXCOLS-1)
{
printf("%s","stack overflow");
}
else
ps->items[++(ps->top)]=x;
return;
}
親這是我修改後的你看一下有什麼不懂再問我,希望採納!!!
Ⅳ 如何將中綴式轉換成後綴式 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語言編程
#include<stdio.h>
#include<stdlib.h>
typedef struct astack *Stack;
typedef struct astack
{
int top;
int maxtop;
char* data;
}Astack;
Stack NewEmpty(int size)
{
Stack S=(Stack)malloc(sizeof(Astack));
S->maxtop=size;
S->top=-1;
S->data=(char*)malloc(size*sizeof(char));
return S;
}
int StackEmpty(Stack S)
{
return S->top<0;
}
int StackFull(Stack S)
{
return S->top==S->maxtop;
}
int Peek(Stack S)
{
return S->data[S->top];
}
void Push(char x,Stack S)
{
if(StackFull(S))
{
printf("Stack is full!\n");
exit(1);
}
else
S->data[++S->top]=x;
}
int Pop(Stack S)
{
if(StackEmpty(S))
{
printf("Stack is empty!\n");
exit(1);
}
else
return S->data[S->top--];
}
Stack NewStack(int size)
{
Stack S=NewEmpty(size);
int i,x,num;
printf("Please enter the number of data:\n");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("Please enter the %d date:\n",i+1);
scanf("%c",&x);
Push(x,S);
}
return S;
}
void ShowStack(Stack S)
{
int i;
for(i=0;i<=S->top;i++)
{
printf(" %c",S->data[i]);
}
printf("\n");
}
Ⅵ 用C語言實現中綴表達式到後綴表達式的轉換 求改正!
main函數中:
printf("%2c",queueempty(q));
改為
printf("%2c",q->data[q->front++]);
測試:
1+2*3#
1 2 3 * +
Ⅶ 在c語言中,如何將中綴表達式轉換成後綴表達式呢
(A==0&&B!=0)||(B==0&&A!=0)
((x>10&&x<100)||x<0)&&x!=-2.0
Ⅷ 如何在程序中將中綴表達式轉換為後綴表達式
中綴表達式轉換為後綴表達式的方法
a + b * c - (d + e)
按照運算符的優先順序對所有的運算單位加括弧。
((a + (b * c)) - (d + e))
轉換中綴與後綴表達式後綴:把運算符號移動到對應的括弧後面。
((a (b c) * ) + (d e) + ) -
把括弧去掉,記得到了後綴表達式
a b c * + d e + -
可以發現,後綴表達式是不需要括弧來調整運算優先順序的。
Ⅸ 哪個幫我做一個中綴表達式轉換成後綴表達式,要C語言,要帶括弧哦,我寫一下午了沒寫出來,嗚嗚~~
參考這個:
運行環境,VC , BCB
#include<iostream>
#include <vector>
using namespace std;
/*********************** 棧操作 ************************/
/*********************************************************/
const int MAXSIZE =100;
template <class ElemType> class MyStack
{
public:
ElemType data[MAXSIZE];
int top;
public:
void init(); // 初始化棧
bool empty(); // 判斷棧是否為空
ElemType gettop(); // 讀取棧頂元素(不出棧)
void push(ElemType x); // 進棧
ElemType pop(); // 出棧
};
template<class T> void MyStack<T>::init()
{
this->top = 0;
}
template<class T> bool MyStack<T>::empty()
{
return this->top == 0? true : false;
}
template<class T> T MyStack<T>::gettop()
{
if(empty())
{
cout << "棧為空!\n";
exit(1);
}
return this->data[this->top-1];
}
template<class T> void MyStack<T>::push(T x)
{
if(this->top == MAXSIZE)
{
cout << "棧已滿!\n";
exit(1);
}
this->data[this->top] =x;
this->top ++;
}
template<class T> T MyStack<T>::pop()
{
if(this->empty())
{
cout << "棧為空! \n";
exit(1);
}
T e =this->data[this->top-1];
this->top --;
return e;
}
/*********************** 函數聲明 ************************/
bool isoperator(char op); // 判斷是否為運算符
int priority(char op); // 求運算符優先順序
void postfix(char pre[] , char post[],int &n); // 把中綴表達式轉換為後綴表達式
double read_number(char str[],int *i); // 將數字字元串轉變成相應的數字
double postfix_value(char post[]); // 由後綴表達式字元串計算相應的中值表達式的值
/*********************************************************/
double read_number(char str[],int *i)
{
double x=0.0;
int k = 0;
while(str[*i] >='0' && str[*i]<='9') // 處理整數部分
{
x = x*10+(str[*i]-'0');
(*i)++;
}
if(str[*i]=='.') // 處理小數部分
{
(*i)++;
while(str[*i] >= '0'&&str[*i] <='9')
{
x = x * 10 + (str[*i]-'0');
(*i)++;
k++;
}
}
while(k!=0)
{
x /= 10.0;
k--;
}
return x;
}
// 把中綴表達式轉換為後綴表達式,返回後綴表達式的長度(包括空格)
void postfix(char pre[] ,char post[],int &n)
{
int i = 0 ,j=0;
MyStack<char> stack;
stack.init(); // 初始化存儲操作符的棧
stack.push('#'); // 首先把結束標志『#』放入棧底
while(pre[i]!='#')
{
if((pre[i]>='0' && pre[i] <='9')||pre[i] =='.') // 遇到數字和小數點直接寫入後綴表達式
{
post[j++] = pre[i];
n++;
}
else if (pre[i]=='(') // 遇到「(」不用比較直接入棧
stack.push(pre[i]);
else if(pre[i] ==')') // 遇到右括弧將其對應左括弧後的操作符(操作符棧中的)全部寫入後綴表達式
{
while(stack.gettop()!='(')
{
post[j++] = stack.pop();
n++;
}
stack.pop(); // 將「(」出棧,後綴表達式中不含小括弧
}
else if (isoperator(pre[i]))
{
post[j++] = ' '; // 用空格分開操作數(
n++;
while(priority(pre[i]) <= priority(stack.gettop()))
{
// 當前的操作符小於等於棧頂操作符的優先順序時,將棧頂操作符寫入到後綴表達式,重復此過程
post[j++] = stack.pop();
n++;
}
stack.push(pre[i]); // 當前操作符優先順序大於棧頂操作符的優先順序,將該操作符入棧 }
i++;
}
while(stack.top) // 將所有還沒有出棧的操作符加入後綴表達式
{
post[j++] = stack.pop();
n++;
}
}
// 判斷是否為運算符
bool isoperator(char op)
{
switch(op)
{
case '+':
case '-':
case '*':
case '/':
return 1;
default :
return 0;
}
}
// 求運算符優先順序
int priority(char op)
{
switch(op)
{
case '#':
return -1;
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default :
return -1;
}
return -1;
}
// 將數字字元串轉變成相應的數字
double readnumber(char str[],int *i)
{
double x=0.0;
int k = 0;
while(str[*i] >='0' && str[*i]<='9') // 處理整數部分
{
x = x*10+(str[*i]-'0');
(*i)++;
}
if(str[*i]=='.') // 處理小數部分
{
(*i)++;
while(str[*i] >= '0'&&str[*i] <='9')
{
x = x * 10 + (str[*i]-'0');
(*i)++;
k++;
}
}
while(k!=0)
{
x /= 10.0;
k--;
}
return x;}
// 由後綴表達式字元串計算相應的中值表達式的值
double postfix_value(char post[])
{
MyStack<double> stack; // 操作數棧
stack.init();
int i=0 ;double x1,x2;
while(post[i] !='#')
{
if(post[i] >='0' && post[i] <='9')
stack.push(read_number(post,&i));
else if(post[i] == ' ')
i++;
else if (post[i] =='+')
{
x2 = stack.pop();
x1 = stack.pop();
stack.push(x1+x2);
i++;
}
else if (post[i] =='-')
{
x2 = stack.pop();
x1 = stack.pop();
stack.push(x1-x2);
i++;
}
else if (post[i] =='*')
{
x2 = stack.pop();
x1 = stack.pop();
stack.push(x1*x2);
i++;
}
else if (post[i] =='/')
{
x2 = stack.pop();
x1 = stack.pop();
stack.push(x1/x2);
i++;
}
}
return stack.gettop();
}
// main()函數
void main(){
char pre[] ="2*((6-4)+8/4)#";
char post[100] ;
cout <<"中綴表達式為:"<< pre << endl;
int n =0; // 返回後綴表達式的長度
postfix(pre,post,n);
cout <<"後綴表達式為:";
for( int i =0 ;i < n ;i++)
cout << post[i] ;
cout << "\n由後綴表達式計算出的數值結果: ";cout << postfix_value(post) << endl;
system("pause");}
Ⅹ C語言中綴表達式換後綴表達式,有錯,求解
原輸入字元串後面不用添加#,字元串的結束符是'\0'
#不應該參與比較,但是要第一個壓入堆棧,a數組定義為[6][6]。在poplinkstack時用循環,因為出棧的可能是多個運算符。當數字讀入結束回時(也就是字元串結束時)輸出所有在答站內的符號,直到#的出現。