-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_functions.dart
124 lines (100 loc) · 3.44 KB
/
calculator_functions.dart
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
import 'dart:io';
void main() {
int firstValue, secondValue, result, action;
double division;
print("Enter first value: ");
firstValue = int.parse(stdin.readLineSync()!);
print("Do you want to enter second value (Y/N): ");
String response = stdin.readLineSync()!;
if (response == 'Y' || response == 'y') {
print("Enter second value: ");
secondValue = int.parse(stdin.readLineSync()!);
} else {
secondValue = 0;
}
print("please select an action from below:");
print(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
action = int.parse(stdin.readLineSync()!);
if (action == 1) {
if(response == 'Y' || response == 'y') {
result = Addition(firstValue, secondValue, response);
print("The Addition of $firstValue and $secondValue is: $result");
}else{
result = Addition(firstValue, secondValue, response ,thirdValue:21);
print("The Addition of $firstValue and ${21} is: $result");
}
}
else if (action == 2) {
if(response == 'y' || response == 'Y'){
result = Subtraction(firstValue, secondValue, response);
print("The Subtraction of $firstValue and $secondValue is: $result");
}
else
{
result = Subtraction(firstValue, secondValue, response);
print("The Subtraction of $firstValue and ${12} is: $result");
}
} else if (action == 3) {
if(response == 'y' || response == 'Y'){
result = Multiplication(firstValue, secondValue, response);
print("The Multiplication of $firstValue and $secondValue is: $result");
}
else{
result = Multiplication(firstValue, secondValue, response);
print("The Multiplication of $firstValue and ${13} is: $result");
}
} else if (action == 4) {
if(response == 'y' || response == 'Y'){
division = Division(firstValue, secondValue, response);
print("The Division of $firstValue / $secondValue is: ${division.toStringAsFixed(2)}");// Round to 2 decimal places
} else if (response != 'y' || response != 'Y'){
division = Division(firstValue, secondValue, response);
print("The Division of $firstValue / ${23} is: ${division.toStringAsFixed(2)}");
} else if (secondValue == 0) {
print(
"$firstValue / $secondValue cannot be divided beacause it become infinity.");
}
} else {
print("invalid action");
}
}
int Addition(int firstValue, int secondValue, String response,{int? thirdValue, int? fourthValue}) {// Optional named parameter
int result = 0;
if(response == 'y' || response == 'Y'){
result = firstValue + secondValue;
}
else{
result = firstValue + thirdValue!;
}
return result;
}
int Subtraction(int firstValue, int secondValue, String response,{int? thirdValue = 12}) {// Default valued parameter
int result = 0;
if(response == 'y' || response == 'Y'){
result = firstValue - secondValue;
}
else{
result = firstValue - thirdValue!;
}
return result;
}
int Multiplication(int firstValue, int secondValue, String response,[int thirdValue = 13, int? fourthValue]) {// Optional positional parameter
int result = 0;
if(response == 'y' || response == 'Y'){
result = firstValue * secondValue;
}
else{
result = firstValue * thirdValue;
}
return result;
}
double Division(int firstValue, int secondValue, String response,[double thirdValue = 23]) { // Optional positional parameter
double result = 0;
if(response == 'y' || response == 'Y'){
result = firstValue / secondValue;
}
else{
result = firstValue / thirdValue;
}
return result;
}