您的位置:首页 > 其它

FOJ--1339--Calculator--解题报告

2009-08-05 22:07 204 查看
Today the teacher give out a tedious homework to loy concerning the integer calculation practice. He is not contend with such boring homework, so instead, loy decides to write a program to help him finish the homework.

The expression is consists of only integers,+,-,*,/,( and ). The brackets is guaranteed to be exactly matched. And the operator / returns the integer part of the division, i.e. 7/2=3. It is guaranteed that the divisor is not zero.

Input

The first line of the input contains a single integer t, representing the number of testcase. The following t lines contains expressions one per line. There is no extra spaces in the expression. And the total length of the expression will not exceed 200.

Output

For each case, output a integer representing the result of the expression.

Sample Input

2
1+3/2
1-(2+(3*(4/5)))

Sample Output

2
-1

分析:用2个栈来存放,一个存放符号,一个存放数字

代码如下:

//一边插入一边运算,只要遇到前面有优先级高的就要先算前面的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stack>
using namespace std;
bool Compare(char char_1,char char_2)  //比价比较两个字符的优先级别
{
int value1,value2;
switch(char_1)
{
case '+':
value1=1;
break;
case '-':
value1=1;
break;
case '*':
value1=2;
break;
case '/':
value1=2;
break;
}
switch (char_2)
{
case '+':
value2=1;
break;
case '-':
value2=1;
break;
case '*':
value2=2;
break;
case '/':
value2=2;
break;
case '(':
value2=3;;
break;
case '#':
value2=10;
break;
}
return value1<=value2;
}
int yun_suan(int a,int b,char hao)   //加减乘除运算
{
int c;
if(hao=='+')
c=a+b;
else if(hao=='-')
c=b-a;
else if(hao=='*')
c=a*b;
else
c=b/a;
return c;
}
int main()
{
int n,i,len_str,len_ver;
int a,b,c,elem;
char str[201],ver[201],hao;
stack<int> int_n;
stack<char> char_n;
scanf("%d",&n);
char_n.push('#');
while (n--)
{
scanf("%s",&str);
len_str=strlen(str);
len_ver=0;
for(i=0;i<len_str;i++)
{
if(str[i]>='0' && str[i]<='9')   //为数字的话数组中间的数组ver[]就要存这个数字,直到遇到符号
{
ver[len_ver++]=str[i];
if(i==len_str-1)
{
ver[len_ver]='/0';
elem=atoi(ver);
int_n.push(elem);
}
}
else if(str[i]=='(')
char_n.push(str[i]);
else if(str[i]=='+' || str[i]=='-' || str[i]=='*' || str[i]=='/')
{

if(len_ver!=0)
{
ver[len_ver]='/0';
elem=atoi(ver);
int_n.push(elem);
}
while(Compare(str[i],char_n.top()) && char_n.top()!='('  && char_n.top()!='#')  //优先的级别的要先算,要用while()
{
hao=char_n.top();
char_n.pop();
a=int_n.top();
int_n.pop();
b=int_n.top();
int_n.pop();
c=yun_suan(a,b,hao);
int_n.push(c);
}
char_n.push(str[i]);
len_ver=0;
}
else if(str[i]==')')
{
ver[len_ver]='/0';
if(len_ver!=0)
{
elem=atoi(ver);
int_n.push(elem);
}
while(1)
{
hao=char_n.top();
char_n.pop();
if(hao=='(')
break;
a=int_n.top();
int_n.pop();
b=int_n.top();
int_n.pop();
c=yun_suan(a,b,hao);
int_n.push(c);
}
len_ver=0;
}
}
while (char_n.top()!='#')
{
hao=char_n.top();
char_n.pop();
a=int_n.top();
int_n.pop();
b=int_n.top();
int_n.pop();
c=yun_suan(a,b,hao);
int_n.push(c);
}
c=int_n.top();
int_n.pop();
printf("%d/n",c);
}
return 0;
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: