我如何解释这个帕斯卡部分C++

How do I interpret this Pascal part to C++?

本文关键字:帕斯卡部 C++ 解释 何解释      更新时间:2023-10-16

如何用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;     
}