中缀转后缀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时用循环,因为出栈的可能是多个运算符。当数字读入结束回时(也就是字符串结束时)输出所有在答站内的符号,直到#的出现。