九度oj 题目1019:简单计算器
- 题目描述:
-
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
- 输入:
-
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
- 输出:
-
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
- 样例输入:
-
1 + 2
4 + 2 * 5 - 7 / 11
0
- 样例输出:
-
3.00
13.36 这道题采用两个栈,一个存储符号,一个存储数字
比较运算符的优先级以确定计算的顺序
一开始准备采用巧妙的办法来读取数字,代码如下#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#define MAX 202
using namespace std; int cmp(char a, char b) {
if(b == '+' || b == '-') {
if(a == '+' || a == '-') {
return ;
}
else if(a == '*' || a == '/') {
return -;
}
if(a == '$') {
return -;
}
}
else if(b == '*' || b == '/') {
return ;
}
} double cal(double a, double b, char c) {
if(c == '+') {
return a + b;
}
else if(c == '-') {
return a - b;
}
else if(c == '*') {
return a * b;
}
else if(c == '/') {
return a / b;
}
}
stack<double> num;
stack<char> sign;
char temp[MAX];
char calTemp[MAX]; int main(int argc, char const *argv[])
{
gets(temp);
int numTemp;
int signTemp;
sign.push('$'); while(strcmp(temp,"") != ) {
double ans = ;
int ptr = ;
int tlen = strlen(temp);
char signTemp;
int flag = ;
while(ptr < tlen){
sscanf(&temp[ptr],"%s",calTemp);
if(temp[ptr] >= '' && temp[ptr] <= '') {
sscanf(&temp[ptr],"%d",&numTemp);
if(flag == ) {
num.push(numTemp);
}
else if(flag == ) {
num.push(numTemp);
double a = num.top();
num.pop();
double b = num.top();
num.pop();
char calSign = sign.top();
sign.pop();
ans = cal(b, a, calSign);
num.push(ans);
flag = ;
}
else if(flag == ) {
double a = num.top();
num.pop();
double b = num.top();
num.pop();
char calSign1 = sign.top();
sign.pop();
char calSign2 = sign.top();
sign.pop();
ans = cal(b, a, calSign2);
num.push(ans);
sign.push(calSign1);
num.push(numTemp);
flag = ;
}
}
else {
if(sign.size() == && cmp(sign.top(),calTemp[]) == -) {
sign.push(calTemp[]);
}
else if(cmp(sign.top(),calTemp[]) == ){
sign.push(calTemp[]);
flag = ;
}
else if(cmp(sign.top(),calTemp[]) == ){
sign.push(calTemp[]);
flag = ;
} }
int clen = strlen(calTemp);
ptr = ptr + clen + ;
}
while(num.size() >= ) {
double a = num.top();
num.pop();
double b;
if(num.size() != ) {
b = num.top();
num.pop();
}
else {
b = ;
}
char calSign = sign.top();
sign.pop();
ans = cal(b, a, calSign);
num.push(ans);
if(num.size() == ) {
break;
}
}
while(num.size() != ) {
num.pop();
}
printf("%.2lf\n", ans); gets(temp);
} return ;
}但提交后run time error ,不明白是哪里出错了
后来修改为如下代码
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#define MAX 202 int cmp(char aq, char bq) {
if(bq == '+' || bq == '-') {
if(aq == '$') {
return ;
}
return -;
}
else if(bq == '*' || bq == '/') {
if(aq == '*' || aq == '/') {
return -;
}
if(aq == '$') {
return ;
}
return ;
}
} double cal(double a, double b, char c) {
if(c == '+') {
return a + b;
}
else if(c == '-') {
return a - b;
}
else if(c == '*') {
return a * b;
}
else if(c == '/') {
return a / b;
}
}
std::stack<double> num;
std::stack<char> sign;
char temp[MAX];
char calTemp[MAX]; int main(int argc, char const *argv[])
{
gets(temp);
double numTemp;
int signTemp;
sign.push('$');
while(strcmp(temp,"") != ) {
int numTemp = ;
int flag = ;
bool isNum = false; int i = ;
while(i < strlen(temp)) {
while(temp[i] >= '' && temp[i] <= '') {
numTemp = numTemp * + temp[i] - '';
i++;
isNum = true;
} if(isNum == true) {
if(flag == ) {
num.push(numTemp);
}
else if(flag == ) {
double a = num.top();
num.pop();
char calSign = sign.top();
sign.pop();
double ans = cal(a, numTemp, calSign);
num.push(ans);
flag = ;
}
else if(flag == -) {
double a = num.top();
num.pop();
double b = num.top();
num.pop();
char calSign1 = sign.top();
sign.pop();
char calSign2 = sign.top();
sign.pop();
double ans = cal(b, a, calSign2);
num.push(ans);
sign.push(calSign1);
num.push(numTemp);
flag = ;
}
numTemp = ;
isNum = false;
}
else if(temp[i] != ' '){
flag = cmp(sign.top(),temp[i]);
sign.push(temp[i]);
isNum = false;
}
i++;
}
double a = num.top();
num.pop();
double b = num.top();
num.pop();
char calSign = sign.top();
sign.pop();
double res = cal(b, a, calSign); printf("%.2lf\n", res);
gets(temp);
} return ;
}一开始把 '==' 打成了 '=',结果出现了匪夷所思的错误,经过多次排查,终于发现错误。以后要注意!!!!!!!!!!!!
-----9-17更新
事实上,如果只有加减乘除的话,还可以采用下面的办法,用一个数组去存储加减的数,遇到乘除法先算出来再存到数组中,最后直接算这个数组中数的和,代码如下
#include <cstdio>
#include <cstring> char str[];
double dealed[]; int main(int argc, char const *argv[])
{
while(gets(str) != && strcmp(str,"") != ) {
int len = strlen(str);
int isP = ;
int i = ;
int p = ;
int isM = ;//0 null ,1 * ,2 /
double last = ;
while(i < len) {
double tmp = ;
while(i < len && str[i] >= '' && str[i] <= '') {
tmp = *tmp + str[i] - '';
i++;
}
while(i < len && str[i] == ' ') {
i++;
}
if(isM == ) {
tmp = tmp * last;
isM = ;
}
else if(isM == ) {
tmp = last /tmp;
isM = ;
}
if(i < len) {
if(str[i] == '+') {
dealed[p++] = tmp * isP;
isP = ;
}
else if(str[i] == '-') {
dealed[p++] = tmp * isP;
isP = -;
}
else if(str[i] == '*') {
last = tmp;
isM = ;
}
else if(str[i] == '/') {
last = tmp;
isM = ;
}
i++;
while(i < len && str[i] == ' ') {
i++;
}
}
else {
dealed[p++] = tmp * isP;
} }
double ans = ;
for(int i = ; i < p; i++) {
ans = ans + dealed[i];
}
printf("%.2lf\n",ans);
}
return ;
}
九度oj 题目1019:简单计算器的更多相关文章
- 九度oj题目1019:简单计算器
题目1019:简单计算器 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6346 解决:2334 题目描述: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达 ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
随机推荐
- 解决java.lang.NoClassDefFoundError: javax/xml/rpc/service错误的方法
最近在做WebService项目,本地测试没有问题,打算部署到服务器上,但是部署后,访问时出现了如下图1的错误: 图1 图1报的是没有找到定义的类的错误.刷新页面有又出现了另外“新”的错误: 图2 根 ...
- WebService学习之旅(六)使用Apache Axis2实现WebService客户端调用
上节介绍了如何使用Axis2 发布一个WebService,Axis2除了为我们编写WebService应用带来了便利,也同样简化的客户端调用的过程,本节在上节的基础上使用Axis2自带的工具生成客户 ...
- WebService学习之旅(三)JAX-WS与Spring整合发布WebService
Spring本身就提供了对JAX-WS的支持,有兴趣的读者可以研究下Spring的Spring-WS项目,项目地址: http://docs.spring.io/spring-ws/sites/1.5 ...
- Linux中配置系统参数
[root@localhost ~]# vim /etc/security/limits.conf root soft nofile 65535root hard nofile 65535* soft ...
- 【春节版】年度精品 XP,32/64位Win7,32/64位Win8,32/64位Win10系统
本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...
- HDU 4044 GeoDefense (树形DP,混合经典)
题意: 给一棵n个节点的树,点1为敌方基地,叶子结点都为我方阵地.我们可以在每个结点安放炸弹,每点至多放一个,每个结点有ki种炸弹可选,且每种炸弹有一个花费和一个攻击力(1点攻击力使敌人掉1点hp). ...
- python打飞机pro版
# -*- coding: utf-8 -*- import pygame from sys import exit import random pygame.init() screen = pyga ...
- Entity Framework插入数据报错:Validation failed for one or more entities
www.111cn.net 编辑:lanve 来源:转载 今天在处理Entity Framework插入数据库时,报错: Validation failed for one or more entit ...
- Codeforces Round #318 (Div. 2) A Bear and Elections (优先队列模拟,水题)
优先队列模拟一下就好. #include<bits/stdc++.h> using namespace std; priority_queue<int>q; int main( ...
- (五)使用Docker镜像(上)
1. 获取镜像 # 获取镜像 docker pull image:tag // 不使用tag 默认下载latest标签的镜像,即最新的镜像. 2. 查看镜像信息 # 查看镜像信息docker imag ...