输入一个只包含个位数字的简单四则运算表达式字符串,计算该表达式的值

问题描述:

输入一个只包含个位数字的简单四则运算表达式字符串,计算该表达式的值

注:1、表达式只含+, -, *, / 四则运算符,不含括号

2、表达式数值只包含个位整数(0-9),且不会出现0作为除数的情况

3、要考虑加减乘除按通常四则运算规定的计算优先级

4、除法用整数除法,即仅保留除法运算结果的整数部分。比如8/3=2。输入表达式保证无

0作为除数情况发生

5、输入字符串一定是符合题意合法的表达式,其中只包括数字字符和四则运算符字符和(),除

此之外不含其它任何字符,不会出现计算溢出情况



利用后缀式计算,若是数字则直接入栈,若是运算符则出栈两次,进行计算,并把结果再入栈。

#include <iostream>  
#include <stack>
using namespace std;  

//中缀转后缀
int cmp(char c1,char c2)//相同为0,
{
	int v1=0,v2=0;
	if(c2=='#')
		return -1;

	if(c1=='+' || c1=='-')
		v1=1;
	if(c1=='*' || c1=='/')
		v1=2;
	

	if(c2=='+' || c2=='-')
		v2=1;
	if(c2=='*' || c2=='/')
		v2=2;

	return v2-v1;
	
}
void change(char *p1,char *p2)
{
	stack<char> st;
	st.push('#');
	int i=0,j=0;
	int len=strlen(p1);
	for(i=0;i<len;i++)
	{
		
		if(p1[i]>='0' && p1[i]<='9')
			p2[j++]=p1[i];
		
		 if(p1[i]=='+'||p1[i]=='-'||p1[i]=='*'||p1[i]=='/')  
		 {
			 if(cmp(st.top(),p1[i])>0)
			 {
				 st.push(p1[i]);
			 }
			 else
			 {
				 while(st.top()!='#' && cmp(st.top(),p1[i])<=0)
				{
					p2[j++]=st.top();
					 st.pop();
				 }
				 st.push(p1[i]);
			 }
		 }
	}
	while(st.top()!='#')
	{
		p2[j++]=st.top();
		 st.pop();
	}
	p2[j]='\0';
}
void compute(char *p)
{
	stack<int> st;
	for(int i=0;i<strlen(p);i++)
	{
		if(p[i]>='0' && p[i]<='9')
		{
			st.push(p[i]-'0');
		}
		else
		{
			int n=0;
			int n1=st.top();
			st.pop();
			int n2=st.top();
			st.pop();
			if(p[i]=='+')
				n=n1+n2;
			else if(p[i]=='-')
				n=n2-n1;
			else if(p[i]=='*')
				n=n1*n2;
			else if(p[i]=='/')
				n=n2/n1;
			st.push(n);
		}
	}
	cout<<st.top()<<endl;
}
int main()  
{  
	char p1[100];  
    char p2[100];  
    cin>>p1;  
    change(p1,p2);  
    cout<<p2<<endl;  
	compute(p2);
    system("pause");  
}  


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较完整的代码,包括了输入、转换、计算和输出的过程。可以直接在Visual Studio 2019中新建一个C语言控制台应用程序,将以下代码复制到main函中即可。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAXSIZE 100 // 定义栈的最大容量 // 定义运算符优先级 int priority(char op) { switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; default: return 0; } } // 中缀表达式转换为后缀表达式 int infix2postfix(char *infix, char *postfix) { char stack[MAXSIZE]; // 定义栈 int top = -1; // 栈顶指针 int i = 0, j = 0; // i遍历输入的中缀表达式,j指向输出的后缀表达式 while (infix[i] != '\0') { if (isdigit(infix[i])) { postfix[j++] = infix[i++]; while (isdigit(infix[i])) { // 处理多位数字 postfix[j++] = infix[i++]; } postfix[j++] = ' '; // 数字后面加个空格 } else if (infix[i] == '(') { stack[++top] = infix[i++]; } else if (infix[i] == ')') { while (stack[top] != '(') { postfix[j++] = stack[top--]; postfix[j++] = ' '; } top--; // 弹出左括号 i++; } else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/') { while (top >= 0 && priority(stack[top]) >= priority(infix[i])) { postfix[j++] = stack[top--]; postfix[j++] = ' '; } stack[++top] = infix[i++]; } else { // 忽略空格等非法字符 i++; } } while (top >= 0) { // 将栈中剩余的运算符弹出 postfix[j++] = stack[top--]; postfix[j++] = ' '; } postfix[j] = '\0'; // 后缀表达式字符串以'\0'结尾 return 0; } // 计算后缀表达式 int calc_postfix(char *postfix, double *result) { double stack[MAXSIZE]; // 定义栈 int top = -1; // 栈顶指针 int i = 0; double x, y, z; while (postfix[i] != '\0') { if (isdigit(postfix[i])) { sscanf(postfix + i, "%lf", &z); // 从字符串中读取数字 stack[++top] = z; // 数字入栈 while (isdigit(postfix[i]) || postfix[i] == '.') { // 跳过数字 i++; } i++; // 跳过空格 } else if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/') { y = stack[top--]; // 弹出栈顶元素 x = stack[top--]; switch (postfix[i++]) { case '+': z = x + y; break; case '-': z = x - y; break; case '*': z = x * y; break; case '/': z = x / y; break; } stack[++top] = z; // 运算结果入栈 } else { // 忽略空格等非法字符 i++; } } *result = stack[top]; // 最终结果为栈中唯一元素 return 0; } int main() { char infix[MAXSIZE], postfix[MAXSIZE]; double result; printf("请输入中缀表达式:\n"); fgets(infix, MAXSIZE, stdin); // 从标准输入中读取一行字符 infix2postfix(infix, postfix); printf("后缀表达式为:%s\n", postfix); calc_postfix(postfix, &result); printf("计算结果为:%f\n", result); return 0; } ``` 这个程序可以处理整和小四则运算表达式,支持加、减、乘、除、括号等运算符,可以自动跳过空格等非法字符。使用时只需要在控制台中输入中缀表达式,程序会自动输出后缀表达式计算结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值