-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
710 lines (656 loc) · 16 KB
/
calculator.cpp
File metadata and controls
710 lines (656 loc) · 16 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
//Function Call for Each Calculation
void switchcase(int sino);
void arithmetic();
void trignometry();
void logarithmic();
void exponential();
void relational();
void addition();
void subtraction();
void multiplication();
void division();
double raddegcheck(int &);
void inversetrig();
void trig();
void Sin(double pi);
void Cos(double pi);
void Tan(double pi);
void Cot(double pi);
void Sec(double pi);
void Cosec(double pi);
void ArcSin();
void ArcCos();
void ArcTan();
void ArcCot();
void ArcSec();
void ArcCosec();
//void modu();
int main() {
cout << "Welcome to Calculator Program"<< endl ;
cout<<"This is a program to perform various mathematical operations such as Arithmetic, Trignometric,"
"Exponential, Logarithmic Functions and Relational Operations"<<endl;
/*cout<<endl<<"1. Arithmetic Operations"<<endl<<"2. Trignometric Operations"<<endl<<"3. Logarithmic Functions";
cout<<endl<<"4. Exponential Functions"<<endl<<"5. Relational Operations"<<endl;*/
bool cont=true;
char yesno;
int SINo;
//For user to enter the value continuously till a limit/
while(cont)
{
cout<<"Enter a number to perform the operation(between 1-5)";
cout<<endl<<"1. Arithmetic Operations"<<endl<<"2. Trignometric Operations"<<endl<<"3. Logarithmic Functions";
cout<<endl<<"4. Exponential Functions"<<endl<<"5. Relational Operations"<<endl;
cin>>SINo;
if(SINo>0 && SINo<=5)
{
switchcase(SINo);
}
else
{
cout<<endl<<"Enter a valid number between 1 and 5"<<endl;
continue;
}
//Checking whether the use want to continue the program//
checkagain:
cout<<"Enter (Y/N) for Continuing Calculator Program "<<endl<<"Y for Continue"<<endl<<"N for Exiting"<<endl;
cin>>yesno;
if (yesno == 'y' || yesno == 'Y')
{
{
cont = true;
}
}
else if (yesno == 'n' || yesno == 'N')
{
{
cont = false;
cout << "Calculator Program Ended Successfully" << endl;
}
}
else
{
cout << "Wrong Output" << endl;
goto checkagain;
// cout << "Wrong Output. Yes(Y) taken as default" << endl;
}
}
return 0;
}
void switchcase(int sino)
{
//To check which operation to be performed//
switch(sino)
{
case 1:
{
arithmetic();
cout<<endl;
break;
}
case 2:
{
trignometry();
cout<<endl;
break;
}
case 3:
{
logarithmic();
cout<<endl;
break;
}
case 4:
{
exponential();
cout<<endl;
break;
}
case 5:
{
relational();
cout<<endl;
break;
}
default:
{
cout<<"This will never get printed!!!"<<endl;
break;
}
}
}
//Arithmetic Operation//
void arithmetic()
{
cout<<endl<<"Welcome To Arithmetic Operations such as"<<endl;
cout<<"Addition, Subtraction, Multiplication, Division"<<endl;
bool cont1=true;
char yesorno;
int switchvalue;
while(cont1)
{
cout<<"Enter a number between 1 and 5 to perform Arithmetic Operation"<<endl;
cout<<"1 for Addition"<<endl<<"2 for Subtraction"<<endl;
cout<<"3 for Multiplication"<<endl<<"4 for Division"<<endl;
// cout<<"5 for Modulus"<<endl;
cin>>switchvalue;
if (switchvalue>0 && switchvalue<=4)
{
switch(switchvalue)
{
case 1:
{
addition();
cout<<endl;
break;
}
case 2:
{
subtraction();
cout<<endl;
break;
}
case 3:
{
multiplication();
cout<<endl;
break;
}
case 4:
{
division();
cout<<endl;
break;
}
/* case 5:
{
modu();
cout<<endl;
break;
}*/
default:
break;
}
}
else
{
cout<<"Enter a valid number between 1 and 4"<<endl;
continue;
}
//Checking whether the use want to continue the program//
arithmeticcheck:
cout<<"Enter (Y/N) for Arithmetic Operations"<<endl<<"Y for continue"<<endl<<"N for Exiting"<<endl;
cin>>yesorno;
if (yesorno == 'y' || yesorno == 'Y')
{
{
cont1 = true;
}
}
else if (yesorno == 'n' || yesorno == 'N')
{
{
cont1 = false;
cout << "Arithmetic Operation Completed Successfully" << endl;
}
}
else
{
cout << "Wrong Output" << endl;
goto arithmeticcheck;
// cout << "Wrong Output. Yes(Y) taken as default" << endl;
}
}
}
void addition()
{
double n,ar[1024],sum=0;
int i;
cout<<"No of terms to find sum"<<endl;
cin>>n;
cout<<endl<<"Enter numbers";
for(i=0;i<n;i++)
{
cin>>ar[i];
}
for(i=0;i<n;i++)
{
sum=sum+ar[i];
}
cout<<endl<<"Sum of "<<i<<" terms is"<<sum<<endl;
}
void subtraction()
{
cout<< endl;
double n,ar[1024],diff;
int i;
cout<<"No of terms to find difference"<<endl;
cin>>n;
cout<<endl<<"Enter numbers"<<endl;
for(i=0;i<n;i++)
{
cin>>ar[i];
}
diff=ar[0];
for(i=1;i<=n;i++)
{
diff=diff-ar[i];
}
cout<<endl<<"Difference of "<<i<<" terms is"<<diff<<endl;
}
void multiplication()
{
cout<< endl;
double n,ar[1024],pro=1;
int i;
cout<<"No of terms to find product"<<endl;
cin>>n;
cout<<endl<<"Enter numbers"<<endl;
for(i=0;i<n;i++)
{
cin>>ar[i];
}
for(i=0;i<n;i++)
{
pro=pro*ar[i];
}
cout<<endl<<"Sum of " << i-1<<" terms is "<<pro<<endl;
}
void division()
{
cout<< endl;
double n,ar[1024],div;
int i;
cout<<"No of terms to divide: "<<endl;
cin>>n;
cout<<endl<<"Enter numbers: "<<endl;
for(i=0;i<n;i++)
{
cin>>ar[i];
}
div=ar[0];
for(i=1;i<n;i++)
{
if (ar[i] != 0)
{
div = div / ar[i];
}
else
{
cout<<endl<<"Division by zero is underdefined!! "<<endl;
break;
}
}
cout << endl << "Quotient of " << i << " terms is" << div << endl;
}
/*void modu()
{
float a,b;
cout<<"Enter the number to find the modulus";
cin>>a;
b= a % 10;
}*/
//Trignometric Operations
void trignometry()
{
int a;
bool cont1=true;
char yesorno;
int switchvalue;
while(cont1)
{
cout<<"Enter a value for performing the calculation";
cout<<endl<<"1.Trigonometric function" <<endl<<"2.Inverse Trigonometric"<<endl;//<<"3.Hyperbolic Function"<<endl;//
cin>>a;
if(a==1)
{
trig();
}
else if(a==2)
{
inversetrig();
}
else
{
cout<<endl<<"Enter a valid number"<<endl;
continue;
}
//Checking whether the use want to continue the program//
tricheck:
cout<<"Enter (Y/N) for Trigonometric Operations(Both Trig and Inverse) "<<endl<<" Y for continue "<<endl<<" N for Exiting "<<endl;
cin>>yesorno;
if (yesorno == 'y' || yesorno == 'Y')
{
{
cont1 = true;
}
}
else if (yesorno == 'n' || yesorno == 'N')
{
{
cont1 = false;
cout << "Trigonometric Operations(Both Trig and Inverse) Completed Successfully" << endl;
}
}
else
{
cout << "Wrong Output." << endl;
goto tricheck;
}
}
}
void trig()
{
cout<<endl<<"Welcome To Trigonometric Calculation"<<endl;
bool cont1=true;
const double pi=3.14159265358979323846;
char yesorno;
int switchvalue;
while(cont1)
{
cout << endl << " 1.Sine "<<endl<<" 2.Cosine "<<endl<<" 3.Tangent "<<endl<<" 4.Cotangent "<<endl<<" 5.Secant "<<endl<<" 6.Cosecant"<<endl;
cin >> switchvalue;
if (switchvalue >= 1 && switchvalue <= 6)
{
switch (switchvalue)
{
case 1:
{
Sin(pi);
break;
}
case 2:
{
Cos(pi);
break;
}
case 3:
{
Tan(pi);
break;
}
case 4:
{
Cot(pi);
break;
}
case 5:
{
Sec(pi);
break;
}
case 6:
{
Cosec(pi);
break;
}
default:
{
cout<<"This will never get printed!!!"<<endl;
break;
}
}
}
else
{
cout << endl << "Enter a Valid number between 1 and 6"<<endl;
continue;
}
//Checking whether the use want to continue the program//
trigcheck:
cout << "Enter (Y/N) for Trigonometric Operations" << endl << "Y for continue " << endl << "N for Exiting " << endl;
cin >> yesorno;
if (yesorno == 'y' || yesorno == 'Y') {
{
cont1 = true;
}
} else if (yesorno == 'n' || yesorno == 'N') {
{
cont1 = false;
cout << "Trigonometric Operation Completed Successfully" << endl;
}
} else {
cout << "Wrong Output!!" << endl;
goto trigcheck;
}
}
}
double raddegcheck(int &num){
const double pi=3.14159265358979323846;
double b,c;
double radian;
cout<<endl<<"Enter your Choice"<<endl<<"1 for Degree"<<endl<<"2 for Radian"<<endl;
cin>>num;
if(num==1){
cout<<"Enter the number in degree"<<endl;
cin>>c;
radian = (c*pi)/180;
return radian;
}
else if(num==2){
cout<<"Enter the number is radian"<<endl;
cin>>c;
return c;
}
else{
cout<<"Wrong Input!!"<<endl;
}
}
void Sin(double pi)
{
int num;
double b=raddegcheck(num);
if(num==1||num==2){
double c=sin(b);
cout<<"Value is "<<c<<endl;
}
}
void Cos(double pi)
{
int num;
double b=raddegcheck(num);
if(num==1||num==2){
double c=cos(b);
cout<<"Value is "<<c<<endl;
}
}
void Tan(double pi)
{
int num;
double b=raddegcheck(num);
if(num==1||num==2){
double c=sin(b)/cos(b);
cout<<"Value is "<<c<<endl;
}
}
void Cot(double pi)
{
int num;
double b=raddegcheck(num);
if(num==1||num==2){
double c=cos(b)/sin(b);
cout<<"Value is "<<c<<endl;
}
}
void Sec(double pi)
{
int num;
double b=raddegcheck(num);
if(num==1||num==2){
double c=1/cos(b);
cout<<"Value is "<<c<<endl;
}
}
void Cosec(double pi)
{
int num;
double b=raddegcheck(num);
if(num==1||num==2){
double c=1/sin(b);
cout<<"Value is "<<c<<endl;
}
}
void inversetrig()
{
cout<<endl<<"Welcome To Inverse Trigonometric calculation"<<endl;
bool cont=true;
char yesorno;
int switchvalue;
while(cont){
cout << endl << "1.ArcSine "<<endl<<"2.ArcCosine "<<endl<<"3.ArcTangent "<<endl<<"4.ArcCotangent "<<endl<<"5.ArcSecant "<<endl<<"6.ArcCosecant"<<endl;
cin >> switchvalue;
if(switchvalue>0&&switchvalue<=6){
switch(switchvalue){
case 1:
{
ArcSin();
break;
}
case 2:
{
ArcCos();
break;
}
case 3:
{
ArcTan();
break;
}
case 4:
{
ArcCot();
break;
}
case 5:
{
ArcSec();
break;
}
case 6:
{
ArcCosec();
break;
}
default:
{
cout<<"This will never get printed!!!"<<endl;
break;
}
}
}
else{
cout<<endl<<"Enter a valid number between 1 and 6"<<endl;
}
inversetrigcheck:
cout << "Enter (Y/N) for Inverse Trigonometric Operations" << endl << "Y for continue " << endl << "N for Exiting " << endl;
cin >> yesorno;
if (yesorno == 'y' || yesorno == 'Y') {
{
cont = true;
}
} else if (yesorno == 'n' || yesorno == 'N') {
{
cont = false;
cout << "Inverse Trigonometric Operation Completed Successfully" << endl;
}
} else {
cout << "Wrong Output. " << endl;
goto inversetrigcheck;
}
}
}
void ArcSin()
{
double value;
cout<<endl<<"Enter the sin value"<<endl;
cin>>value;
double a=asin(value);
cout<<endl<<"Arc Sine Value(In Radian): "<<a<<endl;
}
void ArcCos()
{
double value;
cout<<endl<<"Enter the Cos value"<<endl;
cin>>value;
double a=acos(value);
cout<<endl<<"Arc Cos Value(In Radian): "<<a<<endl;
}
void ArcTan()
{
double value;
cout<<endl<<"Enter the Tan value"<<endl;
cin>>value;
double a=atan(value);
cout<<endl<<"Arc Tan Value(In Radian): "<<a<<endl;
}
void ArcCot()
{
double value;
cout<<endl<<"Enter the Cot value"<<endl;
cin>>value;
double a=atan(1/value);
cout<<endl<<"Arc Cot Value(In Radian): "<<a<<endl;
}
void ArcSec()
{
double value;
cout<<endl<<"Enter the sec value"<<endl;
cin>>value;
double a=acos(1/value);
cout<<endl<<"Arc Sec Value(In Radian): "<<a<<endl;
}
void ArcCosec()
{
double value;
cout<<endl<<"Enter the Cosec value"<<endl;
cin>>value;
double a=asin(1/value);
cout<<endl<<"Arc Cosec Value(In Radian): "<<a<<endl;
}
//Logarithmic Operations
void logarithmic()
{
cout<<"Enter the number whose log (base-10)value is to be found"<<endl;
int n;
cin>>n;
double ans=log10(n);
cout<<"Answer is "<<ans<<endl;
}
//Exponential operation
void exponential()
{
cout<<"Enter the number whose exponential is to be found"<<endl;
int n;
cin>>n;
cout<<"Enter the power to find"<<endl;
int power;
cin>>power;
long long ans=pow(n,power);
cout<<"The value of "<<n<<"^"<<power<<" is "<<ans<<endl;
}
//relational operations
void relational()
{
cout<<"How many numbers you want to check"<<endl;
int n;
cin>>n;
vector<int> ans;
cout<<"Enter your numbers"<<endl;
for(int i=0;i<n;i++){
int t;
cin>>t;
ans.push_back(t);
}
sort(ans.begin(), ans.end());
cout<<"Array in sorted manner"<<endl;
for(int i=0;i<n;i++){
cout<<ans[i]<<" ";
}
cout<<endl<<"Largest Number in the array "<<ans[ans.size()-1]<<endl;
cout<<"Smallest Number in the array "<<ans[0]<<endl;
}