C ++:从.txt文件中读取复数并将其存储到复数数组中

时间:2019-09-17 03:43:01

标签: c++ arrays

我在弄清楚如何处理实数和虚数这个问题时遇到了麻烦。

这是我到目前为止的代码:


#include <iostream>
#include <fstream>
#include <string>


using namespace std;

class Complex {

    public:

        Complex();
        Complex(double realNum);
        Complex(double realNum, double imagNum);

        //Complex(double real = 0.0, double imaginary = 0.0); This avoids the 3 above?

        Complex(const Complex& obj);



    private:

        double real;
        double imaginary;



};


int main () {

    Complex *complexArray;
    complexArray = new Complex[8];

    ifstream myfile("complex.txt");

    string line;
    int i = 0;

    if (myfile.is_open()) {

        while (! myfile.eof()) {

            getline(myfile, line);
            complexArray[i] = line;
            i++

        };

        myfile.close();


    };

    else {

        cout << "Error. Could not find/open file." ;
    }



    return 0;

};


Complex::Complex(const Complex& obj) {

    real = obj.real;
    imaginary = obj.imaginary;

};



Complex::Complex () {

    real = 0;
    imaginary = 0;

};



Complex::Complex (double realNum) {

    real = realNum;
    imaginary = 0;

};



Complex::Complex (double realNum, double imagNum) {

    real = realNum;
    imaginary = imagNum;

};

所以我意识到我无法读复数并将其直接存储到我的数组中...

我在想也许我应该这样做?

  1. 读入数字并将其作为字符串存储在字符串数组中。
  2. 执行循环以遍历字符串数组并进行循环...

    1. 检查(如何执行此操作?),以确保它是格式正确的复数,以避免 “假线嗨!”
    2. real = myStringArray [i] .at(0) imaginary = myStringArray [i] .at(1和2个位置...以某种方式执行此操作)

无论如何,我只是困惑如何解决这个问题。

谢谢!

3 个答案:

答案 0 :(得分:0)

我建议使用正则表达式。这是我的方法:

#include <iostream>
#include <fstream>
#include <string>
#include <regex>

int main() {
    std::regex rgx("^(?=[iI.\\d+-])([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?(?![iI.\\d]))?([+-]?(?:(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?)?)?[iI]$");
    std::smatch match;

    std::ifstream infile("complex.txt");
    std::string line;

    while (std::getline(infile, line)) {
        if (std::regex_search(line, match, rgx)) {
            std::cout << "text: " << match[0] << ", real: " << match[1] << ", imaginary: " << match[2] << std::endl;
        }
    }
}

正则表达式是对https://stackoverflow.com/a/50428157/1994239的编辑,适用于您提供的示例文本。

答案 1 :(得分:0)

您可以使用正则表达式来匹配仅包含虚数的行。以下是解决您问题的完整程序-

Process Explorer

答案 2 :(得分:0)

使用正则表达式进行验证。

#include <iostream>
#include <fstream>
#include <string>
#include <regex>

using namespace std;

class Complex
{

public:

    Complex();
    Complex(double realNum);
    Complex(double realNum, double imagNum);

    //Complex(double real = 0.0, double imaginary = 0.0); This avoids the 3 above?

    Complex(const Complex& obj);

    friend ostream & operator << (ostream &out, const Complex &c)
    {
        cout<<c.real<<" + "<<c.imaginary<<"i";
        return out;
    }


private:

    double real;
    double imaginary;



};


int main ()
{

    Complex *complexArray;
    complexArray = new Complex[8];

    ifstream myfile("complex.txt");

    string line;
    int i = 0;

    if (myfile.is_open())
    {

        while (! myfile.eof())
        {

            getline(myfile, line);
            const std::string& str = line;
            //use regex to identify if this line represents a complex number or not
            double real = 0.0, imag = 0.0;
            std::regex realRegex("^(-)?\\s*(\\d+(\\.\\d+)?)$");
            std::regex imagRegex("^(-)?\\s*(\\d+(\\.\\d+)?)i$");
            std::regex bothRegex("^(-)?\\s*(\\d+(\\.\\d+)?)\\s*([-+])\\s*(\\d+(\\.\\d+)?)i$");
            std::smatch match;
            if (std::regex_match(str.begin(), str.end(), match, realRegex))
            {
                real = std::atof(match[2].str().c_str());
                if (match[1].matched)
                {
                    real = -real;
                }
            }
            else if (std::regex_match(str.begin(), str.end(), match, imagRegex))
            {
                imag = std::atof(match[2].str().c_str());
                if (match[1].matched)
                {
                    imag = -imag;
                }
            }
            else if (std::regex_match(str.begin(), str.end(), match, bothRegex))
            {
                real = std::atof(match[2].str().c_str());
                imag = std::atof(match[5].str().c_str());
                if (match[1].matched)
                {
                    real = -real;
                }
                if (match[4].str() == "-")
                {
                    imag = -imag;
                }
            }
            else
            {
                continue;
            }

            Complex c(real, imag);
            cout<<c<<endl;
            complexArray[i]=c;
            i++;

        }

        myfile.close();


    }
    else
    {

        cout << "Error. Could not find/open file." ;
    }



    return 0;

};


Complex::Complex(const Complex& obj)
{

    real = obj.real;
    imaginary = obj.imaginary;

};



Complex::Complex ()
{

    real = 0;
    imaginary = 0;

};



Complex::Complex (double realNum)
{

    real = realNum;
    imaginary = 0;

};



Complex::Complex (double realNum, double imagNum)
{

    real = realNum;
    imaginary = imagNum;

};