簡體   English   中英

C ++:從.txt文件中讀取復數並將其存儲到復數數組中

[英]C++: Read in complex numbers from a .txt file and store into an array of Complex numbers

我在弄清楚如何處理實數和虛數的問題時遇到了麻煩。

這是我到目前為止的代碼:


#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. 檢查(如何執行此操作?)以確保它是格式正確的復數,以避免出現“ fake Line hi!”。
    2. 實= myStringArray [i] .at(0)虛= myStringArray [i] .at(1和2位...以某種方式執行此操作)

無論如何,我只是困惑如何解決這個問題。

謝謝!

我建議使用正則表達式。 這是我的方法:

#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的編輯,適用於您提供的示例文本。

您可以使用正則表達式來匹配僅包含虛數的行。 以下是解決您的問題的完整程序-

#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);
    private:
       double real;
       double imaginary;
 };

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;
}

int main () {
    Complex *complexArray = new Complex[8];
    ifstream myfile("test.txt");
    /* this will match the complex numbers of the form - 123.123 + 14.1244  or 123 - 1343.193 and so on, 
      basically assumes that both the real and the imaginary parts are both doubles*/
    regex reg_obj("^[ ]*([-]?\\d+(\\.\\d+)?)\\s*([+-])\\s*(\\d+(\\.\\d+)?)i");  
    smatch sm;
    string line;
    int i = 0;
    double real, imag;
    if (myfile.is_open()) {
        while (! myfile.eof()) {

            getline(myfile, line);
            if(regex_search(line, sm, reg_obj)){
                real = stod(sm[1]);       // this ([-]?\\d+(\\.\\d+)?) is group 1 and will match the real part of the number
                imag = stod(sm[4]);       // second group (\\d+(\\.\\d+)?)i is group 4 which matches the imaginary part of the complex number without matching + or - which are taken care of separately because there could be space between the +|- symbol and the imaginary part
                if(sm[3]=="-") imag = -imag;
                complexArray[i] = Complex(real, imag);
                i++;
            }
        }

        myfile.close();
     }else {
        cout << "Error. Could not find/open file." ;
     }
     return 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;

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM