算法训练 表达式计算
2017-03-11 11:02
806 查看
问题描述
输入一个只包含加减乖除和括号的合法表达式,求表达式的值。其中除表示整除。
输入格式
输入一行,包含一个表达式。
输出格式
输出这个表达式的值。
样例输入
1-2+3*(4-5)
样例输出
-4
数据规模和约定
表达式长度不超过100,表达式运算合法且运算过程都在int内进行。
解答代码
#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
#include<stack>
#define N 128
using namespace std;
stack<int> operator_num;
stack<char> operator_char;
char inputStr
;
char getNumber
;
void getResType1();
void getResType2();
int main()
{
int i,index=0;
int number;
char str
="=";
gets(inputStr);
strcat(inputStr,str);
operator_char.push('(');
for(i=0;inputStr[i]!='\0';i++)
{
//获取操作数
if(inputStr[i]>='0' && inputStr[i]<='9')
{
getNumber[index++]=inputStr[i];
continue;
}
getNumber[index]='\0';
if(getNumber[0]!='\0')
{
number=atoi(getNumber);
getNumber[0]='\0';
operator_num.push(number);
}
index=0;
switch(inputStr[i])
{
case '+':
getResType1();
operator_char.push('+');
break;
case '-':
getResType1();
operator_char.push('-');
break;
case '(':
//getResType1();
operator_char.push('(');
break;
case ')':
getResType1();
operator_char.pop();
break;
case '*':
getResType2();
operator_char.push('*');
break;
case '/':
getResType2();
operator_char.push('/');
break;
case '=':
getResType1();
operator_char.pop();
break;
}
}
cout<<operator_num.top()<<endl;
return 0;
}
void getResType1()
{
char ch=operator_char.top();
int num1,num2;
while(ch!='(')
{
num1=operator_num.top();
operator_num.pop();
num2=operator_num.top();
operator_num.pop();
switch (ch)
{
case '+':
operator_num.push(num2+=num1);
break;
case '-':
operator_num.push(num2-=num1);
break;
case '*':
operator_num.push(num2*=num1);
break;
case '/':
operator_num.push(num2/=num1);
break;
}
operator_char.pop();
ch=operator_char.top();
}
}
void getResType2()
{
char ch=operator_char.top();
int num1,num2;
while(ch=='*' || ch=='/')
{
num1=operator_num.top();
operator_num.pop();
num2=operator_num.top();
operator_num.pop();
switch(ch)
{
case '*':
operator_num.push(num2*=num1);
break;
case '/':
operator_num.push(num2/=num1);
break;
}
operator_char.pop();
ch=operator_char.top();
}
}
输入一个只包含加减乖除和括号的合法表达式,求表达式的值。其中除表示整除。
输入格式
输入一行,包含一个表达式。
输出格式
输出这个表达式的值。
样例输入
1-2+3*(4-5)
样例输出
-4
数据规模和约定
表达式长度不超过100,表达式运算合法且运算过程都在int内进行。
解答代码
#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
#include<stack>
#define N 128
using namespace std;
stack<int> operator_num;
stack<char> operator_char;
char inputStr
;
char getNumber
;
void getResType1();
void getResType2();
int main()
{
int i,index=0;
int number;
char str
="=";
gets(inputStr);
strcat(inputStr,str);
operator_char.push('(');
for(i=0;inputStr[i]!='\0';i++)
{
//获取操作数
if(inputStr[i]>='0' && inputStr[i]<='9')
{
getNumber[index++]=inputStr[i];
continue;
}
getNumber[index]='\0';
if(getNumber[0]!='\0')
{
number=atoi(getNumber);
getNumber[0]='\0';
operator_num.push(number);
}
index=0;
switch(inputStr[i])
{
case '+':
getResType1();
operator_char.push('+');
break;
case '-':
getResType1();
operator_char.push('-');
break;
case '(':
//getResType1();
operator_char.push('(');
break;
case ')':
getResType1();
operator_char.pop();
break;
case '*':
getResType2();
operator_char.push('*');
break;
case '/':
getResType2();
operator_char.push('/');
break;
case '=':
getResType1();
operator_char.pop();
break;
}
}
cout<<operator_num.top()<<endl;
return 0;
}
void getResType1()
{
char ch=operator_char.top();
int num1,num2;
while(ch!='(')
{
num1=operator_num.top();
operator_num.pop();
num2=operator_num.top();
operator_num.pop();
switch (ch)
{
case '+':
operator_num.push(num2+=num1);
break;
case '-':
operator_num.push(num2-=num1);
break;
case '*':
operator_num.push(num2*=num1);
break;
case '/':
operator_num.push(num2/=num1);
break;
}
operator_char.pop();
ch=operator_char.top();
}
}
void getResType2()
{
char ch=operator_char.top();
int num1,num2;
while(ch=='*' || ch=='/')
{
num1=operator_num.top();
operator_num.pop();
num2=operator_num.top();
operator_num.pop();
switch(ch)
{
case '*':
operator_num.push(num2*=num1);
break;
case '/':
operator_num.push(num2/=num1);
break;
}
operator_char.pop();
ch=operator_char.top();
}
}