Datentyp complex

Bastih84

Mitglied
Hat jemand eine gute Erklärunf für den Datentyp complex in c++ Oder kann mir jemand so weiterhelfen Ich soll ein Programm schreiben das complexe Zahlen tauscht, int und double Zahlen funktionieren schon


#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <fstream>
#include <complex>

using namespace std;
std::complex;

void tausch(int& a, int& b)
{
cout << endl << "int-Funktion" << endl;
int h=a;
a=b;
b=h;
}


void tausch(double& c, double& d)
{
cout << endl << "double-Funktion" << endl;
double h=c;
c=d;
d=h;
}



void main()
{
int a=2, b=4;
double c=12.3, d=23.4;

tausch(a,b);
cout << endl << a << endl << b << endl << endl;


tausch(c,d);
cout << endl << c << endl << d << endl << endl;



}
 
Du könntest das mit einem Funktions-Template machen, in etwa so:
Code:
template<class T> void Tausch(T& a, T&b)
{
	T h = a;
	a = b;
	b = h;
}
danach könnte dein Code so aussehen:
Code:
typedef complex<double> compl;

int main(void)
{
	int a=2, b=4;
	double c=12.3, d=23.4;
	compl e = compl(0.0, 1.0);
	compl f = compl(-1.0, 0.0);

	Tausch(a, b);
	cout << endl << a << endl << b << endl << endl;
	Tausch(c, d);
	cout << endl << c << endl << d << endl << endl;
	Tausch(e, f);
	cout << setprecision(1) << setiosflags(ios::fixed) << endl << e << endl << f << endl << endl;

	return 0;
	
}
 
Hi,

Ich nochmal, ich bekomme die Ausgabe nicht zum laufen, << wäre kein zulässiger Bitoperator (oder so) sagt er, da ich überhaupt keine Idee habe, frage ich einfach nochmal, ich hoffe ihr könnt mir helfen
 
Zurück