c语言逆波兰计算器程序,逆波兰_计算器

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#include

#include

#include

using namespace std;

vector > f(string s)//��stringת��Ϊvector

{

vector > v;

char ch[100];

string str;

int i=0, j;

while (s[i] != '\0')

{

if (s[i] == '(')

{

str = s[i++] + '\0';

v.push_back(make_pair (1, str));

}

else if (s[i] == ')')

{

str = s[i++] + '\0';

v.push_back(make_pair < int, string>(2, str));

}

else if (s[i] == '+' || s[i] == '-')

{

str = s[i++] + '\0';

v.push_back(make_pair < int, string>(3, str));

}

else if (s[i] == '*' || s[i] == '/')

{

str = s[i++] + '\0';

v.push_back(make_pair < int, string>(4, str));

}

else if(s[i] == ' ')

{

i++;

continue;

}

else if (s[i] <= '9' && s[i] >= '0' || s[i] == '.')

{

bool dian;

if(s[i] == '.')

dian = true;

j = 1;

ch[0] = s[i++];

int k = 1;

while(s[i] <= '9' && s[i] >= '0' || s[i] == '.')

{

if(s[i] == '.' && dian)

{

cout<

while(1);

}

ch[j] = s[i];

++j;

++i;

}

ch[j] = '\0';

v.push_back(make_pair < int, string >(0, ch));

dian = false;

}

}

return v;

}

bool bijiao(int n, string a)

{

int m;

if(a == "+" || a == "-")

m=3;

else if(a == "*" || a == "/")

m=4;

else if(a == "#")

m=-1;

return n>m?true:false;

}

stack f_nbl(vector > v)

{

stack s, f;

f.push("#");

for(vector >::iterator xi = v.begin(); xi != v.end(); ++xi)

{

if(xi->first == 0)

s.push(xi->second);

else

{

if(xi->first == 1)

f.push(xi->second);

else if(xi->first == 2)

{

while(f.top() != "(")

{

s.push(f.top());

f.pop();

}

f.pop();

}

else if(f.top() == "(")

f.push(xi->second);

else

{

if(f.top() == "#")

{

f.push(xi->second);

}

else

{

if(bijiao(xi->first, f.top()))

f.push(xi->second);

else

{

while(!bijiao(xi->first, f.top()))

{

s.push(f.top());

f.pop();

}

f.push(xi->second);

}

}

}

}

}

while(f.top() != "#")

{

s.push(f.top());

f.pop();

}

f.pop();

while(!s.empty())

{

f.push(s.top());

s.pop();

}

return f;

}

double f_js(stack s)

{

double a, b;

string ch;

stack p;

while(!s.empty())

{

while(s.top() != "+" && s.top() != "-" && s.top() != "*" && s.top() != "/")

{

//p.push((int)(s.top() - '0'));

p.push(atof(s.top().c_str()));

s.pop();

}

ch = s.top();

s.pop();

b = p.top();

p.pop();

a = p.top();

p.pop();

switch(ch[0])

{

case '+' : p.push(a + b); break;

case '-' : p.push(a - b); break;

case '*' : p.push(a * b); break;

case '/' : if(b==0.0)

{

cout<

while(1);

}

else

p.push(a / b);

break;

}

}

return p.top();

}

int main()

{

string s;

cout<

cin>>s;

cout<

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值