-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc.c
More file actions
84 lines (79 loc) · 2.08 KB
/
Copy pathc.c
File metadata and controls
84 lines (79 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*---------------------------------------------------------------------------------------
ComplexNums.c
Menu driven program to define a structure Complex to find sum and product of complex numbers.
DIVYA RAJ.K
16-10-18
-----------------------------------------------------------------------------------------*/
#include<stdio.h>
typedef struct ComplexType{
float Real;
float Imag;
}COMPLEX;
//----------------------------------function prototype---------------------------------------------
void fnInputComplex(COMPLEX *C);
void fnPrintComplex(COMPLEX C);
COMPLEX fnAddComplexNumbers(COMPLEX C1,COMPLEX C2);
COMPLEX fnProdComplexNumbers(COMPLEX C1,COMPLEX C2);
main()
{
COMPLEX X,Y,Z;
int ch;
system("clear");
printf("Menu driven program to define a structure Complex\n\n");
ch=1;
while(ch!=4){
printf("MENU\n\n1.Input complex numbers\n");
printf("2.Add complex numbers\n");
printf("3.Multiply complex numbers\n");
printf("4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1: printf("Enter the real and imaginary part of first complex number: ");
fnInputComplex(&X);
printf("Enter the real and imaginary part of second complex number: ");
fnInputComplex(&Y);
printf("\nComplex Number 1\n");
fnPrintComplex(X);
printf("\nComplex Number 2\n");
fnPrintComplex(Y);
break;
case 2: Z=fnAddComplexNumbers(X,Y);
printf("\nThe sum is: ");
fnPrintComplex(Z);
break;
case 3: Z=fnProdComplexNumbers(X,Y);
printf("\nThe product is: ");
fnPrintComplex(Z);
break;
case 4: break;
}
}
}
void fnInputComplex(COMPLEX *C)
{
scanf("%f",&(C->Real));
scanf("%f",&(C->Imag));
}
void fnPrintComplex(COMPLEX C)
{
printf("%5.2f",C.Real);
if(C.Imag<0)
printf("-%5.2f i\n",C.Imag);
else
printf("+%5.2f i\n",C.Imag);
}
COMPLEX fnAddComplexNumbers(COMPLEX C1,COMPLEX C2)
{
COMPLEX Z;
Z.Real=C1.Real+C2.Real;
Z.Imag=C1.Imag+C2.Imag;
return Z;
}
COMPLEX fnProdComplexNumbers(COMPLEX C1,COMPLEX C2)
{
COMPLEX Z;
Z.Real=C1.Real*C2.Real-C1.Imag*C2.Imag;
Z.Imag=C1.Real*C2.Imag+C1.Imag*C2.Real;
return Z;
}