简体   繁体   English

C ++如何让用户输入方程式求值

[英]c++ how to have a user input equation to be evaluated

My program is meant to ask a user to input an equation. 我的程序旨在要求用户输入方程式。 Then find the maximum over an interval given by the user. 然后在用户给定的时间间隔内找到最大值。 When I compile my program, the output I get is: 当我编译程序时,得到的输出是:

Please complete the equation to be evaluated f(x)= 
Please enter the first number of the interval to be checked: 
Please enter the last number of the interval to be checked: 
Please enter the desired initial step size: 
sh: PAUSE: command not found

with the last line repeating many times. 最后一行重复很多次。 I think the problem here has something to do with having the user input the equation to be tested. 我认为这里的问题与用户输入要测试的方程式有关。 However, I'm unsure of how to fix this. 但是,我不确定如何解决此问题。

Here's my code 这是我的代码

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
    int a, b, delta, fx, x, y;
    int max = 0;

    cout <<"Please complete the equation to be evaluated f(x)= " << endl;
    cin >> fx;
    cout <<"Please enter the first number of the interval to be checked: " << endl;
    cin >> a;
    cout << "Please enter the last number of the interval to be checked: " << endl;
    cin >> b;
    cout << "Please enter the desired initial step size: " << endl;
    cin >> delta;

    for(x = a; x <= b; x = x+delta)                                
    {
        y = fx;     
        if (y > max)  
        { 
            max = y;  
            cout <<"The maximum over the interval from " << a <<"to " << b <<"is " << delta;
        }
        else
        {
            delta= delta/2;
        }
        if (delta <  pow( 10, -6))
        {
            system ("PAUSE");
        }
    }      

    return 0;
}

Simply don't use system("pause"); 只是不要使用system("pause"); in the if statement and you'll lose that error: "sh: PAUSE: command not found" . 在if语句中,您将丢失该错误: “ sh:PAUSE:command not found” Place it right before the end of the main. 将其放置在主体末端之前。

system("pause");
return 0;

F(x) shouldn't be an integer variable, it should be a string variable. F(x)不应为整数变量,而应为字符串变量。 That way, the user can enter operators as characters instead of the compiler thinking they should be numbers. 这样,用户可以将运算符作为字符输入,而不是编译器认为它们应该是数字。 You would then have to process the string to determine the equation; 然后,您必须处理字符串以确定方程式; this would require some thought, and possibly a more advanced data structure such as a binary tree. 这将需要一些思考,并且可能需要更高级的数据结构,例如二叉树。

As pointed out by others, the form of f(x) could be an issue with the above code. 正如其他人指出的那样, f(x)的形式可能是上述代码的问题。

Consider to redesign what to achieve for your program. 考虑重新设计您的程序要实现的目标。 One possibility is to narrow down the f(x) as polynomial function so that you can avoid parsing general algebraic equation, in this case you can ask: how many degree of the polynomial ? 一种可能性是缩小f(x)的多项式函数,以便避免解析一般的代数方程式,在这种情况下,您可以问:多项式的阶数是多少? upon this, it is followed by input the coefficient value for each factor in the polynomial equation. 在此之后,随后输入多项式方程中每个因子的系数值。

This way, you can still use integer (or double - better) in the program. 这样,您仍然可以在程序中使用整数(或更好的double值 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM