P1449 后缀表达式(洛谷线性表题单内的一道题)

题目描述

所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级)。

本题中运算符仅包含 +-*/+-*/。保证对于 // 运算除数不为 0。特别地,其中 // 运算的结果需要向 0 取整(即与 C++ / 运算的规则一致)。

如:3*(5-2)+73*(5-2)+7 对应的后缀表达式为:3.5.2.-*7.+@3.5.2.-*7.+@。在该式中,@ 为表达式的结束符号。. 为操作数的结束符号。

输入格式

输入一行一个字符串 �s,表示后缀表达式。

输出格式

输出一个整数,表示表达式的值。

这道题不需要考虑运算符号间的优先级,所以还是比较简单的,重在理解题意,不要过多的关注'.'和’@‘这个符号,没啥用 

代码如下

#include<iostream>
#include<stack>
using namespace std;
int main()
{
    string a;
    cin>>a;
    
	stack<int> p;
for(int i=0;i<a.size();i++)
{
	  if(a[i]>='0'&&a[i]<='9')
			{
				int j=i;
				int need=0;
				
				while(a[j]!='.')
				{
					 int temp=a[j]-'0';
					
					need=need*10+temp;  //两位数及以上得先获得数字再压入栈内
					j++;
					
				}
				i=j-1;  //切记i的值也发生改变
				p.push(need);
	
			}
			
		else if(a[i]=='+')
		{
			int need=0;
				need+=p.top();
				p.pop();
				need+=p.top();
				p.pop();
				
				p.push(need);
	
		}
		else if(a[i]=='-')
		{
			int need=0;
			int temp=p.top();
			p.pop();
			need=p.top()-temp;
			p.pop();
			
			p.push(need);
			
		}
		else if(a[i]=='*')
		{
				int need=0;
				need=p.top();
				p.pop();
				need*=p.top();
				p.pop();
				p.push(need);
		}
		else if(a[i]=='/')
			{
				int need=0;
					int temp=p.top();
					p.pop();
					need=p.top()/temp;
					p.pop();
					p.push(need);
			}
		}
			cout<<p.top()<<endl;
}

有个题目是计算表达式,需要考虑每个运算符号的权重,可以看看

题目描述: 

  • 数据保证给定的表达式合法。
  • 题目保证符号 - 只作为减号出现,不会作为负号出现,例如,-1+2,(2+2)*(-(1+1)+2) 之类表达式均不会出现。
  • 题目保证表达式中所有数字均为正整数。
  • 题目保证表达式在中间计算过程以及结果中,均不超过 231−1231−1。
  • 题目中的整除是指向 00 取整,也就是说对于大于 00 的结果向下取整,例如 5/3=15/3=1,对于小于 00 的结果向上取整,例如 5/(1−4)=−15/(1−4)=−1。
  • C++和Java中的整除默认是向零取整;Python中的整除//默认向下取整,因此Python的eval()函数中的整除也是向下取整,在本题中不能直接使用。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>

using namespace std;

stack<int> num;
stack<char> op;

void eval()
{
    auto b = num.top(); num.pop();
    auto a = num.top(); num.pop();
    auto c = op.top(); op.pop();
    int x;
    if (c == '+') x = a + b;
    else if (c == '-') x = a - b;
    else if (c == '*') x = a * b;
    else x = a / b;
    num.push(x);
}

int main()
{
    unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
    string str;
    cin >> str;
    for (int i = 0; i < str.size(); i ++ )
    {
        auto c = str[i];
        if (isdigit(c))
        {
            int x = 0, j = i;
            while (j < str.size() && isdigit(str[j]))
                x = x * 10 + str[j ++ ] - '0';
            i = j - 1;
            num.push(x);
        }
        else if (c == '(') op.push(c);
        else if (c == ')')
        {
            while (op.top() != '(') eval();
            op.pop();
        }
        else
        {
            while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval();
            op.push(c);
        }
    }
    while (op.size()) eval();
    cout << num.top() << endl;
    return 0;
}

上面的代码原作者是 :yxc
链接:https://www.acwing.com/activity/content/code/content/972603/
来源:AcWing
 

  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构中,线性表是一种常见的数据结构,可以用顺序表或链表来实现。关于线性表的编程目有很多,这里提供两个例子。 1. 删除线性表中所有值为x的数据元素: 引用提供了一个时间复杂度为O(n)、空间复杂度为O(1)的算法来删除线性表中所有值为x的数据元素。该算法使用顺序表来存储数据,并通过遍历顺序表将不等于x的元素前移。具体实现如下: ```cpp void del(Sqlist &L, Elemtype x) { int k = 0; // 顺序表中不等于x的元素个数 for (int i = 0; i < L.length; i++) { if (L.data[i != x) L.data[k++] = L.data[i]; } L.length = k; } ``` 2. 在无头结点的动态单链表上实现线性表操作INSERT(L,i,b): 引用提供了一个在无头结点的动态单链表上实现INSERT操作的算法。具体实现如下: ```cpp typedef struct Node { Elemtype data; struct Node* next; } Node, *LinkList; bool insert(LinkList &L, int i, Elemtype b) { int j = 0; LinkList p = L; while (p && j < i - 1) { p = p->next; j++; } if (!p || j > i - 1) return false; Node* s = new Node; s->data = b; s->next = p->next; p->next = s; return true; } ``` 以上是两个关于线性表的编程的解答,分别是删除线性表中所有值为x的数据元素和在无头结点的动态单链表上实现INSERT操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [数据结构 | 第二章 线性表 WD课后算法编程合集【C++ / 可实现】](https://blog.csdn.net/weixin_47187147/article/details/125744333)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [【严蔚敏数据结构集】C/C++编程线性表练习(一)](https://blog.csdn.net/QianQing_mio/article/details/123295139)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值