英文:
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 << "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);
}
}
};
答案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<int>
(I missed that)
#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;
}
答案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<<"enter size of array: ";
cin>>n;
a = new complexNum[n];
for (int i=0; i<n; i++)
{
complexNum num;
cout<<"enter real part: ";
cin>>num.real;
cout<<"enter image part: ";
cin>>num.imaginary;
a[i]=num;
}
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论