火星计算器

// marks_calc.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int changeOp(char op)
{
switch(op)
{
case '&':return 1;
case '$':return 2;
case '#':return 3;
case '@':return 4;
}
}


int calc(char op,int y,int x)
{
switch(op)
{
/*case 1: return (5*num1+20)*(4*num2+1);//(x+y+1)*(y-9)+(x+7)/(y-8);
case 2: return (3*num1+11)*(2*num2+7);//(x+1)*(2*x+3)*(y-1)*(2*y-3);
case 3: return (2*num1+3)*(3*num2+4); //(2*x+5)*(3*y+60);
case 4: return (num1+1)*(num2+2); //(x-1)*(y+1);
*/
case 1: return (x+y+1)*(y-9)+(x+7)/(y-8);
case 2: return (x+1)*(2*x+3)*(y-1)*(2*y-3);
case 3: return (2*x+5)*(3*y+60);
case 4: return (x-1)*(y+1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//string str;
//stack<int> numstack;
//stack<int> opstack;
char op;
int num=0;
char in[1024];
//cin >> str;
//getline(cin,str);
//cout << "str:"<<str<<endl;
while(gets(in)!=NULL)
{
string str(in);
stack<int> numstack;
stack<int> opstack;
//str=in;
num=0;
int i;
for(i=0;i<str.size();i++)
{
if(str[i]>='0'&&str[i]<='9')
{
num=num*10+str[i]-'0';
}
else
{
numstack.push(num);
num=0;
op=str[i];
if(numstack.size()==1)
{
opstack.push(changeOp(op));
continue;
}
int curop=changeOp(op);
int preop=opstack.top();
if(preop>=curop)
{
int num1=numstack.top();
numstack.pop();
int num2=numstack.top();
numstack.pop();
numstack.push(calc(opstack.top(),num1,num2));\
opstack.pop();
}
opstack.push(curop);
}
}
numstack.push(num);
while(!opstack.empty())
{
int num1= numstack.top();
numstack.pop();
int num2= numstack.top();
numstack.pop();
numstack.push(calc(opstack.top(),num1,num2));
opstack.pop();
}
cout << numstack.top()<<endl;
numstack.pop();
}
//system("pause");


return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值