九度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编程基础-面向对象(中)
本章承接Java编程基础-面向对象(上)一文. 一.static关键字 在java中,定义了一个static关键字,它用于修饰类的成员,如成员变量.成员方法以及代码块等,被static修饰的成员具备一 ...
- BZOJ3073: [Pa2011]Journeys(线段树优化建图 Dijkstra)
题意 \(n\)个点的无向图,构造\(m\)次边,求\(p\)到任意点的最短路. 每次给出\(a, b, c, d\) 对于任意\((x_{a \leqslant x \leqslant b}, y_ ...
- 查找算法(顺序查找、二分法查找、二叉树查找、hash查找)
查找功能是数据处理的一个基本功能.数据查找并不复杂,但是如何实现数据又快又好地查找呢?前人在实践中积累的一些方法,值得我们好好学些一下.我们假定查找的数据唯一存在,数组中没有重复的数据存在. (1)顺 ...
- uvm_config_db——半个全局变量
UVM中的配置机制uvm_config_db,uvm_config_db机制用于在UVM平台间传递参数.它们通常是成对出现的,set 寄信,而get函数是收信.config 机制大大提高了UVM的验证 ...
- 【R语言进行数据挖掘】回归分析
1.线性回归 线性回归就是使用下面的预测函数预测未来观测量: 其中,x1,x2,...,xk都是预测变量(影响预测的因素),y是需要预测的目标变量(被预测变量). 线性回归模型的数据来源于澳大利亚的C ...
- github入门之分支操作--5
1.显示分一览表 2.创建.切换分支 2.1.切换到feature-A分支并进行提交 2.1.1.执行下面的命令,创建名为feature-A的分支 实际上,执行以命令也能收到同样的效果,但是我习惯使用 ...
- 动手使用ABAP Channel开发一些小工具,提升日常工作效率
今天的故事要从ABAP小游戏说起. 中国的ABAP从业者们手头或多或少都搜集了一些ABAP小游戏,比如下面这些. 消灭星星: 扫雷: 来自我的朋友刘梦,公众号"SAP干货铺"里的俄 ...
- 贴一发STL源码
int my_lower_bound(int size, long long key){ int first = 0, middle; int half, len; len = si ...
- nuxt 初接触
对于nuxt服务端渲染让人动心的是不会再想vue一样去定义无数的路由了这一点是挺爽的!!! 先直接晒张图 在api这块增加了一个fetch方法 它会在组件每次加载前被调用(即在服务端或切换至目标路 ...
- python之道06
1,使⽤循环打印以结果: * *** ***** ******* ********* 答案: 方法一: for i in range(10): if i % 2 == 1: print(i*'*') ...