我如何解释这个帕斯卡部分C++
How do I interpret this Pascal part to C++?
如何用C++编写此记录和过程?如何将代码转换为类和方法而不是直接翻译?
type
complex=record
im,re:real;
end;
procedure NumberMultiplication(a:complex; var b:complex; k:byte);
begin
b.re:=a.re*k;
b.im:=a.im*k;
end;
C++ 标准库已经提供了此功能。
包括标题<complex>
,使用类型std::complex<double>
,用普通*
表示乘法。
例:
#include <complex>
using Complex = std::complex<double>;
using Byte = unsigned char;
auto operator*( Byte const b, Complex const& c )
-> Complex
{ return Complex( b )*c; }
#include <iostream>
using namespace std;
auto main() -> int
{
Byte const b = 42;
Complex const c = {2, 3}; // 2 + 3*i
cout << b*c << endl;
}
在C++中,您可以:
struct complex {
double im, re;
};
然后,该过程可以编写为:
void NumberMultiplication(complex a, complex &b, byte k)
{
b.re = a.re * k;
b.im = a.im * k;
}
这是Pascal实现的直接翻译;根据你的总体目标,也许有更好的方法来用C++编写它。
您可以定义自己的结构
struct complex
{
double re, im;
}
void NumberMultiplication(const complex a; complex &b, short int k)
{
b.re = a.re * k;
b.im = a.im * k;
}
(为什么是字节 -- 短整数类型???(。
我更喜欢更好的方法,那就是
#include <complex>
std::complex<double> a, b;
short int k;
b = a * k;
(仍然对短 int 感到困惑(。您可以在 http://www.cplusplus.com/reference/complex/中检查复杂类引用
如前所述,最直接的翻译是将 Pascal record
替换为 struct
。请注意,byte
的C++等价物是unsigned char
,因此最直接的翻译是:
struct complex
{
double re, im;
};
void NumberMultiplication(complex a, complex& b, unsigned char k)
{
b.re = a.re*k;
b.im = a.im * k;
}
但是,这不是最好的翻译。首先要提到的是,与 Pascal 不同,C++允许返回结构化类型(我认为也有 Pascal 方言允许这样做(。因此,您可以将NumberMultiplication
更改为返回complex
的函数:
complex NumberMultiplication(complex a, unsigned char k)
{
complex b { a.re*k, a.im * k };
return b;
}
请注意,在上面的代码中,我使用了直接在局部变量定义中初始化C++ struct
的可能性。但是,通过给complex
一个构造函数,你可以做得更好:
struct complex
{
double re, im;
complex(double real, double imag = 0); // constructor declaration
};
// inline is a hint for the compiler to optimize by inserting that code
// into calling code
inline complex::complex(double real, double imag):
re(real),
im(imag) // these initialize the members
{
// here you could write other code to be executed on initialization
// (not needed in this case)
}
complex NumberMultiplication(complex a, unsigned char k)
{
return complex(a.re * k, a.im * k);
}
请注意,构造函数的第二个参数(double imag
(有一个默认参数(= 0
(;这使得可以省略虚部;此外,由于现在构造函数可以只用一个参数调用(并且没有标记为explicit
(,这也使隐式转换为双精度,也就是说,你现在可以写
complex z = NumberMultiplication(3.0, 7);
这意味着与
complex z = NumberMultiplication(complex(3.0, 0.0), 7);
下一个改进是使用 C++ 的运算符重载:没有必要为我们通常使用乘法运算符 *
的运算发明一个花哨的函数名称;我们可以通过简单地使用特殊函数名称operator*
来为我们自己的类型定义该乘法运算符:
complex operator*(complex a, unsigned char k)
{
return complex(a.re * k, a.im * k);
}
有了这个,您现在可以简单地编写例如。
complex x(1,2), y;
unsigned char c = 42;
y = x*c;
当然,您有时也可能想写y=c*x
,并希望它也起作用。幸运的是,由于函数重载,您可以做到这一点:只需添加
complex operator*(unsigned char k, complex a)
{
return complex(a.re * k, a.im * k);
}
编译器将从类型中选择要调用的正确函数。
接下来,我们可以通过隐藏成员变量来改进 complex
结构,使其成为一个合适的类。我们提供访问器函数来读取实部和虚部。所以我们现在得到:
class complex
{
public:
complex(double real, double imag=0);
double real();
double imag();
};
inline complex::complex(doube real, double imag):
re(real),
im(imag)
{
};
inline double complex::real()
{
return re;
}
inline double complex::imag()
{
return im;
}
complex operator*(complex a, unsigned char k)
{
return complex(a.real() * k, a.imag() * k);
}
complex operator*(unsigned char k, complex a)
{
return complex(a.real() * k, a.imag() * k);
}
现在复数当然需要比乘法更多的运算;但幸运的是,你可以省去所有的工作,因为有人已经为你做了。在C++中存在一个标头complex
其中包含复数的完整实现。因此你可以写
#include <complex>
并在 Pascal 代码使用自定义complex
类型的任何地方使用类型 std::complex<double>
。
我会这样解释它
strcut complex
{
double im;
double re;
};
complex operator *( const complex &c, int k )
{
return { k * c.im, k * c.re };
}
complex operator *( int k, const complex &c )
{
return { k * c.im, k * c.re };
}
也就是说,操作将是可交换的。你可以写
complex c = { 1, 2 };
c = 3 * c;
c = c * 3;
或者,如果您的编译器不支持初始值设定项列表,则
strcut complex
{
double im;
double re;
};
complex operator *( const complex &c, int k )
{
complex tmp = { k * c.im, k * c.re };
return tmp;
}
complex operator *( int k, const complex &c )
{
complex tmp = { k * c.im, k * c.re };
return tmp;
}
- C++:试图通过组合和帕斯卡三角形来理解constexpr
- 如何在C++的帕斯卡三角形中打印曲棍球棒的元素?
- CUDA - 统一内存(至少是帕斯卡)
- 找到一个数字平方和是质数(帕斯卡或C++)的数字
- 有人知道一个模仿帕斯卡"range array"的类吗?
- 找到第1500行帕斯卡三角形中的每个数字
- 需要使用帕斯卡三角形 (x+y) 打印二项式系数
- 帕斯卡三角形程序 C++ 上的间距
- 为什么我在计算帕斯卡三角形元素时在递归 C 程序中出现堆栈溢出错误
- C++:如何生成帕斯卡三角形的'nth'线?
- 从C++打电话给斯卡拉帕克
- 帕斯卡三角形|李特码错误答案
- 帕斯卡三角形实现
- 帕斯卡的模数
- 以C++为中心帕斯卡三角形输出
- 帕斯卡的读密钥是否有等效项;在 C 还是C++?
- 我如何解释这个帕斯卡部分C++
- 为什么这个帕斯卡三角形程序不起作用?
- c++生成帕斯卡三角形,输出错误
- C++:帕斯卡三角形 - 奇怪的结果