-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_4_4_2015.05.26.c
161 lines (144 loc) · 3.47 KB
/
C_4_4_2015.05.26.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <limits.h>
#define pi 3.1415
struct complex
{
double Re, Im;
};
void cPrint (struct complex C)
{
if (C.Im >= 0)
printf("%f+%fi\n", C.Re, C.Im);
else
printf("%f%fi\n", C.Re, C.Im);
}
struct complex cAdd(struct complex C1, struct complex C2)
{
struct complex temp;
temp.Re = C1.Re + C2.Re;
temp.Im = C1.Re + C2.Im;
return temp;
}
struct complex cSub(struct complex C1, struct complex C2)
{
struct complex temp;
temp.Re = C1.Re - C2.Re;
temp.Im = C1.Im - C2.Im;
return temp;
}
double cReal(struct complex C1)
{
return C1.Re;
}
double cImag(struct complex C1)
{
return C1.Im;
}
struct complex cConj(struct complex C1)
{
struct complex temp;
temp.Re = C1.Re;
temp.Im = -C1.Im;
return temp;
}
double cAbs(struct complex C)
{
int a = C.Re, b = C.Im;
return sqrt(a*a+b*b);
}
double cArg(struct complex C)
{
int a = C.Re, b = C.Im;
if (a == 0)
if (b == 0)
return INT_MIN;
else
return pi/2;
else
if (b < 0)
return -atan(-b/abs(a));
else
return atan(b/abs(a));
}
struct complex cMul(struct complex C1, struct complex C2)
{
struct complex temp;
temp.Re = C1.Re*C2.Re - C1.Im*C2.Im;
temp.Im = C1.Re*C2.Im + C1.Im*C2.Re;
return temp;
}
struct complex cDiv(struct complex C1, struct complex C2)
{
struct complex temp;
temp.Re = (C1.Re*C2.Re + C1.Im*C2.Im)/cAbs(C2);
temp.Im = (C2.Re*C1.Im - C2.Im*C1.Re)/cAbs(C2);
return temp;
}
/*struct complex cRead(char *str)
{
int i, l = strlen(str), c=0;
double a, b;
int f = 0 ;
char t[100];
struct complex temp;
for (i = 0; i < l; i++)
{
if ( (str[i] != '+') && (str[i] != 'i') && (str[i] != '-') && (str[i] != '.'))
{
t[c] = str[i];
c++;
}
else
{
if (f==0)
{
a = atof(t);
f=1;
}
else
b = atof(t);
strcpy(t, "\0");
c = 0;
}
}
temp.Re = a;
temp.Im = b;
return temp;
}*/
struct complex cRead(double a, double b)
{
struct complex temp;
temp.Re = a;
temp.Im = b;
return temp;
}
int main()
{
char s[100];
double a, b;
struct complex C1, C2;
printf("Введите комплексное число в формате a, b: ");
scanf("%lf%lf", &a, &b);
C1 = cRead(a, b);
cPrint(C1);
printf("Введите комплексное число в формате a,b: ");
scanf("%lf%lf", &a, &b);
C2 = cRead(a,b);
cPrint(C2);
printf("РЎСѓРјРјР°: ");
cPrint(cAdd(C1, C2));
printf("Разность: ");
cPrint(cSub(C1, C2));
printf("Произведение: ");
cPrint(cMul(C1, C2));
printf("Частное: ");
cPrint(cDiv(C1, C2));
printf("Модуль 1-ого: %f: \n", cAbs(C1));
printf("Аргумент 1-ого: %f: \n", cArg(C1));
printf("Сопряжённое 1-ого: "); cPrint(cConj(C1));
printf("Re 1-РѕРіРѕ: %f: \n", cReal(C1));
printf("Im 1-РѕРіРѕ: %f: \n", cImag(C1));
return 0;
}