Home  >  Article  >  Backend Development  >  C++ program to initialize and print a complex number

C++ program to initialize and print a complex number

王林
王林forward
2023-09-01 09:41:071403browse

C++ program to initialize and print a complex number

Complex numbers are a very basic concept in modern science. This concept was first introduced into mathematics. Introduced in the early 17th century. The plural form is a ib, where a and b is a real number. a is called the real part and ib is called the imaginary part of a is a real number. a is called the real part and ib is called the imaginary part of a plural. In C, there is a class representing complex numbers, which is Complex class. Complex classes in C can represent and manipulate various operations About plural numbers. Let's look at how to represent, initialize and display complex numbers number.

Use class constructor for initialization

To use the plural class, we must import the plural library, which can be done by importing or Use the #include statement to include. Declare and initialize a Complex objects are described below. In the first example we use the constructor for assignment Use the real and imaginary parts of the complex number as initialization values.

grammar

double value1 = <double value>;
double value2 = <double value>;
complex <double> cno(value1, value2);

algorithm

  • Accepts input in two numeric variables.

  • Pass these two variables to the constructor of the complex number.

  • Display plural numbers.

The translation of

Example

is:

Example

#include <iostream>
#include <complex>
using namespace std;

//displays the complex number supplied
void display(complex <double> c){
   cout << "The complex number is: ";
   cout << real(c) << '+' << imag(c) << 'i' << endl;
}

//initializing the complex number
complex<double> solve( double real, double img ){
   complex<double> cno(real, img);
   return cno;
}
int main(){

   //the real and the imaginary values are represented as double values
   double v1 = 10;
   double v2 = 7;

   //creating and displaying the complex number
   display(solve(v1, v2));
   return 0;
}

Output

The complex number is: 10+7i

Any numeric data type can be used to replace the current

of a complex variable Type, double.

Use assignment operator for initialization

We can also use the assignment operator to assign the values ​​of the real and imaginary parts to a complex number number. However, to do this we must provide a number of type "a ib" and "b" are both numerical values. If the number is an integer, place zeros in the spaces after the decimal point. Decimal point must be used when writing real numbers Part "a". For example, 10 must be written as 10.0.

grammar

//the real and imaginary parts have to be assigned as it is
complex <double> cno = 15.0 + 6i;

algorithm

  • Get a new plural object.

  • Assign a value to the object using 'a.ib' notation.

  • Display plural values.

The translation of

Example

is:

Example

#include <iostream>
#include <complex>
using namespace std;

//displays the complex number supplied
void display(complex <double> c){
   cout << "The complex number is: ";
   cout << real(c) << '+' << imag(c) << 'i' << endl;
}
int main(){
   
   //initializing a complex number object
   complex <double> cno = 15. + 6i;
   
   //displaying the complex number
   display(cno);
   return 0;
}

Output

The complex number is: 15+6i

Display plural numbers

Using the "real()" and "imag()" functions, you can get the real and imaginary parts of complex integers.

are displayed individually. The "real()" function displays the real part of a complex number, The "imag()" function displays the imaginary part of a complex number. Here is an example

sample.

grammar

//displaying in the a + ib format
complex<double> c;
cout << real(c) << '+' << imag(c) << 'i' << endl;

algorithm

  • Get a new plural object.

  • Assign a value to an object using 'a.ib' notation.

  • Display plural values.

The translation of

Example

is:

Example

#include <iostream>
#include <complex>
using namespace std;
//displays the complex number supplied
void display(complex <double> c){
   cout << "The complex number is: ";
   cout << real(c) << '+' << imag(c) << 'i' << endl;
}
//initializing the complex number
complex<double> solve( double real, double img ){
   complex<double> cno(real, img);
   return cno;
}
int main(){
//the real and the imaginary values are represented as double values
   double v1 = 3;
   double v2 = 4;
//creating and displaying the complex number
   display(solve(v1, v2));
   return 0;
}

Output

The complex number is: 3+4i

in conclusion

Complex numbers are needed in a wide range of applications in many different programs.

scientific disciplines. The C plural class, included in header file , provides Interface representing plural numbers. Plural classes support all operations Complex numbers, including addition, subtraction, multiplication, conjugate, norm and Lots of more operations. As we have already discussed in this article, it is not too difficult Convert regular numeric values ​​to complex numbers. We must note that although When displaying complex numbers, we need to pay attention to the real and imaginary parts number, otherwise multiple errors may occur in the program.

The above is the detailed content of C++ program to initialize and print a complex number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete