九度 1101 - 字符串处理 - 计算表达式

根据我的通过来看,首先这道题里面没有小数,如果存在除不尽的情况,也是按取整来算。

本题建立了两个栈,一个存储数字的数字栈,一个存储加减乘除的符号栈。在处理字符串的时候,每次找到一个数字时,放进一个string的临时变量里,因为会存在十位以上的情况;每次找到一个符号时,首先将string变量转int放入数字栈,然后检查符号栈的栈顶符号是否为乘或者除,如果是就从符号栈弹出顶,从数字栈弹出两个数,计算后放回数字栈。这样到处理完字符串时,我们的符号栈内只剩下加和减了。此时不断弹出符号与数字进行计算,直至符号栈为空。此时数字栈的栈顶就是答案。

#include<iostream>
#include<stack>
#include<string>
#include <stdlib.h>
using namespace std;
string data;
string str="";
int main(){

	
	while(cin>>data){
		data+='#'; //为了处理到最后一个数字时,仍能继续处理,我们加一个#作为字符串结尾。
		stack<int>n;
		stack<char>f;
		for(int i=0;data[i];i++){
			if(data[i]<='9'&&data[i]>='0'){
				str+=data[i];
			}
			else{
				n.push(atoi(str.c_str()));str="";
				if(!f.empty()){
					char tmp=f.top();
					if(tmp=='*'){
						f.pop();
						int a=n.top();n.pop();
						int b=n.top();n.pop();
						n.push(a*b);
					}
					else if(tmp=='/'){
						f.pop();
						int a=n.top();n.pop();
						int b=n.top();n.pop();
						n.push(b/a);
					}
				}
				if(data[i]!='#')f.push(data[i]);
			}
		}
		while(!f.empty()){
			char tmp=f.top();
			if(tmp=='+'){
					f.pop();
					int a=n.top();n.pop();
					int b=n.top();n.pop();
					n.push(a+b);
			}
			else if(tmp=='-'){
					f.pop();
					int a=n.top();n.pop();
					int b=n.top();n.pop();
					n.push(b-a);
			}
		}
		cout<<n.top()<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,根据提供的引用内容,我无法回答关于西北工业大学计算机考研真题在百度云上的问题。引用提供了一个关于字符串合并的样例输入和输出,而引用和引用提供了一些与西北工业大学计算机研究生的相关信息。但是,这些引用内容都没有提供与百度云相关的信息。如果您有关于其他方面的问题,我将很乐意为您提供帮助和回答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [九度OJ 题目1471:合并符串](https://blog.csdn.net/SJF0115/article/details/8609716)[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: 33.333333333333336%"] - *2* [2021西北工业大学考研历年真题](https://blog.csdn.net/weixin_42502288/article/details/118177726)[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: 33.333333333333336%"] - *3* [undefined](undefined)[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: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值