您的位置:首页 > 其它

九度:1019<模拟>

2014-03-02 23:09 302 查看
http://ac.jobdu.com/problem.php?pid=1019



// 浙大2006年机试
// 九度:1019
// 题目:简单计算器
//
// 第一次做错了,想了好久才发现自己的错误。
// 第一次中,每次都是读取字符,也就意味着,
// 程序只能计算个位数的数据。测试自己写的
// case 也都正确,所以哎呀!
//
// 第二次更新了策略,第一次读取一个浮点数,
// 接下来每次读取一个字符,如果是空格在读
// 取一个字符和一个浮点数,空格说明不是结
// 束。
//
//

#include <stdio.h>
#include <cctype>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

#define SIZE 205

using namespace std;

int main(){
#ifdef ONLINE_JUDGE
#else
freopen("E:\\in.txt", "r" , stdin);
#endif

double x;
while(scanf("%lf", &x) != EOF && x )
{
double qn[SIZE];
char qs[SIZE];
int pos = 0;
char c;

qn[pos] = x;
while(scanf("%c", &c) && c != '\n')
{
if(c == ' ')
scanf("%c %lf", &c, &x);
if(c == '+' || c == '-')
{
qs[pos++] = c;
qn[pos] = x;
}
else if(c == '*')
{
qn[pos]  *= x;
}
else
{
qn[pos] /= x;
}
}//read a line

int len=pos;
double ans=qn[0];

for(int i=0; i<len; i++)
{
c = qs[i];
if(c == '+')
{
ans += qn[i+1];
}else{
ans -= qn[i+1];
}
}
printf("%.2lf\n", ans);

}//while
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: