-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathUpdateSimpleCalc_22026555.cpp
More file actions
140 lines (138 loc) · 2.8 KB
/
UpdateSimpleCalc_22026555.cpp
File metadata and controls
140 lines (138 loc) · 2.8 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
#include <bits/stdc++.h>
using namespace std;
int arithmetic(double num1, double num2, char op);
double sothuc(string op, double num);
int main(int argc, char* argv[])
{
if (argc == 4)
{
string n = argv[2];
int k = n.length();
if (k != 1)
{
cout << "Invalid operator" << endl; return 0;
}
int num1, num2; char op;
string tmp = argv[1];
string tmpp = argv[3];
for (int i = 0; i < tmp.length(); i++)
{
if (tmp[i] < '0' || tmp[i] > '9')
{
cout << "Invalid number"; exit(1);
}
}
for (int i = 0; i < tmpp.length(); i++)
{
if (tmpp[i] < '0' || tmpp[i] > '9')
{
cout << "Invalid number"; exit(1);
}
}
num1 = atof(argv[1]);
op = argv[2][0];
num2 = atof(argv[3]);
cout << arithmetic(num1, num2, op) << endl;
return 0;
}
else if (argc == 3)
{
string n = argv[1];
int k = n.length();
if (k == 3){
if ((n != "cos") && (n != "sin") && (n != "tan" && n != "cot")){
cout<< "Invalid operator";
exit(1);
}
}
if (k == 4){
if (n != "sqrt"){
cout<< "Invalid operator";
exit(1);
}
}
string tmp = argv[2];
for (int i = 0; i < tmp.length(); i++)
{
if (tmp[i] < '0' || tmp[i] > '9')
{
cout << "Invalid number"; exit(1);
}
}
double num;
num = atof(argv[2]);
cout<< sothuc(n, num);
}
else {
cout<< "Invalid operator";
exit(1);
}
}
int arithmetic(double num1, double num2, char op)
{
switch (op)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (num2 == 0)
{
cout << "Invalid divisor" << endl;
exit(1);
}
else
return num1 / num2;
case '%':
if (num2 == 0 || num2 != (int)num2 || num1 != (int)num1)
{
cout << "Invalid divisor" << endl;
exit(1);
}
else
return int(num1) % int(num2);
default:
cout << "Invalid operator" << endl;
exit(1);
}
}
double sothuc(string op, double num)
{
if (op == "sin")
{
return sin(num);
}
else if (op == "cos")
{
return cos(num);
}
else if (op == "tan")
{
if (cos(num) == 0)
{
cout << "Invalid Number"; exit(1);
}
else return tan(num);
}
else if (op == "cot")
{
if (sin(num) == 0)
{
cout << "Invalid Number"; exit(1);
}
else return (1/tan(num));
}
else if (op == "sqrt")
{
if (num < 0)
{
cout << "Invalid Number"; exit(1);
}
else return sqrt(num);
}
else {cout << "Invalid operator" << endl;
exit(1);}
}