void TestMathematicalOperationsOfTwoComplexNumbers()
{
	cnum1.real = 1;
	cnum1.imag = 2;
	cnum2.real = 3;
	cnum2.imag = 4;

	complexNumber result;

	result = complexAdd(cnum1, cnum2);
	assert(result.real == 4);
	assert(result.imag == 6);

	result = complexSub(cnum1, cnum2);
	assert(result.real = -2);
	assert(result.imag = -2);

	result = complexMul(cnum1, cnum2);
	assert(result.real = -5);
	assert(result.imag = 10);

	result = complexDiv(cnum1, cnum2);
	assert(result.real = .44);
	assert(result.imag = .08);
}
/**
*					---showMenu---
*
* Gives the user the choice of the mathematical operation 
* to be executed
*
* 1: ADDITION | 2: SUBTRACTION | 3: MULTIPLICATION | 4: DIVISION
*
* recursive exception-handling for wrong user input
*/
void showMenu()
{
	system("CLS");
	printf_s("Welche Rechnenoperation m%cchten Sie durchf%chren?", oe, ue); 
	printf_s("\n1: Addition\n2: Subtraktion\n3: Multiplikation\n4: Division\n");

	int userInput;
	complexNumber divResult;

	fflush(stdin);
	scanf_s("%d", &userInput); fflush(stdin);

	switch (userInput)
	{
	case ADDITION:
		printf_s("\nDas Ergebnis der Operation Zahl1 + Zahl2 lautet: ");
		showResult(complexAdd(cnum1, cnum2));
		break;

	case SUBTRACTION:
		printf_s("\nDas Ergebnis der Operation Zahl1 - Zahl2 lautet: ");	
		showResult(complexSub(cnum1, cnum2));
		break;

	case MULTIPLICATION:
		printf_s("\nDas Ergebnis der Operation Zahl1 * Zahl2 lautet: ");
		showResult(complexMul(cnum1, cnum2));
		break;

	case DIVISION:
		divResult = complexDiv(cnum1, cnum2);
		if(divResult.real != -1 && divResult.imag != -1)
		{
			printf_s("\nDas Ergebnis der Operation Zahl1 / Zahl2 lautet: ");
			showResult(divResult);
			break;
		} else 
			break;

	default: 
		printf_s("\n%d, ist keine g%cltige Eingabe...\nBitte wiederholen:\n", userInput, ue);
		Sleep(2000);
		showMenu();
	}
}
Beispiel #3
0
int main(){
	complex c1={0,0};
	complex c2={0,0};
	printf("Input Complex number c1 (a+bi): ");
	scanf("%lf+%lfi,",&c1.re,&c1.im);
	printf("Input Complex number c2 (c+di): ");
	scanf("%lf+%lfi,",&c2.re,&c2.im);
	printf("Specify Operation: {ADD|MUL}:");
	char s[4];	scanf("%s",s);
	complex result={0,0};
	if(s[0]=='A'){
		result=complexAdd(c1,c2);
	}else{
		result=complexMul(c1,c2);
	}
	printf("Result: %lg + %lg i\n",result.re,result.im);
	return 0;
}
Beispiel #4
0
complex_t* fft(complex_t* x, int N) 
{
	complex_t* X = (complex_t*) malloc(sizeof(struct Complex) * N);
	complex_t * d, * e, * D, * E;
	int k;

	if (N == 1) 
	{
		X[0] = x[0];
		return X;
	}

	e = (complex_t*) malloc(sizeof(struct Complex) * N / 2);
	d = (complex_t*) malloc(sizeof(struct Complex) * N / 2);

	for (k = 0; k < N / 2; k++) 
	{
		e[k] = x[2 * k];
		d[k] = x[2 * k + 1];
	}

	E = fft(e, N / 2);
	D = fft(d, N / 2);

	free(e);
	free(d);

	for (k = 0; k < N / 2; k++) 
	{
		/* Multiply entries of D by the twiddle factors e^(-2*pi*i/N * k) */
		D[k] = complexMult(complexFromPolar(1, -2.0 * M_PI *k / N), D[k]);
	}

	for (k = 0; k < N / 2; k++) 
	{
		X[k]       = complexAdd(E[k], D[k]);
		X[k + N/2] = complexSub(E[k], D[k]);
	}

	free(D);
	free(E);
	return X;
}