题目描述:
    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入:
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过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:简单计算器的更多相关文章

  1. 九度oj题目1019:简单计算器

    题目1019:简单计算器 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6346 解决:2334 题目描述:     读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达 ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  5. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  6. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  7. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  8. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  9. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

随机推荐

  1. PLSQL连接Oracle64监听和服务的配置!

    前言: 这里不会涉及到太多关于版本问题的解决,只是简单提一下基本的监听和服务配置问题的解决,让你可以快速的用PLSQL连接上你自己创建的Oracle数据库(这里示例数据库名为ORCL); 版本问题: ...

  2. this详解,对执行上下文说 Yes

    this 指向多变,很多隐蔽的 bug 都缘于它.与此同时,this 强大灵活,如果能熟练驾驭,就会写出更简洁.优雅的代码. 社区上对于 this 的讲解虽然不少,但缺乏统一梳理. this 相关知识 ...

  3. IE兼容只读模式

    表单input具有只读模式属性,一般来说,一般的浏览器都支持该属性,即readyonly,但IE不支持,只能寻找其兼容性. 第一种:unselectable='on' <input id=&qu ...

  4. AndroidStudio进行Build时出现DexArchiveMergerException异常的解决办法

    今天在AndroidStudio中导入了一个项目,编译的时候没有什么问题,但是在执行Rebuild Project 和 Build APK(s)时报错了,提示: Error:Execution fai ...

  5. [Rational Rose 2007]解决启动报”解决无法启动此程序因为丢失suite objects.dll“的问题

    问题根源1:不是丢失suite objects.dll文件,而是环境变量配置错误或无配置 假如安装目录如:C:\Program Files\Rational 需要配置环境变量的路径为:C:\Progr ...

  6. FusionCharts3.2.1 参数的详细说明和功能特性

    功能特性animation                    是否动画显示数据,默认为1(True)showNames                 是否显示横向坐标轴(x轴)标签名称rotat ...

  7. 连接MongoDB数据库的配置说明

  8. flex常用属性

    <1>align-items: 垂直方向的对齐方式 align-items: stretch(拉伸,布满父容器) | center(垂直居中) | flex-start(上对齐) | fl ...

  9. cdlinux

    xset q xset s 6000 xset -dpms ntpdate time.nist.gov date

  10. 浏览器window产生的缓存九种解决办法

    浏览器缓存(Browser Caching)是浏览器端保存数据用于快速读取或避免重复资源请求的优化机制,有效的缓存使用可以避免重复的网络请求和浏览器快速地读取本地数据,整体上加速网页展示给用户.浏览器 ...