-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.c
More file actions
213 lines (175 loc) · 4.13 KB
/
Copy pathsol.c
File metadata and controls
213 lines (175 loc) · 4.13 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
struct root
{
double r, i;
};
void print_root(struct root x)
{
printf("( %+lf ; %+lf )", x.r, x.i);
}
int read_col(int a_flag, int b_flag, int c_flag, int _a, int _b, int _c, int complex)
{
while(1)
{
int code = 1;
double a = _a, b = _b, c = _c;
if (a_flag == 0)
code = scanf("%lf", &a);
if ((b_flag == 0) && (code == 1))
code = scanf("%lf", &b);
if ((c_flag == 0) && (code == 1))
code = scanf("%lf", &c);
if(code == EOF)
{
break;
}
if(code != 1)
{
return 1;
}
solve_equation(a, b, c, complex);
}
return 0;
}
int solve_equation(double a, double b, double c, int complex)
{
struct root x1 = {0, 0}, x2 = {0, 0};
if (a == 0)
{
if (b != 0)
{
x1.r = -1.*c/b;
x1.i = 0;
x2 = x1;
}
else
{
goto wrongInput;
}
}
else
{
double d = (b * b) - 4 * a * c;
if (d>=0)
{
x1.r = (-b + sqrt(d)) / (2 * a);
x1.i = 0;
x2.r = (-b - sqrt(d)) / (2 * a);
x2.i = 0;
}
else
{
if (complex == 1)
{
x1.r = (-b ) / (2 * a);
x1.i = sqrt(-1. * d) / (2 * a);
x2.r = (-b) / (2 * a);
x2.i = -1.*sqrt(-1. * d) / (2 * a);
}
else
{
goto wrongInput;
}
}
}
if (x1.r*x1.r + x1.i*x1.i > x2.r*x2.r + x2.i*x2.i)
{
struct root temp;
temp = x1;
x1 = x2;
x2 = temp;
}
print_root(x1);
printf("\t");
print_root(x2);
printf("\n");
return 0;
wrongInput:
fprintf(stderr, "Wrong command line arguments given.\n");
return 1;
}
int main(int argc, char **argv)
{
double a = 0, b = 0, c = 0;
int a_flag = 0, b_flag = 0, c_flag = 0;
int complex = 0;
/*int argc = 5;
char *argv[5]= {"nameprog", "-a", "0", "-c", "0"};*/
size_t current = 1;
while (current != argc)
{
if ((strcmp(argv[current], "-i") == 0) || (strcmp(argv[current], "--complex") == 0))
{
complex = 1;
current++;
continue;
}
if (strcmp(argv[current], "-a") == 0)
{
if (current + 1 == argc || a_flag == 1)
goto wrongInput;
char *err_ptr = argv[current + 1];
a = strtod(argv[current + 1], &err_ptr);
a_flag = 1;
if (strcmp(err_ptr, "") == 0)
{
current += 2;
}
else
{
goto wrongInput;
}
continue;
}
if (strcmp(argv[current], "-b") == 0)
{
if (current + 1 == argc || b_flag == 1)
goto wrongInput;
char *err_ptr = argv[current + 1];
b = strtod(argv[current + 1], &err_ptr);
b_flag = 1;
if (strcmp(err_ptr, "") == 0)
{
current += 2;
}
else
{
goto wrongInput;
}
continue;
}
if (strcmp(argv[current], "-c") == 0)
{
if (current + 1 == argc || c_flag == 1)
goto wrongInput;
char *err_ptr = argv[current + 1];
c = strtod(argv[current + 1], &err_ptr);
c_flag = 1;
if (strcmp(err_ptr, "") == 0)
{
current += 2;
}
else
{
goto wrongInput;
}
continue;
}
current++;
}
//scanf("%lf%lf%lf", &a, &b, &c);
if (a_flag && b_flag && c_flag)
{
return solve_equation(a, b, c, complex);
}
else
{
return read_col(a_flag, b_flag, c_flag, a, b, c, complex);
}
wrongInput:
fprintf(stderr, "Wrong command line arguments given.\n");
return 1;
}