提问者:小点点

在HP-35 C++程序中实现科学功能


我在尝试实现一些科学函数时遇到了一些问题,例如,?(pi)和函数chs(改变X的符号),1/X(recip),log(十进制对数),ln(自然对数),ex(exp),√X(sqrt),sin,cos,tan(以及它们的逆arcsin,arccos和arctan)和xy(pow)。

函数名应接受大写或小写。 使用cmath库中的函数来完成实际工作。 预定义常数M_PI包含pi的值。

下面是我的代码,我试图在其中实现科学函数,以便:

#include <string>
#include <iostream>
#include <sstream>
#include <cmath>

using namespace std;

class HPStack
{
    double stack[100]; //stack to store operands
    int top; //defines topmost position of stack
//    const double PI = 3.1415926535897;
    double sin(double x);

public:
    HPStack() //constructor to initialize top
    {
        top = -1; //defines empty stack
    }

    void push(double data) //adds operand to stack
    {
        top++;
        stack[top] = data;
    }

    double pop() //removes operand to stack
    {
        return stack[top--];
    }

    double peek() //gets topmost value of stack
    {
        return stack[top];
    }
};

int main(int argc, char *argv[])
{
    HPStack stack;
    string line;

    while (getline(cin, line)) {
        stringstream expression(line);
        string token;
        while (expression >> token) {
            if (isdigit(token[0])) {
                stack.push(atof(token.data()));
            }
            else if (token == "sin")
            {
                double sin(double x);
                stack.push(sin(0));
            }
            else if (token == "-") { // Addition Operation
                double x = stack.pop();
                double y = stack.pop();
                stack.push(y - x);
            }
            else if (token == "+") { // Subtraction Operation
                double x = stack.pop();
                double y = stack.pop();
                stack.push(y + x);
            }
            else if (token == "/") { // Multiplication Operation
                double x = stack.pop();
                double y = stack.pop();
                stack.push(y / x);
            }
            else if (token == "*") { // Division Operation
                double x = stack.pop();
                double y = stack.pop();
                stack.push(y * x);
            }
            else if (token == "M_PI") { // Division Operation
                double pi = M_PI;
                stack.push(M_PI);
            }
            else if (token == "sqrt") { // Division Operation
                double sqrt;
                stack.push(sqrt);
            }
            else if (token == "I") { // Division Operation
                double arcsin;
                stack.push(arcsin);
            }
            else if (token == "arccos") { // Division Operation
                double arccos;
                stack.push(arccos);
            }
            else if (token == "arctan") { // Division Operation
                double arctan;
                stack.push(arctan);
            }
            else if (token == "exp") { // Division Operation
                double exp;
                stack.push(exp);
            }
            else if (token == "pow") { // Division Operation
                double pow;
                stack.push(pow);
            }
            else if (token == "log") { // Division Operation
                double log;
                stack.push(log);
            }

        }
        cout << stack.peek()<<"\n";
    }

    return 0;
}

有人能在这方面给我一些帮助吗? 我已经开始了,你可以看到,我如何有双圆周率,但我没有很多运气在做这件事。


共1个答案

匿名用户

当输入函数名时,您应该进行计算,而不是仅仅推送不确定值。

示例:

            else if (token == "sqrt") {
                double x = stack.pop();
                stack.push(sqrt(x));
            }

其他功能也可以这样做。

要同时支持大写和小写字符,很容易将读取的令牌转换为小写,并与用小写字符编写的关键字进行比较。 可以这样进行转换:

for (size_t i = 0; i < token.size(); i++) {
    token[i] = tolower(static_cast<unsigned char>(token[i]));
}

添加#include以使用ToLower()

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(hp-35|c++|程序|中|科学|功能)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?