| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 916 人关注过本帖
标题:表达式求值程序求助!!!!!!!!
只看楼主 加入收藏
xzbxzb
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-10-19
收藏
 问题点数:0 回复次数:0 
表达式求值程序求助!!!!!!!!
程序不知那里错了,各位大侠请帮帮忙!
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>

using namespace std;

double read_and_evaluate(istream& ins);
void evaluate_stack_tops(stack<double>& numbers,stack<char>& operations);
int main()
{
    double answer;
    cout <<"type a arithmetic expression:" <<endl;
    answer = read_and_evaluate(cin);
    cout << "the answer is:" << answer <<endl;

    return EXIT_SUCCESS;
}
double read_and_evaluate (istream& ins)
{
    const char DECIMAL ='.';
    const char RIGHT_PARENTHESIS =')';
    stack<double> numbers;
    stack<char> operations;
    double number;
    char symbol;
    while (ins && ins.peek( ) != '\n')
    {
        if(isdigit(ins.peek( )) || (ins.peek( ) == DECIMAL))
        {
            ins >> number;
            numbers.push(number);
        }
        else if (strchr("+-*",ins.peek( ) != NULL))
        {
            ins >> symbol;
            operations.push(symbol);
        }
        else if (ins.peek( ) == RIGHT_PARENTHESIS)
        {
            ins.ignore( );
            evaluate_stack_tops(numbers,operations);
        }
        else
            ins.ignore();
    }
    return numbers.top( );
}

void evaluate_stack_tops(stack<double>& numbers,stack<char>& operations)
{
    double operand1, operand2;

    operand2 = numbers.top( );
    numbers.pop( );
    operand1 = numbers.top( );
    numbers.pop( );
    switch (operations.top( ))
    {
    case '+': numbers.push(operand1 + operand2);
        break;
    case '-': numbers.push(operand1 - operand2);
        break;
    case '*': numbers.push(operand1 * operand2);
        break;
    case '/': numbers.push(operand1 / operand2);
        break;
    }
    operations.pop( );
}
搜索更多相关主题的帖子: 求值 表达 
2008-10-19 13:16
快速回复:表达式求值程序求助!!!!!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022162 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved