如何存储复数数组?

huangapple go评论57阅读模式
英文:

How can I store an array of complex numbers?

问题

我想编写一个代码,用于处理一系列复数并在其中搜索一个数字。但我不知道如何编写形成该序列的函数。到目前为止,我尝试了以下内容:

class complex {    
private:
    int real;
    int image; 
    int n;
    int *a;
    
public:
    complex(float l, float k) : real(l), image(k) {}
        
    float set(float l, float k)
    {
        cout << "enter size of array: ";
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            cout << "enter real part: ";
            cin >> l;
            cout << "enter image part: ";
            cin >> k;
            *(a+i) = complex(l, k);
        }
    }
};

请注意,这段代码中存在一些问题,需要进一步修复。

英文:

I want to write code that takes a sequence of complex numbers and searches for a number in it. But I don't know how to write a function to form the sequence. This is what I've tried so far:

class complex {	
private:
	int real;
	int image; 
	int n;
	int *a;
	
public:
	complex(float l, float k) : real(l), image(k) {}
		
	float set(float l, float k)
	{
		cout &lt;&lt; &quot;enter size of array: &quot;;
		cin &gt;&gt; n;
		for (int i = 0; i &lt; n; i++)
		{
			cout &lt;&lt; &quot;enter real part: &quot;;
			cin &gt;&gt; l;
			cout &lt;&lt; &quot;enter image part: &quot;;
			cin &gt;&gt; k;
			*(a+i) = complex(l, k);
		}
	}
};

答案1

得分: 2

你可以使用标准库中的复数类型,并创建一个向量(数组)来存储它们。使用 std::complex_literals,甚至可以在浮点数后加上 i 以进行计算。

对于你的用例,你也可以使用 std::complex<int>(我之前遗漏了)。

#include <complex>
#include <iostream>
#include <vector>

int main()
{
    using namespace std::complex_literals;
    std::vector<std::complex<double>> values{ {1.0,1.0}, {2.0,2.0}, {3.0,3.0} };

    for (const auto value : values)
    {
        std::cout << 3.0i * value << "\n";
    }

    return 0;
}
英文:

You can use complex numbers from the standard library and make a vector (array) from them. With std::complex_literals you can even postfix floating point numbers with an i to do calculations with them.
For your use case you can also use std::complex&lt;int&gt; (I missed that)

#include &lt;complex&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

int main()
{
	using namespace std::complex_literals;
	std::vector&lt;std::complex&lt;double&gt;&gt; values{ {1.0,1.0}, {2.0,2.0}, {3.0,3.0} };

	for (const auto value : values)
	{
		std::cout &lt;&lt; 3.0i * value &lt;&lt; &quot;\n&quot;;
	}

	return 0;
}

答案2

得分: 0

因为你不能使用库(这很愚蠢),我建议创建一个结构体来保存数据。

struct complexNum {             
  int real;        
  int imaginary;   
};

另一个问题是a是未定义的,为什么不直接使用a[i]呢?而且为什么不创建一个典型的数组呢?你的函数也不需要参数或返回类型。

可以这样做:

class complex {  
    private:
        struct complexNum {             
            int real;        
            int imaginary;   
        };
        int n;
        complexNum* a;
    
    public:        
        void set()
        {
            cout << "输入数组大小:";
            cin >> n;
            a = new complexNum[n];
            for (int i = 0; i < n; i++)
            {
                complexNum num;
                cout << "输入实部:";
                cin >> num.real;
                cout << "输入虚部:";
                cin >> num.imaginary;
                a[i] = num;
            }
        }
};
英文:

Because you arent allowed to use the library (which is stupid), what I would suggest is creating a struct to save the data

struct complexNum {             
  int real;        
  int imaginary;   
};

The other problem is that a is undefined as is and why not just use a[i]. Also instead why not create a typical array? You also dont need arguments or a return type for your function.

Something like:

class complex{  
    private:
        struct complexNum {             
            int real;        
            int imaginary;   
        };
        int n;
        complexNum* a;
    
    public:        
        void set()
        {
            cout&lt;&lt;&quot;enter size of array: &quot;;
            cin&gt;&gt;n;
            a = new complexNum[n];
            for (int i=0; i&lt;n; i++)
            {
                complexNum num;
                cout&lt;&lt;&quot;enter real part: &quot;;
                cin&gt;&gt;num.real;
                cout&lt;&lt;&quot;enter image part: &quot;;
                cin&gt;&gt;num.imaginary;
                a[i]=num;
            }
        }
};

huangapple
  • 本文由 发表于 2023年6月12日 23:25:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定