【问题标题】:Multiply complex object with real and imaginary parts by a multiplier of type 'double' in C++在 C++ 中将具有实部和虚部的复数对象乘以“double”类型的乘数
【发布时间】:2019-11-08 12:02:46
【问题描述】:

我试图将复杂数组的每个元素乘以一个乘数,以应用傅立叶变换。我正在将汉宁窗滤波器应用于波函数的复杂文件。

我正在使用 CodeBlocks 使用 C++,我不断得到 -->

error: invalid types 'double[int]' for array subscript

我的代码在这里:

#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <cmath>
#define PI 3.14159265359
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[1000];
    ifstream myfile("myfile.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++;
            }
            // Hanning Window
            for (int i = 0; i < 1000; i++) {
                double multiplier = 0.5 * (1 - cos(2*PI*i/999));
                complexArray[i] = multiplier[i] * complexArray[i];
                }
        }

        myfile.close();
     }else {
        cout << "Error. Could not find/open file." ;
     }
     cout << complexArray << endl;
     return 0;
};

我想将复杂对象的每个元素乘以乘法数组中的每个元素。我不确定执行此操作的正确方法。

【问题讨论】:

标签: c++ class operator-overloading operators complex-data-types


【解决方案1】:

对于此循环中的初学者

        for (int i = 0; i < 1000; i++) {
            double multiplier = 0.5 * (1 - cos(2*PI*i/999));
            complexArray[i] = multiplier[i] * complexArray[i];
            }

变量multiplier 被声明为double 类型的标量对象。所以你需要写

complexArray[i] = multiplier * complexArray[i];

而不是

complexArray[i] = multiplier[i] * complexArray[i];

您还需要为您的班级重载operator *

例如

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 const Complex operator *( double, const Complex & );
    private:
       double real;
       double imaginary;
};

//...

const Complex operator *( double value, const Complex &c )
{
    return { value * c.real, value * c.imaginary };
}

也是while循环中的条件

    while (! myfile.eof()) {

        getline(myfile, line);
        //...

代替

    while ( getline(myfile, line) ) {
        //...

还有这个循环

        for (int i = 0; i < 1000; i++) {
            double multiplier = 0.5 * (1 - cos(2*PI*i/999));
            complexArray[i] = multiplier[i] * complexArray[i];
            }

应该在while循环之外。例如

        for ( int j = 0; j < i; j++) {
            double multiplier = 0.5 * (1 - cos(2*PI*i/999));
            complexArray[j] = multiplier * complexArray[j];
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多