-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.c
295 lines (280 loc) · 7.04 KB
/
Source.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
/************************
* *
* Kornelijus Petronis *
* namu darbas *
* Assessment task 1 *
* EAf-19 *
* *
***********************/
/**
* @file Source.c
* @author Kornelijus Petronis
* @date 4 Nov 2019
* This program allows you to calculate certain mathematical functions.
* CMD calculator
* Command line utility.
*
* Syntax
* =================================================================
* smartcalc <operation> name> <x> <y> CALCULATOR
* smartcalc <operation> --help Specific explanation of operation
* smartcalc --help List of operations
* =================================================================
*/
#define _USE_MATH_DEFINES
// Library list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// =========================================
#define TORAD 180.0 / M_PI;
#define TOANGLE M_PI / 180.0;
// =========================================
#define operationAmount 16
// Operation list
char *operation[operationAmount] = {
"floor",
"round",
"ceil",
"sin",
"cos",
"cosh",
"exp",
"tan",
"tanh",
"sinh",
"log",
"log10",
"sqrt",
"pow",
"trunc",
"--help"};
// =========================================
// Enumeration
enum operations
{
Floor,
Round,
Ceil,
Sin,
Cos,
Cosh,
Exp,
Tan,
Tanh,
Sinh,
Log,
Log10,
Sqrt,
Pow,
Trunc
};
//==========================================
// All functions
void calc(int index, double y, double z, int err);
// err = 0 Calculator
// err = 1 Show explanation
//=================- MAIN -=========================
int main(int argc, char *argv[])
{
double number, numbery; /** x and y values for calculator*/
int state = 0;
for (int i = 0; i < operationAmount - 1; i++)
{
if (!strcmp(operation[i], argv[1]))
{
//====
// Help for specific operation
if (!strcmp("--help", argv[2]))
{
// Show operation explanation
calc(i, 0, 0, 1);
}
//====
// One argument
if (i != 13 && atof(argv[2]))
{
number = atof(argv[2]);
calc(i, number, 0, 0);
}
// Two arguments
else if (atof(argv[2]))
{
number = atof(argv[2]);
numbery = atof(argv[3]);
calc(i, number, numbery, 0);
}
state = 1;
}
// Help list
else if (!strcmp("--help", argv[1]))
{
printf("----- Help -----\n");
printf("This is smart calculator\n\n");
printf("More information about operations\n");
printf("Smartcalc <operation> --help\n\n");
printf("Smartcalc <operation> <double x> <double y>\n\n");
printf("Operation list\n");
printf("floor\nround\nceil\nsin\ncos\ncosh\n"
"exp\ntan\ntanh\nsinh\nlog\nlog10\n"
"sqrt\npow\ntrunc\n");
state = 1;
break;
}
}
// Unknown argument
if (!state)
printf("Write --help for help\n");
return 0;
}
// =============================================
// Switch calculation functions
/**
* @brief Calculator function
*
* @param index
* @param y
* @param z
* @param err
*/
void calc(int index, double y, double z, int err)
{
enum operations oper = index;
switch (oper)
{
// =========================================================
// Floor
case Floor:
if (!err)
printf("Largest integer value less than or equal to %.2lf is %.2lf\n", y, floor(y));
else
printf("Double floor(double x) returns the largest integer value less than or equal to x.\n");
break;
//=========================================================
// Round
case Round:
if (!err)
printf("round of %.2lf is %lf\n", y, round(y));
else
printf("Double Round(double x) returns the nearest integer value of the double\n");
break;
//=========================================================
// Ceil
case Ceil:
if (!err)
printf("Smallest integer value greater than or equal to %.2lf is %.0lf\n", y, ceil(y));
else
printf("Double ceil(double x) returns the smallest integer value greater than or equal to x.\n");
break;
//=========================================================
// Sin
case Sin:
if (!err)
{
z = y * TOANGLE;
printf("Sin value for %.2lf degrees equals to %.4lf\n", y, sin(z));
}
else
printf("double sin(double x) returns the sine of a radian angle x\n");
break;
//=========================================================
// Cos
case Cos:
if (!err)
{
z = y * TOANGLE;
printf("The cosine of %.2lf is %.4lf degrees\n", y, cos(z));
}
else
printf("Double cos(double x) returns the cosine of a radian angle x\n");
break;
//=========================================================
// Cosh
case Cosh:
if (!err)
printf("The hyperbolic cosine of %.2lf is %.4lf\n", y, cosh(y));
else
printf("Double cosh(double x) returns the hypebolic cosine of x\n");
break;
//=========================================================
// Exp
case Exp:
if (!err)
printf("The exponential value of %.2lf is %.4lf\n", y, exp(y));
else
printf("Double exp(double x) returns the value of e raised to the xth power\n");
break;
//=========================================================
// Tan
case Tan:
if (!err)
{
z = y * TOANGLE;
printf("Tangent of %.2lf degrees is %.4lf\n", y, tan(z));
}
else
printf("Returns the tangent of an angle of x radians\n");
break;
//=========================================================
// Tanh
case Tanh:
if (!err)
printf("The hyperbolic tangent of %.2lf is %.2lf degrees\n", y, tanh(y));
else
printf("Double tanh(double x) returns the hyperbolic tangent of x\n");
break;
//=========================================================
// Sinh
case Sinh:
if (!err)
printf("The hyperbolic sine of %lf is %lf degrees\n", y, sinh(y));
else
printf("Double sinh(double x) returns the hyperbolic sine of x\n");
break;
//=========================================================
// Log
case Log:
if (!err)
printf("log(%.2lf) = %.2lf\n", y, log(y));
else
printf("Double log(double x) returns the natural logarithm (base-e logarithm) of x\n");
break;
//=========================================================
// Log10
case Log10:
if (!err)
printf("log10(%.2lf) = %.2lf\n", y, log10(y));
else
printf("Double log10(double x) returns the common logarithm (base-10 logarithm) of x\n");
break;
//=========================================================
// Sqrt
case Sqrt:
if (!err)
printf("Square root of %.2lf is %.2lf\n", y, sqrt(y));
else
printf("Double sqrt(double x) returns the square root of x\n");
break;
//=========================================================
// Pow
case Pow:
if (!err)
printf("%.2lf ^ %.2lf = %lf\n", y, z, pow(y, z));
else
printf("Double pow(double x, double y) returns x raised to the power of y i.e. xy\n");
break;
//=========================================================
// Trunc
case Trunc:
if (!err)
printf("Truncated value of %.2lf is %.0lf\n", y, trunc(y));
else
printf("This function truncates the decimal value from floating point value and returns integer value\n");
break;
//=========================================================
default:
printf("Write --help for help\n");
break;
}
}