http://ac.jobdu.com/problem.php?pid=1101
-
题目描述:
-
对于一个不存在括号的表达式进行计算
-
输入:
-
存在多种数据,每组数据一行,表达式不存在空格
-
输出:
-
输出结果
-
样例输入:
-
6/2+3+3*4
-
样例输出:
-
18
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
#define MAX 2000
float stack[MAX];
int tail = 0;
int main(){
int a;
//ifstream cin("data.txt");
while (scanf_s("%d",&a)!=EOF)
{
tail = 0;
stack[tail] = (float)a;
char algo = getchar(); //algo表示运算符或者换行符
while (algo != '\n' && (algo =='+'||algo =='-'||algo=='*'||algo =='/'))//这儿要加上判断是否是运算符符号的条件
{
scanf_s("%d", &a);
if (algo == '+')
stack[++tail] = (float)a;
else if (algo == '-')
stack[++tail] = -(float)a;
else if (algo == '*')
stack[tail] = stack[tail] * (float)a;
else if (algo == '/')
stack[tail] = stack[tail] / (float)a;
algo = getchar();
}
int result = 0;
for (int i = 0; i <= tail; i++)
result += stack[i];
printf_s("%d\n", result);
}
system("pause");
return 0;
}