-
Notifications
You must be signed in to change notification settings - Fork 2
/
calc.c
312 lines (238 loc) · 5.31 KB
/
calc.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "main.h"
int check(int arg, char *s);
/**
* main - The body of the calculator
* @argc: command line argument operations and values to perform them on
* @argv: stings of operations and values to perform them on
*
* Return: 0 if compiled successfully
*/
int main(int argc, char *argv[])
{
/**
* Declaring varables that can be used in the calculator,
* and inserted into functions.
*/
float res, prev, prev_mul, num;
int i, c, ch;
prev = 0;
prev_mul = 1;
/* checking for no arguments */
if (argc == 1)
{
printf("No operation passed as arguments\n");
printf("Usage: ./calculator <<operation>> <<argument(s)>>\n"
"Usage: ./calculator menu\n");
return (1);
}
/* Printing a menu for help */
c = strcmp(argv[1], "menu");
if (c == 0)
{
printf("To use the calculator, run:\n"
"./calculator <<operation>> <<argument(s)>>\n");
printf("Available functions (operation) (argument(s))\n"
"Addition (add) (a) (b) (c) (...)\n"
"Subtraction (subtract) (a) (b) (c) (...)\n"
"Multiplication (multiply) (a) (b) (c) (...)\n"
"Division (divide) (a) (b) (c) (...)\n"
"Factorial (factorial) (a) (b) (...)\n"
"Power (power) (a) (b)\n"
"Converting bases (base) (num) (base) (dest_base)\n"
"Quadratic (quadratic) (a) (b) (c)\n"
"Root (root) (a) (b) (c) (...)\n"
"Silmultaneous (simultaneous) a(x) b(y) c d(x) e(y) f\n"
);
return (0);
}
c = strcmp(argv[1], "add");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
for (i = 2; i < argc; i++)
{
num = atof(argv[i]);
res = add(prev, num);
prev = res;
}
printf("The result of the addition is: %.2f\n", res);
return (0);
}
c = strcmp(argv[1], "subtract");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
num = atof(argv[2]);
for (i = 2; i < argc; i++)
{
/* prev = next number, starts as 0 */
res = sub(num, prev);
num = res;
if (argv[i + 1])
{
prev = atof(argv[i + 1]);
}
}
printf("The result of the subtraction is: %.2f\n", res);
return (0);
}
c = strcmp(argv[1], "multiply");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
for (i = 2; i < argc; i++)
{
num = atof(argv[i]);
res = mul(prev_mul, num);
if (res > FLT_MAX)
{
printf("Exceeded maximum result value\n");
return (1);
}
prev_mul = res;
}
printf("The result of the multiplication is: %.2f\n", res);
return (0);
}
c = strcmp(argv[1], "divide");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
num = atof(argv[2]);
for (i = 2; i < argc; i++)
{
/* prev_mul = next number to divide, init as 1 */
res = divi(num, prev_mul);
num = res;
if (argv[i + 1])
{
prev_mul = atof(argv[i + 1]);
}
}
printf("The result of the division is: %.2f\n", res);
return (0);
}
c = strcmp(argv[1], "factorial");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
for (i = 2; i < argc; i++)
{
res = fact(atof(argv[i]));
printf("The factorial of %.2f is %.2f\n",
atof(argv[i]), res);
}
return (0);
}
c = strcmp(argv[1], "power");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
if (argc > 4)
printf("Power operation takes the 1st two operands\n");
res = _pow(atof(argv[2]), atoi(argv[3]));
printf("%.2f raised to the power of %d is %.2f\n",
atof(argv[2]), atoi(argv[3]), res);
return (0);
}
c = strcmp(argv[1], "base");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
if (argc < 5)
{
printf("Insufficient parameters\n"
"Usage: ./calculator base num current_base dest_base\n");
return (0);
}
if (argc > 5)
printf("Base operation takes the 1st three operands\n");
convert(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
return (0);
}
c = strcmp(argv[1], "quadratic");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
if (argc < 5)
{
printf("Insufficient parameters\n"
"Usage: ./calculator quadratic (a) (b) (c)\n");
return (0);
}
if (argc > 5)
printf("Quad operation takes the 1st three operands\n");
quad(atof(argv[2]), atof(argv[3]), atof(argv[4]));
return (0);
}
c = strcmp(argv[1], "root");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
for (i = 2; i < argc; i++)
{
res = root(atof(argv[i]));
printf("The square root of %.2f is %.2f\n",
atof(argv[i]), res);
}
return (0);
}
c = strcmp(argv[1], "simultaneous");
if (c == 0)
{
ch = check(argc, argv[1]);
if (ch == 1)
return (1);
if (argc < 8)
{
printf("Insufficient parameters\n"
"Usage: ./calculator simultaneous ax by c dx ey f\n");
return (0);
}
if (argc > 8)
printf("Quad operation takes the 1st three operands\n");
simu(atof(argv[2]), atof(argv[3]), atof(argv[4]), atof(argv[5]),
atof(argv[6]), atof(argv[7]));
return (0);
}
printf("Error: Invalid function to calulate!\n");
printf("Usage: ./calculator menu\nUsage: will list all operations\n");
return (1);
}
/**
* check - checks if arg is equal to 2
* @arg: arg to check
* @s: function name
*
* Return: 1 if arg is 2 and 0 otherwise
*/
int check(int arg, char *s)
{
if (arg == 2)
{
printf("Error: Insufficient values passed to %s function\n", s);
return (1);
}
else
{
return (0);
}
}