题目描述:
    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入:
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过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. ubuntu中使用eclipse开发android,logcat显示问题

    〜/工作区/ .metadata / .plugins / org.eclipse.core.runtime / .settings / com.android.ide.eclipse.ddms.pr ...

  2. windows的cmd和git bash的常用命令

    windows下使用git bash,使用的事linux下的命令,整理常用命令如下: windows下的命令 linux下的命令 命令的含义 cd e:\xx cd /e/xx 切换到xx目录 cd ...

  3. 并查集+思维——Destroying Array

    一.题目描述(题目链接) 给定一个序列,按指定的顺序逐一删掉,求连续子序列和的最大值.例如序列1 3 2 5,按3 4 1 2的顺序删除,即依次删除第3个.第4个.第1个.第2个,答案为5 4 3 0 ...

  4. python_108_格式化字符串format函数

    #通过关键字映射 print('I am {name},age {age}'.format(name='qiqi齐',age=18))#I am qiqi齐,age 18 dictory={'name ...

  5. Windows MinGW 64-bit boost 踩坑

    >g++ -Wall -shared -g -DBUILD_DLL main.cpp -ID:\gcc\boost\include\boost-1_69 -LD:\gcc\boost\lib - ...

  6. numpy中tile函数

    tile函数位于python模块numpy.lib.shape_base中,他的功能是重复某个数组. 函数的形式是tile(A,reps) 函数参数说明中提到A和reps都是array_like的,什 ...

  7. Dubbo中的监控和管理

    一.Dubbo中的监控 1.原理 原理:服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心. 2.搭建监控服务 3.修改配置文件 修改注册中心的地址: 注意:这个 ...

  8. Bootstrap 原始按钮

    Bootstrap 原始按钮 <!DOCTYPE html><html><head><meta http-equiv="Content-Type&q ...

  9. tkinter学习-选择按钮

    阅读目录 Checkbutton Radiobutton LabelFrame checkbutton : 说明:多选框控件,用于在程序中提供多项选择框,但是处理“多选一”的问题,还是交给 Radio ...

  10. docker系列之基础命令-2

    一.查看本地镜像 docker images 二.需要基础的镜像两种方式 1.docker pull centos 可以直接拉起镜像 2.直接用xshell导入就行,docker load -i  加 ...