-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
233 lines (180 loc) · 3.36 KB
/
functions.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "jac.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned long long binomial(unsigned long long n,
unsigned long long k)
{
if(k > n)
return 0;
/* symmetry */
if(k > n - k)
k = n - k;
if(k > n / 2)
k = n - k;
unsigned long long c = 1;
unsigned long long result = 1;
for(;c <= k; c++) {
result *= n--;
result /= c;
}
return result;
}
unsigned long factorial(unsigned long f)
{
if (f == 0)
return 1;
return(f * factorial(f - 1));
}
int quadr_eq() /* Solve a quadratic equation */
{
double a, b, c, x1, disc;
printf("Please enter the value of a:\n");
printf(">");
scanf("%lf", &a);
printf("Please enter the value of b:\n");
printf(">");
scanf("%lf", &b);
printf("Please enter the value of c:\n");
printf(">");
scanf("%lf", &c);
if (a == 0)
{
x1 = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf\n", x1);
}
else
{
disc = (b*b) - 4*a*c;
double den = 2*a;
if (disc == 0)
{
x1 = -b / den;
printf("x1 = %lf\nx2 = %lf\n", x1, x1);
}
else if (disc > 0)
{
double sq = sqrt(disc);
x1 = (-b + sq) / den;
double x2 = (-b - sq) / den;
printf("x1 = %.1lf\nx2 = %.1lf\n", x1, x2);
}
else
{
double xr = - b / den;
double num = -(b*b - 4*a*c);
printf("x1 = %lf + i sqrt(%lf)/%lf\nx2 = %lf -i sqrt(%lf)/%lf\n", xr, num,den, xr, num,den);
}
}
return 0;
}
long long bin_dec(long long n)
{
int dec = 0, i = 0, rem;
while (n != 0)
{
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
long long dec_bin(long long n)
{
long long bin = 0;
int rem, i;
rem = i = 1;
while (n != 0)
{
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
int input_number(unsigned long long *num)
{
*num = 0;
char c;
while(1) {
c = getchar();
if (!isspace(c))
break;
}
while(1) {
if (c == '\n')
return 0;
if (!isdigit(c))
return -2;
*num *= 10;
*num += c - '0';
c = getchar();
}
}
int special_functions() /* Functions seldomly used */
{
long long (*fp[2])(long long n);
fp[0] = dec_bin;
fp[1] = bin_dec;
unsigned int ans;
long long number;
printf("Enter 1 for decimal to binary conversion\n");
printf("Enter 2 for binary to decimal conversion\n");
printf("Enter 3 to solve ax^2+bx+c=0\n");
printf("Enter 4 to calculate the binomial coefficient C_{n,k}\n");
printf("Enter 0 to quit\n>>");
scanf("%d", &ans);
if(ans != 0 && ans < 5)
{
if (ans == 3)
quadr_eq();
if (ans == 4)
{
unsigned long long n,k;
printf("input n:\n>");
int ret = input_number(&n);
if (ret < 0) {
printf("Error reading n\n");
return 1;
}
printf("input k:\n>");
ret = input_number(&k);
if (ret < 0) {
printf("Error reading k\n");
return 1;
}
printf("%lld\n>>", binomial(n,k));
}
else
{
printf(">");
scanf("%lld", &number);
printf("%lld\n", fp[ans-1](number));
}
}
return 0;
}
void print_result(long double x)
{
long double tmp = ceill(x);
if (fabsl(x-tmp) < 0.00000000001)
{
x = tmp;
}
if (x == -0)
x = fabsl(x);
printf("%.19Lg\n", x);
}
void remove_spaces(char *str)
{
unsigned int count = 0;
unsigned int i;
for (i = 0; str[i]; i++)
if (str[i] != ' ' && str[i] != '\n')
str[count++] = str[i];
str[count] = '\0';
}