鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > c++ > >

C++复数运算示例 C++ complex numbers

来源:互联网 作者:佚名 时间:2016-07-01 16:54
本例实现C++复数运算(体现出使用complexlibrary的便捷) ThisisanimplementationofC++complexnumbers. 无 #pragma once#includeiostream#includestdlib.h#includecomplexusing namespace std;template typename Tclass ComplexAlgs{public:ComplexAlgs(compl
本例实现C++复数运算(体现出使用complex library的便捷)

This is an implementation of C++ complex numbers. <无>
#pragma once
#include<iostream>
#include<stdlib.h>
#include<complex>

using namespace std;

template <typename T>
class ComplexAlgs
{
public:
	ComplexAlgs(complex<T> complexNum1, complex<T> complexNum2)
	{
		c1 = complexNum1;
		c2 = complexNum2;
	}//end constructor

	complex<T> getSum()
	{
		return c1 + c2;
	}//end funcion.

	complex<T> getSubstraction()
	{
		return c1 - c2;
	}//end funcion.

	complex<T> getMuliplication()
	{
		return c1 * c2;
	}//end funcion.

	complex<T> getDivision()
	{
		return c1 / c2;
	}//end funcion.

	~ComplexAlgs()
	{
		cout << "The class is destroyed!" << endl;
		system("pause");
	}//end constructor

private:
	//Use for represent 2 complex numbers:
	complex<T> c1;
	complex<T> c2;
}; //end class
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<complex>
#include "ComplexAlgs.h"

using namespace std;

int main()
{
	//Define 2 parts of 2 complex numbers correspondingly.
	double real1, img1, real2, img2;
	cout << "Input real part of num1: ";
	cin >> real1;
	cout << "Input imagninary part of num1: ";
	cin >> img1;
	cout << "Input real part of num2: ";
	cin >> real2;
	cout << "Input imaginary part of num2: ";
	cin >> img2;

	complex<double> c1{ real1, img1 };
	complex<double> c2{ real2, img2 };

	ComplexAlgs<double> calculateComplex(c1, c2);
	cout << "c1 + c2: " << calculateComplex.getSum() << endl;
	cout << "c1 - c2: " << calculateComplex.getSubstraction() << endl;
	cout << "c1 * c2: " << calculateComplex.getMuliplication() << endl;
	cout << "c1 / c2: " << calculateComplex.getDivision() << endl;
	
	//calculateComplex.~ComplexAlgs();
	//cout << "c1 + c2: " << calculateComplex.getSum() << endl;
	system("pause");
	return 0;
}
网友评论
<