How to have C++ solve the equation for an input value?

前端 未结 4 1549
Happy的楠姐
Happy的楠姐 2021-01-29 08:06

I am stuck trying to figure out a way using C++ to solve a situation where the user inputs a value using cin and then have the computer solve for a way to get the value of cin,

相关标签:
4条回答
  • 2021-01-29 08:13

    This case can be solved trivially by interval arithmetic. C++ code that solves your "sum of two interval-constrained variables problem" is given below.

    int min_x = 30, max_x = 50;
    int min_y = 60, max_y = 90;
    
    // solutions exist in this interval
    int min_z = min_x + min_y, max_z = max_x + max_y;
    
    cin >> input;
    
    // solutions possible?
    if (input >= min_z && input <= max_z)
    {
        // determine solution interval for x (y is dependent)
        cout
        << "Solution:\n"
        << "x in [" << min(max(  input - max_y  , min_x),max_x)
        << ";"      << min(max(  input - min_y  , min_x),max_x) << "], "
        << "y = "   << input << " - x" << endl;
    }
    else
    {
        cout << "No solution." << endl;
    }
    

    Computers are "basically stupid" and if they do smart things it is the software. Using a general purpose programming language like C++ requires you (or at least the libraries you eventually use) to be very specific on how exactly to solve a problem based on the simple arithmetic means of the bare computer.

    Although the programming language won't magically and somehow do things for you, algorithms exist to solve many mathematical standard problems such as e.g. systems of equations. Numerical Recipes in C++ covers a variety of algorithms and their C++ implementations.

    0 讨论(0)
  • 2021-01-29 08:15

    It looks like you have a 'mathematical' problem here: a couple of values constrained by equations, and you want 'the computer' to find all possible values that fit into the constraints (equations). Am I right?

    While some computer programs can certainly do that, the C++ language is not designed for this purpose. The role of the C++ is to give you a way of giving instructions to the processor, like "store this value in memory" or "add these two numbers". But there is no way of saying "solve this mathematical problem".

    What you need is some equation solver. But I am not familiar with any. There are tools like Matlab or Mathematica. But I do not think they are free.

    0 讨论(0)
  • 2021-01-29 08:16

    If you want to solve the math problem algorithmically, here is a brute force idea in pseudocode:

    Input a number.
    for each value x between 30 and 50
      for each value y between 60 and 90
        if x+y equals the number
          print x and y
    

    Now you can take a good book or tutorial and code in C++. Look for the for and if keywords (algorithmic concepts of iteration and selection) in your teaching material. Have fun!

    0 讨论(0)
  • 2021-01-29 08:38

    It looks like you think that C++ is going to solve equations for you. It won't. C++ is an imperative style language that is based around the concept of you telling it exactly what to do.

    You will have to figure out how to solve for x and y so that you can make an algorithm. This algorithm is then what you make your program from.

    There exists other languages in which you can in a sense describe what you want and have the compiler or runtime figure out how to get it for you. C++ is not one of them.

    Different ways to solve your particular problem would be to set up an equation system and solving that. Or do brute force approach and iterate through the values of x and y in order to find out which values match.

    0 讨论(0)
提交回复
热议问题