简体   繁体   中英

Getting coefficients from a string

I have a project to write a program that receives a polynomial string from the user up to the 5th power (ex. x^3+6x^2+9x+24) and prints out all the real and imaginary roots. The coefficients should be stored in a dynamic array.

The problem is getting these coefficients from the string. One of the coefficients can be a 0 (ex. 2x^2-18) so I can't store the coefficients from left to right by using an increment, because in this case a=2, b=-18, and c has no value, which is wrong.

Another problem is if the coefficient is 1, because in this case nothing will be written beside the x for the program to read (ex. x^2-x+14). Another problem is if the user adds a space, several, or none (ex. x ^3 +4x^ 2- 12 x + 1 3).

I have been thinking of pseudocode for a long time now, but nothing is coming to mind. I thought of detecting numbers from left to right and reading numbers and stopping at x, but the first and second problems occur. I thought of finding each x and then checking the numbers before it, but the second problem occurs, and also I don't know how big the number the user inputs.

Here is another Regex that you can use to get your coefficients after deleting whitespace characters:

(\d*)(x?\^?)(\d*)

It uses groups (indicated by the brackets). Every match has 3 groups:

  1. Your coefficient
  2. x^n, x or nothing
  3. The exponent

If (1) is null (eg does not exist), it means your coefficient is 1 .

If (2) and (3) are null , you have the last single number without x .

If only (3) is null , you have a single x without ^n .

You can try some examples on online regex sites like this one , where you can see the results on the right.

There are many tutorials online how to use Regex with C++.

You should normalize your input string, for example, remove all space then parse coefficients.

Let see my example. Please change it for your case.

#include <iostream>
#include <regex>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>

int main(int argc, char *argv[]) {

    std::string input {argv[1]};
    input.erase(remove_if(input.begin(), input.end(), isspace), input.end());
    std::cout << input  << std::endl;
    std::vector<int> coeffs;
    std::regex poly_regex(R"(\s*\+?\-?\s*\d*\s*x*\^*\s*\d*)");
    auto coeff_begin = std::sregex_iterator(input.begin(), input.end(), poly_regex);
    auto coeff_end = std::sregex_iterator();
    for (std::sregex_iterator i = coeff_begin; i != coeff_end; ++i) {
        std::smatch match = *i;
        std::string match_str = match.str();
        // std::cout << " " << match_str << "\n";
        std::size_t plus_pos = match_str.find('+');
        std::size_t minus_pos = match_str.find('-');
        std::size_t x_pos = match_str.find('x');
        if (x_pos == std::string::npos) {
            std::cout << match_str.substr(plus_pos + 1) << std::endl;            
        } else if (x_pos == 0) {
            std::cout << 1 << std::endl;
        } else if (minus_pos != std::string::npos) {
            if (x_pos - minus_pos == 1) std::cout << -1 << std::endl;
            else std::cout << match_str.substr(minus_pos, x_pos - minus_pos) << std::endl;            
        }
        else {
            std::cout << match_str.substr(plus_pos + 1, x_pos - plus_pos - 1) << std::endl;            
        }
    }
    for (auto i: coeffs) std::cout << i << " ";
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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