北京理工大学2010年计算机考研机试题

在这里插入图片描述

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int a;
	vector<int>vc;
	vector<int>::const_iterator it;
	cout<<"请输入一串整数,以0为结束"<<endl;
	while(cin>>a)
	{
		if(a==0)
		break;
		vc.push_back(a);
	}
	cout<<"当前数据为:";
	for(it=vc.begin();it!=vc.end();it++)
	cout<<*it<<' ';
	cout<<endl;
	char op;
	while(1)
	{
		cin>>op;
		if(op=='a')
		{
			int t;
			cin>>t;
			vc.push_back(t);
			cout<<"当前数据为:";
			for(it=vc.begin();it!=vc.end();it++)
			cout<<*it<<' ';
			cout<<endl;
		}
		else if(op=='c')
		{
			char a,b;
			int i,j;
			cin>>a>>i>>b>>j;
			replace(vc.begin(),vc.end(),i,j);//将值为i的数改成值为j
			cout<<"当前数据为:";
			for(it=vc.begin();it!=vc.end();it++)
			cout<<*it<<' ';
			cout<<endl;
		}
		else if(op=='d')
		{
			int t;
			cin>>t;
			vector<int>::const_iterator it1;
			for(it=vc.begin();it!=vc.end();it++)
			if(*it==t)
			{
				vc.erase(it);
				it--;
			}
			//it1=find(vc.begin(),vc.end(),t);
			//vc.erase(it1);
			cout<<"当前数据为:";
			for(it=vc.begin();it!=vc.end();it++)
			cout<<*it<<' ';
			cout<<endl;
		}
		else if(op=='s')
		{
			sort(vc.begin(),vc.end());
			cout<<"当前数据为:";
			for(it=vc.begin();it!=vc.end();it++)
			cout<<*it<<' ';
			cout<<endl;
		}
		else
		break; 
	}
	
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<string>
#include<stack>
#include<iomanip>
using namespace std;
int fun_yxj(char c)
{
	int yxj;
	switch(c)
	{
		case '+':yxj=2;break;
		case '-':yxj=2;break;
		case '*':yxj=3;break;
		case '/':yxj=3;break;
	}
	return yxj;
}
double value(double op1,double op2,char c)
{
	switch(c)
	{
		case '+':return op1+op2;break;
		case '-':return op1-op2;break;
		case '*':return op1*op2;break;
		case '/':return (op1*1.00)/op2;break;
	}
}
void z_to_h(string str)//中缀转后缀函数 
{
	stack<char>s;//结果栈 
	stack<char>op;//运算符栈 
	char str1[str.length()];
	for(int i=0;i<str.length();i++)
	{
		if(str[i]>='0'&&str[i]<='9')//如果是数字,直接压入结果栈 
		s.push(str[i]);
		else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/') //如果是运算符 
		{
			while(true)
			{
				if(op.empty()||op.top()=='(')//运算符栈为空或栈顶为"(" 
				{
					op.push(str[i]);
					break;
				}
				else if(fun_yxj(str[i])>fun_yxj(op.top()))//当前操作符的优先级大于栈顶操作符的优先级
				{
					op.push(str[i]);
					break;
				} 
				else//当前操作符的优先级小于等于栈顶操作符的优先级
				{
					char a=op.top();
					op.pop();
					s.push(a); 
				}
			}
		}
		else
		{
			if(str[i]=='(')//遇到左括号 
			op.push(str[i]);
			else//遇到右括号 
			{
				while(op.top()!='(') //把括号之间的运算处理 
				{
					char b=op.top();
					op.pop();
					s.push(b);
				}
				op.pop();//把左括号释放掉 
			}
		} 
	}
	while(!op.empty())
	{
		char c=op.top();
		op.pop();
		s.push(c);
	} 
	while(!s.empty())
	{
		char d=s.top();
		s.pop();
		op.push(d);
	}
	cout<<"该表达式的后缀表达式为:";
	stack<double>st;
	while(!op.empty())
	{
		char m=op.top();
		cout<<m;
		if(m>='0'&&m<='9')
		st.push(m-'0');
		else
		{
			double op2=st.top();
			st.pop();
			double op1=st.top();
			st.pop();
			double temp=value(op1,op2,m);
			st.push(temp);
		}
		op.pop();
	}
	cout<<endl<<"该表达式的值为:";
	cout<<fixed<<setprecision(2)<<st.top()<<endl;
}

double h_to_s(string str)
{
	stack<double>st;
	for(int i=0;i<str.length();i++)
	{
		if(str[i]>='0'&&str[i]<='9')
		st.push(str[i]-'0');
		else
		{
			double op2=st.top();
			st.pop();
			double op1=st.top();
			st.pop();
			double temp=value(op1,op2,str[i]);
			st.push(temp);
		}
	}
	return st.top();
}
int main()
{
	int op;
	cout<<"请输入操作选项,1表示输入中缀表达式,2表示输入后缀表达式。"<<endl;
	while(cin>>op)
	{
		if(op==1)
		{
			string str;
			cin>>str;
			z_to_h(str);
			cout<<"请输入操作选项,1表示输入中缀表达式,2表示输入后缀表达式。"<<endl;
		}
		else if(op==2)
		{
			string str;
			cin>>str;
			cout<<"该表达式的值为:";
			cout<<fixed<<setprecision(2)<<h_to_s(str)<<endl;
			cout<<"请输入操作选项,1表示输入中缀表达式,2表示输入后缀表达式。"<<endl;
		}
		else
		break;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值