7-20 表达式转换 (25 分) c语言

算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。

输入格式:

输入在一行中给出不含空格的中缀表达式,可包含+-*\以及左右括号(),表达式不超过20个字符。

输出格式:

在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

输入样例:

2+3*(7-4)+8/4

输出样例:

2 3 7 4 - * + 8 4 / +

数据结构:用到了两个堆栈S1,S2

算法:一个个扫描,遇到数字的时候入栈S2
 【遇到数字】需要处理带正负符号的数字以及带小数点的小数 :带正负符号的数字,则有两种情况:一个是开头出现的符号,另一个是符号的前一位是左括号;带小数点的数字,我们需要检查它的后一位是否带的小数点.
 【遇到运算符】的时候分情况:
 情况1:S1空堆或者S1栈顶元素是( 直接入
 情况2:优先级比较大也是直接入栈S1
 情况3:优先级比较小的时候不入栈S1,把S1弹出 压入S2,直到前面的两种情况发生为止 
 【遇到括号】的时候;左括号压入S1;
  右括号的时候比较复杂-> 扫描S1 一直弹出S1 并且压入S2,直到S1的左括号为止,把S1中的左括号废弃掉
  【最后一步】:退出扫描以后,把S1剩余的全部压入S2  

ps:这题的情况是真的多。。。

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

typedef struct pheap{
	char data[50];
	int top;
}heap;
int isEmpty(heap *H)//是否空 
{
	if(H->top==-1)return 1;
	return 0;
}
void add(heap *H,char x)//入栈 
{
	H->data[++H->top]=x;
}
char pop(heap *H)//出栈 
{
	return H->data[H->top--];
}
int Big(char a,char b){//注意括号也要比较优先级 ,此函数若第一个参数优先级大,返回1
	if((a=='*'&&(b=='+'||b=='-'))||(a=='/'&&(b=='+'||b=='-'))||b=='(')
	return 1;
	else return 0;
}
int isnumber(char a)
{
	if(a>=48&&a<=57)return 1;
	return 0;

} 
int get(int start,int *next,char *token,char *str)
{	
	int i,j=0; 
	//无符号的时候
	if(isnumber(str[start]))
	{
		for(i=start;isnumber(str[i])||str[i]=='.';i++)
		{
				token[j++]=str[i];
		}
		
		token[j]='\0';
		*next=i;
		return 1;
	}
	//有符号,两种情况;最开头的时候,或者前一位是括号 
	else if((str[start]=='+'||str[start]=='-') && (start==0||str[start-1]=='('))
	{
		if(str[start]=='-')
		{
			token[0]='-';
			j=1;
		}
		else
		j=0;
		for(i=start+1;isnumber(str[i])||str[i]=='.';i++)
		{
			token[j++]=str[i];
		}
		token[j]='\0';
		*next=i;
		return 1;
	}
	else
	{
		token[0]=str[start];
		token[1]='\0';
		*next=start+1;
		return 0;
	}
}
int main()
{
	int i,j=0,next,start=0;
	char str[100],token[100];
	heap *S1,*S2;
	S1=(heap*)malloc(sizeof(heap));S2=(heap*)malloc(sizeof(heap));//S1储存运算符,S2储存运算数 
	S1->top=S2->top=-1;
	S1->data[S1->top]=S2->data[S2->top]='\0';
	gets(str);
	
	for(i=0;i<strlen(str);i=next)
	{
		int isnum=get(i,&next,token,str);
		//是数字的话入栈S2
		if(isnum)
		{
			for(j=0;token[j]!='\0';j++)
			{
				add(S2,token[j]);
			}
			add(S2,' ');
		 }
		 //是运算符 
		 else if(str[i]!='('&&str[i]!=')')
		 {
		 	while(1){
		 		if(isEmpty(S1)||S1->data[S1->top]=='(')
		    {
		    	add(S1,str[i]);
		    	break;
			}
			else if(Big(str[i],S1->data[S1->top]))			
			{
				add(S1,str[i]);
				break;
			}
			else
			{
				add(S2,pop(S1));
				add(S2,' ');
			}
			 }
		    
		  }
		  //是左右括号 
		  else
		  {
		  	if(str[i]=='(')
		  	{
		  		add(S1,str[i]);
			  }			 
			  else if(str[i]==')')
			  {
			  	while(S1->data[S1->top]!='(')
			  	{
			  		add(S2,pop(S1));
			  		add(S2,' ');
				  }
				 pop(S1);//把左括号废弃掉 
			  }
		   } 
	}
	while(!isEmpty(S1))
	{
		add(S2,pop(S1));
		add(S2,' ');
	}
	pop(S2);
	for(i=0;i<=S2->top;i++)
	{
		printf("%c",S2->data[i]);
	}
	return 0;
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是将中缀表达式转换为二叉树的 C 语言实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义二叉树节点结构体 typedef struct node { char data; struct node *left; struct node *right; } Node; // 定义栈结构体 typedef struct stack { int top; int capacity; Node **array; } Stack; // 初始化栈 Stack *initStack(int capacity) { Stack *stack = (Stack *)malloc(sizeof(Stack)); stack->top = -1; stack->capacity = capacity; stack->array = (Node **)malloc(stack->capacity * sizeof(Node *)); return stack; } // 判断栈是否为空 int isStackEmpty(Stack *stack) { return stack->top == -1; } // 判断栈是否已满 int isStackFull(Stack *stack) { return stack->top == stack->capacity - 1; } // 入栈 void push(Stack *stack, Node *node) { if (isStackFull(stack)) { printf("Stack is full!\n"); return; } stack->array[++stack->top] = node; } // 出栈 Node *pop(Stack *stack) { if (isStackEmpty(stack)) { printf("Stack is empty!\n"); return NULL; } return stack->array[stack->top--]; } // 获取栈顶元素 Node *peek(Stack *stack) { if (isStackEmpty(stack)) { printf("Stack is empty!\n"); return NULL; } return stack->array[stack->top]; } // 判断是否为操作符 int isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } // 创建节点 Node *createNode(char data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } // 中缀表达式转换为二叉树 Node *infixToBinaryTree(char expression[]) { Stack *stack = initStack(strlen(expression)); Node *root = NULL; for (int i = 0; i < strlen(expression); i++) { if (expression[i] == '(') { Node *newNode = createNode(expression[++i]); push(stack, newNode); } else if (isOperator(expression[i])) { Node *newNode = createNode(expression[i]); newNode->left = pop(stack); newNode->right = createNode(expression[++i]); push(stack, newNode); } else if (expression[i] == ')') { root = pop(stack); } else { Node *newNode = createNode(expression[i]); push(stack, newNode); } } return root; } // 中序遍历输出二叉树 void inOrderTraversal(Node *root) { if (root) { inOrderTraversal(root->left); printf("%c ", root->data); inOrderTraversal(root->right); } } // 测试函数 int main() { char expression[] = "((a+b)*(c-d))/e"; Node *root = infixToBinaryTree(expression); inOrderTraversal(root); printf("\n"); return 0; } ``` 以上代码基于栈实现,首先定义了 `Node` 结构体作为二叉树节点,然后定义了 `Stack` 结构体作为栈,包括初始化、入栈、出栈等基本操作。`isOperator` 函数用于判断是否为操作符,`createNode` 函数用于创建节点。`infixToBinaryTree` 函数实现了中缀表达式转换为二叉树的具体操作,最后通过中序遍历输出二叉树。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值